blob: 9c541dd1b9fbc992719a9abdb56785d67f1d837a [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) {
1527
Evan Cheng1bc78042006-04-26 01:20:17 +00001528 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001529 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001530
Gordon Henriksen86737662008-01-05 16:56:59 +00001531 const Function* Fn = MF.getFunction();
1532 if (Fn->hasExternalLinkage() &&
1533 Subtarget->isTargetCygMing() &&
1534 Fn->getName() == "main")
1535 FuncInfo->setForceFramePointer(true);
1536
Evan Cheng1bc78042006-04-26 01:20:17 +00001537 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001538 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001539 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001540
Chris Lattner29689432010-03-11 00:22:57 +00001541 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1542 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001543
Chris Lattner638402b2007-02-28 07:00:42 +00001544 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001545 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1547 ArgLocs, *DAG.getContext());
1548 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Chris Lattnerf39f7712007-02-28 05:46:49 +00001550 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001551 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001552 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1553 CCValAssign &VA = ArgLocs[i];
1554 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1555 // places.
1556 assert(VA.getValNo() != LastVal &&
1557 "Don't support value assigned to multiple locs yet");
1558 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Chris Lattnerf39f7712007-02-28 05:46:49 +00001560 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001561 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001562 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001564 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001565 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001566 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001567 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001568 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001569 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001570 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001571 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001572 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001573 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1574 RC = X86::VR64RegisterClass;
1575 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001576 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001577
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001578 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001580
Chris Lattnerf39f7712007-02-28 05:46:49 +00001581 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1582 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1583 // right size.
1584 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001585 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001586 DAG.getValueType(VA.getValVT()));
1587 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001588 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001589 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001590 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001591 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001592
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001593 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001594 // Handle MMX values passed in XMM regs.
1595 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001596 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1597 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001598 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1599 } else
1600 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001601 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 } else {
1603 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001605 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001606
1607 // If value is passed via pointer - do a load.
1608 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001609 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1610 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001611
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001613 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001614
Dan Gohman61a92132008-04-21 23:59:07 +00001615 // The x86-64 ABI for returning structs by value requires that we copy
1616 // the sret argument into %rax for the return. Save the argument into
1617 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001618 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001619 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1620 unsigned Reg = FuncInfo->getSRetReturnReg();
1621 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001623 FuncInfo->setSRetReturnReg(Reg);
1624 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001627 }
1628
Chris Lattnerf39f7712007-02-28 05:46:49 +00001629 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001630 // Align stack specially for tail calls.
1631 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001632 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001633
Evan Cheng1bc78042006-04-26 01:20:17 +00001634 // If the function takes variable number of arguments, make a frame index for
1635 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001636 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001637 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001638 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 }
1640 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001641 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1642
1643 // FIXME: We should really autogenerate these arrays
1644 static const unsigned GPR64ArgRegsWin64[] = {
1645 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001646 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001647 static const unsigned XMMArgRegsWin64[] = {
1648 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1649 };
1650 static const unsigned GPR64ArgRegs64Bit[] = {
1651 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1652 };
1653 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1655 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1656 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001657 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1658
1659 if (IsWin64) {
1660 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1661 GPR64ArgRegs = GPR64ArgRegsWin64;
1662 XMMArgRegs = XMMArgRegsWin64;
1663 } else {
1664 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1665 GPR64ArgRegs = GPR64ArgRegs64Bit;
1666 XMMArgRegs = XMMArgRegs64Bit;
1667 }
1668 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1669 TotalNumIntRegs);
1670 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1671 TotalNumXMMRegs);
1672
Devang Patel578efa92009-06-05 21:57:13 +00001673 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001674 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001675 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001676 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001677 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001678 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001679 // Kernel mode asks for SSE to be disabled, so don't push them
1680 // on the stack.
1681 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001682
Gordon Henriksen86737662008-01-05 16:56:59 +00001683 // For X86-64, if there are vararg parameters that are passed via
1684 // registers, then we must store them to their spots on the stack so they
1685 // may be loaded by deferencing the result of va_next.
1686 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001687 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1688 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001689 TotalNumXMMRegs * 16, 16,
1690 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001691
Gordon Henriksen86737662008-01-05 16:56:59 +00001692 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001693 SmallVector<SDValue, 8> MemOps;
1694 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001695 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001696 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001697 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1698 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001699 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1700 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001702 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001703 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001704 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
David Greene67c9d422010-02-15 16:53:33 +00001705 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001707 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001709
Dan Gohmanface41a2009-08-16 21:24:25 +00001710 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1711 // Now store the XMM (fp + vector) parameter registers.
1712 SmallVector<SDValue, 11> SaveXMMOps;
1713 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001714
Dan Gohmanface41a2009-08-16 21:24:25 +00001715 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1716 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1717 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001718
Dan Gohmanface41a2009-08-16 21:24:25 +00001719 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1720 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001721
Dan Gohmanface41a2009-08-16 21:24:25 +00001722 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1723 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1724 X86::VR128RegisterClass);
1725 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1726 SaveXMMOps.push_back(Val);
1727 }
1728 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1729 MVT::Other,
1730 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001731 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001732
1733 if (!MemOps.empty())
1734 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1735 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001736 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001737 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001738
Gordon Henriksen86737662008-01-05 16:56:59 +00001739 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001741 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001742 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001743 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001744 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001745 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001746 BytesToPopOnReturn = 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001747 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001748
Gordon Henriksen86737662008-01-05 16:56:59 +00001749 if (!Is64Bit) {
1750 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001751 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001752 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1753 }
Evan Cheng25caf632006-05-23 21:06:34 +00001754
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001755 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001756
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001758}
1759
Dan Gohman475871a2008-07-27 21:46:04 +00001760SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1762 SDValue StackPtr, SDValue Arg,
1763 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001764 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001765 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001766 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001767 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001768 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001769 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001770 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001771 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001772 }
Dale Johannesenace16102009-02-03 19:33:06 +00001773 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001774 PseudoSourceValue::getStack(), LocMemOffset,
1775 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001776}
1777
Bill Wendling64e87322009-01-16 19:25:27 +00001778/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001779/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001780SDValue
1781X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001782 SDValue &OutRetAddr, SDValue Chain,
1783 bool IsTailCall, bool Is64Bit,
1784 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001785 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001786 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001787 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001788
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001789 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001790 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001791 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001792}
1793
1794/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1795/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001796static SDValue
1797EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001798 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001799 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001800 // Store the return address to the appropriate stack slot.
1801 if (!FPDiff) return Chain;
1802 // Calculate the new stack slot for the return address.
1803 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001804 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001805 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001807 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001808 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001809 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1810 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001811 return Chain;
1812}
1813
Dan Gohman98ca4f22009-08-05 01:29:28 +00001814SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001815X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001816 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001817 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001818 const SmallVectorImpl<ISD::OutputArg> &Outs,
1819 const SmallVectorImpl<ISD::InputArg> &Ins,
1820 DebugLoc dl, SelectionDAG &DAG,
1821 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001822 MachineFunction &MF = DAG.getMachineFunction();
1823 bool Is64Bit = Subtarget->is64Bit();
1824 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001825 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001826
Evan Cheng5f941932010-02-05 02:21:12 +00001827 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001828 // Check if it's really possible to do a tail call.
Evan Cheng022d9e12010-02-02 23:55:14 +00001829 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
1830 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001831
1832 // Sibcalls are automatically detected tailcalls which do not require
1833 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001834 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001835 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001836
1837 if (isTailCall)
1838 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001839 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001840
Chris Lattner29689432010-03-11 00:22:57 +00001841 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1842 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001843
Chris Lattner638402b2007-02-28 07:00:42 +00001844 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001845 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001846 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1847 ArgLocs, *DAG.getContext());
1848 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001849
Chris Lattner423c5f42007-02-28 05:31:48 +00001850 // Get a count of how many bytes are to be pushed on the stack.
1851 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001852 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001853 // This is a sibcall. The memory operands are available in caller's
1854 // own caller's stack.
1855 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001856 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001857 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001858
Gordon Henriksen86737662008-01-05 16:56:59 +00001859 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001860 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001861 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001862 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1864 FPDiff = NumBytesCallerPushed - NumBytes;
1865
1866 // Set the delta of movement of the returnaddr stackslot.
1867 // But only set if delta is greater than previous delta.
1868 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1869 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1870 }
1871
Evan Chengf22f9b32010-02-06 03:28:46 +00001872 if (!IsSibcall)
1873 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001874
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001876 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001877 if (isTailCall && FPDiff)
1878 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1879 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001880
Dan Gohman475871a2008-07-27 21:46:04 +00001881 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1882 SmallVector<SDValue, 8> MemOpChains;
1883 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001884
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001885 // Walk the register/memloc assignments, inserting copies/loads. In the case
1886 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001887 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1888 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001889 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001890 SDValue Arg = Outs[i].Val;
1891 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001892 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001893
Chris Lattner423c5f42007-02-28 05:31:48 +00001894 // Promote the value if needed.
1895 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001896 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001897 case CCValAssign::Full: break;
1898 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001899 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001900 break;
1901 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001902 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001903 break;
1904 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001905 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1906 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1908 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1909 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001910 } else
1911 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1912 break;
1913 case CCValAssign::BCvt:
1914 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001915 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001916 case CCValAssign::Indirect: {
1917 // Store the argument.
1918 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001919 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001920 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001921 PseudoSourceValue::getFixedStack(FI), 0,
1922 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001923 Arg = SpillSlot;
1924 break;
1925 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001926 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001927
Chris Lattner423c5f42007-02-28 05:31:48 +00001928 if (VA.isRegLoc()) {
1929 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001930 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001931 assert(VA.isMemLoc());
1932 if (StackPtr.getNode() == 0)
1933 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1934 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1935 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001936 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001938
Evan Cheng32fe1032006-05-25 00:59:30 +00001939 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001941 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001942
Evan Cheng347d5f72006-04-28 21:29:37 +00001943 // Build a sequence of copy-to-reg nodes chained together with token chain
1944 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001946 // Tail call byval lowering might overwrite argument registers so in case of
1947 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001948 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001949 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001950 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001951 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001952 InFlag = Chain.getValue(1);
1953 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001954
Chris Lattner88e1fd52009-07-09 04:24:46 +00001955 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001956 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1957 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001958 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001959 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1960 DAG.getNode(X86ISD::GlobalBaseReg,
1961 DebugLoc::getUnknownLoc(),
1962 getPointerTy()),
1963 InFlag);
1964 InFlag = Chain.getValue(1);
1965 } else {
1966 // If we are tail calling and generating PIC/GOT style code load the
1967 // address of the callee into ECX. The value in ecx is used as target of
1968 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1969 // for tail calls on PIC/GOT architectures. Normally we would just put the
1970 // address of GOT into ebx and then call target@PLT. But for tail calls
1971 // ebx would be restored (since ebx is callee saved) before jumping to the
1972 // target@PLT.
1973
1974 // Note: The actual moving to ECX is done further down.
1975 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1976 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1977 !G->getGlobal()->hasProtectedVisibility())
1978 Callee = LowerGlobalAddress(Callee, DAG);
1979 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001980 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001981 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001982 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001983
Gordon Henriksen86737662008-01-05 16:56:59 +00001984 if (Is64Bit && isVarArg) {
1985 // From AMD64 ABI document:
1986 // For calls that may call functions that use varargs or stdargs
1987 // (prototype-less calls or calls to functions containing ellipsis (...) in
1988 // the declaration) %al is used as hidden argument to specify the number
1989 // of SSE registers used. The contents of %al do not need to match exactly
1990 // the number of registers, but must be an ubound on the number of SSE
1991 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001992
1993 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001994 // Count the number of XMM registers allocated.
1995 static const unsigned XMMArgRegs[] = {
1996 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1997 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1998 };
1999 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002000 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002001 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002002
Dale Johannesendd64c412009-02-04 00:33:20 +00002003 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002005 InFlag = Chain.getValue(1);
2006 }
2007
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002008
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002009 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002010 if (isTailCall) {
2011 // Force all the incoming stack arguments to be loaded from the stack
2012 // before any new outgoing arguments are stored to the stack, because the
2013 // outgoing stack slots may alias the incoming argument stack slots, and
2014 // the alias isn't otherwise explicit. This is slightly more conservative
2015 // than necessary, because it means that each store effectively depends
2016 // on every argument instead of just those arguments it would clobber.
2017 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2018
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SmallVector<SDValue, 8> MemOpChains2;
2020 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002021 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002022 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002023 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002024 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002025 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2026 CCValAssign &VA = ArgLocs[i];
2027 if (VA.isRegLoc())
2028 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002029 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002030 SDValue Arg = Outs[i].Val;
2031 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 // Create frame index.
2033 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002034 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002035 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002036 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002037
Duncan Sands276dcbd2008-03-21 09:14:45 +00002038 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002039 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002040 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002041 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002042 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002043 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002044 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002045
Dan Gohman98ca4f22009-08-05 01:29:28 +00002046 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2047 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002048 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002049 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002050 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002051 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002052 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002053 PseudoSourceValue::getFixedStack(FI), 0,
2054 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002055 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002056 }
2057 }
2058
2059 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002061 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002062
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002063 // Copy arguments to their registers.
2064 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002065 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002066 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002067 InFlag = Chain.getValue(1);
2068 }
Dan Gohman475871a2008-07-27 21:46:04 +00002069 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002070
Gordon Henriksen86737662008-01-05 16:56:59 +00002071 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002072 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002073 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002074 }
2075
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002076 bool WasGlobalOrExternal = false;
2077 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2078 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2079 // In the 64-bit large code model, we have to make all calls
2080 // through a register, since the call instruction's 32-bit
2081 // pc-relative offset may not be large enough to hold the whole
2082 // address.
2083 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2084 WasGlobalOrExternal = true;
2085 // If the callee is a GlobalAddress node (quite common, every direct call
2086 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2087 // it.
2088
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002089 // We should use extra load for direct calls to dllimported functions in
2090 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002091 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002092 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002093 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002094
Chris Lattner48a7d022009-07-09 05:02:21 +00002095 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2096 // external symbols most go through the PLT in PIC mode. If the symbol
2097 // has hidden or protected visibility, or if it is static or local, then
2098 // we don't need to use the PLT - we can directly call it.
2099 if (Subtarget->isTargetELF() &&
2100 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002101 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002102 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002103 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002104 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2105 Subtarget->getDarwinVers() < 9) {
2106 // PC-relative references to external symbols should go through $stub,
2107 // unless we're building with the leopard linker or later, which
2108 // automatically synthesizes these stubs.
2109 OpFlags = X86II::MO_DARWIN_STUB;
2110 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002111
Chris Lattner74e726e2009-07-09 05:27:35 +00002112 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002113 G->getOffset(), OpFlags);
2114 }
Bill Wendling056292f2008-09-16 21:48:12 +00002115 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002116 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002117 unsigned char OpFlags = 0;
2118
2119 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2120 // symbols should go through the PLT.
2121 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002122 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002123 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002124 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002125 Subtarget->getDarwinVers() < 9) {
2126 // PC-relative references to external symbols should go through $stub,
2127 // unless we're building with the leopard linker or later, which
2128 // automatically synthesizes these stubs.
2129 OpFlags = X86II::MO_DARWIN_STUB;
2130 }
Eric Christopherfd179292009-08-27 18:07:15 +00002131
Chris Lattner48a7d022009-07-09 05:02:21 +00002132 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2133 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002134 }
2135
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 Chengb1712452010-01-27 06:25:16 +00002327 const SmallVectorImpl<ISD::OutputArg> &Outs,
2328 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002329 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002330 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002331 CalleeCC != CallingConv::C)
2332 return false;
2333
Evan Cheng7096ae42010-01-29 06:45:59 +00002334 // If -tailcallopt is specified, make fastcc functions tail-callable.
2335 const Function *CallerF = DAG.getMachineFunction().getFunction();
Dan Gohman1797ed52010-02-08 20:27:50 +00002336 if (GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00002337 if (IsTailCallConvention(CalleeCC) &&
Evan Cheng843bd692010-01-31 06:44:49 +00002338 CallerF->getCallingConv() == CalleeCC)
2339 return true;
2340 return false;
2341 }
2342
Evan Chengb2c92902010-02-02 02:22:50 +00002343 // Look for obvious safe cases to perform tail call optimization that does not
2344 // requite ABI changes. This is what gcc calls sibcall.
2345
Evan Cheng843bd692010-01-31 06:44:49 +00002346 // Do not tail call optimize vararg calls for now.
2347 if (isVarArg)
2348 return false;
2349
Evan Chenga6bff982010-01-30 01:22:00 +00002350 // If the callee takes no arguments then go on to check the results of the
2351 // call.
2352 if (!Outs.empty()) {
2353 // Check if stack adjustment is needed. For now, do not do this if any
2354 // argument is passed on the stack.
2355 SmallVector<CCValAssign, 16> ArgLocs;
2356 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2357 ArgLocs, *DAG.getContext());
2358 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002359 if (CCInfo.getNextStackOffset()) {
2360 MachineFunction &MF = DAG.getMachineFunction();
2361 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2362 return false;
2363 if (Subtarget->isTargetWin64())
2364 // Win64 ABI has additional complications.
2365 return false;
2366
2367 // Check if the arguments are already laid out in the right way as
2368 // the caller's fixed stack objects.
2369 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002370 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2371 const X86InstrInfo *TII =
2372 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002373 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2374 CCValAssign &VA = ArgLocs[i];
2375 EVT RegVT = VA.getLocVT();
2376 SDValue Arg = Outs[i].Val;
2377 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002378 if (VA.getLocInfo() == CCValAssign::Indirect)
2379 return false;
2380 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002381 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2382 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002383 return false;
2384 }
2385 }
2386 }
Evan Chenga6bff982010-01-30 01:22:00 +00002387 }
Evan Chengb1712452010-01-27 06:25:16 +00002388
Evan Cheng86809cc2010-02-03 03:28:02 +00002389 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002390}
2391
Dan Gohman3df24e62008-09-03 23:12:08 +00002392FastISel *
Evan Chengddc419c2010-01-26 19:04:47 +00002393X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2394 DwarfWriter *dw,
2395 DenseMap<const Value *, unsigned> &vm,
2396 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2397 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002398#ifndef NDEBUG
Evan Chengddc419c2010-01-26 19:04:47 +00002399 , SmallSet<Instruction*, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002400#endif
2401 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002402 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002403#ifndef NDEBUG
2404 , cil
2405#endif
2406 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002407}
2408
2409
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002410//===----------------------------------------------------------------------===//
2411// Other Lowering Hooks
2412//===----------------------------------------------------------------------===//
2413
2414
Dan Gohman475871a2008-07-27 21:46:04 +00002415SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002416 MachineFunction &MF = DAG.getMachineFunction();
2417 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2418 int ReturnAddrIndex = FuncInfo->getRAIndex();
2419
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002420 if (ReturnAddrIndex == 0) {
2421 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002422 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002423 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002424 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002425 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002426 }
2427
Evan Cheng25ab6902006-09-08 06:48:29 +00002428 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002429}
2430
2431
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002432bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2433 bool hasSymbolicDisplacement) {
2434 // Offset should fit into 32 bit immediate field.
2435 if (!isInt32(Offset))
2436 return false;
2437
2438 // If we don't have a symbolic displacement - we don't have any extra
2439 // restrictions.
2440 if (!hasSymbolicDisplacement)
2441 return true;
2442
2443 // FIXME: Some tweaks might be needed for medium code model.
2444 if (M != CodeModel::Small && M != CodeModel::Kernel)
2445 return false;
2446
2447 // For small code model we assume that latest object is 16MB before end of 31
2448 // bits boundary. We may also accept pretty large negative constants knowing
2449 // that all objects are in the positive half of address space.
2450 if (M == CodeModel::Small && Offset < 16*1024*1024)
2451 return true;
2452
2453 // For kernel code model we know that all object resist in the negative half
2454 // of 32bits address space. We may not accept negative offsets, since they may
2455 // be just off and we may accept pretty large positive ones.
2456 if (M == CodeModel::Kernel && Offset > 0)
2457 return true;
2458
2459 return false;
2460}
2461
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002462/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2463/// specific condition code, returning the condition code and the LHS/RHS of the
2464/// comparison to make.
2465static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2466 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002467 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002468 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2469 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2470 // X > -1 -> X == 0, jump !sign.
2471 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002472 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002473 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2474 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002475 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002476 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002477 // X < 1 -> X <= 0
2478 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002479 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002480 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002481 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002482
Evan Chengd9558e02006-01-06 00:43:03 +00002483 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002484 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002485 case ISD::SETEQ: return X86::COND_E;
2486 case ISD::SETGT: return X86::COND_G;
2487 case ISD::SETGE: return X86::COND_GE;
2488 case ISD::SETLT: return X86::COND_L;
2489 case ISD::SETLE: return X86::COND_LE;
2490 case ISD::SETNE: return X86::COND_NE;
2491 case ISD::SETULT: return X86::COND_B;
2492 case ISD::SETUGT: return X86::COND_A;
2493 case ISD::SETULE: return X86::COND_BE;
2494 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002495 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002496 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002497
Chris Lattner4c78e022008-12-23 23:42:27 +00002498 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002499
Chris Lattner4c78e022008-12-23 23:42:27 +00002500 // If LHS is a foldable load, but RHS is not, flip the condition.
2501 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2502 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2503 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2504 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002505 }
2506
Chris Lattner4c78e022008-12-23 23:42:27 +00002507 switch (SetCCOpcode) {
2508 default: break;
2509 case ISD::SETOLT:
2510 case ISD::SETOLE:
2511 case ISD::SETUGT:
2512 case ISD::SETUGE:
2513 std::swap(LHS, RHS);
2514 break;
2515 }
2516
2517 // On a floating point condition, the flags are set as follows:
2518 // ZF PF CF op
2519 // 0 | 0 | 0 | X > Y
2520 // 0 | 0 | 1 | X < Y
2521 // 1 | 0 | 0 | X == Y
2522 // 1 | 1 | 1 | unordered
2523 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002524 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002525 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002526 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002527 case ISD::SETOLT: // flipped
2528 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002529 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002530 case ISD::SETOLE: // flipped
2531 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002532 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002533 case ISD::SETUGT: // flipped
2534 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002535 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002536 case ISD::SETUGE: // flipped
2537 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002538 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002539 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002540 case ISD::SETNE: return X86::COND_NE;
2541 case ISD::SETUO: return X86::COND_P;
2542 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002543 case ISD::SETOEQ:
2544 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002545 }
Evan Chengd9558e02006-01-06 00:43:03 +00002546}
2547
Evan Cheng4a460802006-01-11 00:33:36 +00002548/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2549/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002550/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002551static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002552 switch (X86CC) {
2553 default:
2554 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002555 case X86::COND_B:
2556 case X86::COND_BE:
2557 case X86::COND_E:
2558 case X86::COND_P:
2559 case X86::COND_A:
2560 case X86::COND_AE:
2561 case X86::COND_NE:
2562 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002563 return true;
2564 }
2565}
2566
Evan Chengeb2f9692009-10-27 19:56:55 +00002567/// isFPImmLegal - Returns true if the target can instruction select the
2568/// specified FP immediate natively. If false, the legalizer will
2569/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002570bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002571 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2572 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2573 return true;
2574 }
2575 return false;
2576}
2577
Nate Begeman9008ca62009-04-27 18:41:29 +00002578/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2579/// the specified range (L, H].
2580static bool isUndefOrInRange(int Val, int Low, int Hi) {
2581 return (Val < 0) || (Val >= Low && Val < Hi);
2582}
2583
2584/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2585/// specified value.
2586static bool isUndefOrEqual(int Val, int CmpVal) {
2587 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002588 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002589 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002590}
2591
Nate Begeman9008ca62009-04-27 18:41:29 +00002592/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2593/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2594/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002595static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002596 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002597 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002598 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 return (Mask[0] < 2 && Mask[1] < 2);
2600 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002601}
2602
Nate Begeman9008ca62009-04-27 18:41:29 +00002603bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002604 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002605 N->getMask(M);
2606 return ::isPSHUFDMask(M, N->getValueType(0));
2607}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002608
Nate Begeman9008ca62009-04-27 18:41:29 +00002609/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2610/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002611static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002612 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002613 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002614
Nate Begeman9008ca62009-04-27 18:41:29 +00002615 // Lower quadword copied in order or undef.
2616 for (int i = 0; i != 4; ++i)
2617 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002618 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002619
Evan Cheng506d3df2006-03-29 23:07:14 +00002620 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002621 for (int i = 4; i != 8; ++i)
2622 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002623 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002624
Evan Cheng506d3df2006-03-29 23:07:14 +00002625 return true;
2626}
2627
Nate Begeman9008ca62009-04-27 18:41:29 +00002628bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002629 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 N->getMask(M);
2631 return ::isPSHUFHWMask(M, N->getValueType(0));
2632}
Evan Cheng506d3df2006-03-29 23:07:14 +00002633
Nate Begeman9008ca62009-04-27 18:41:29 +00002634/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2635/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002636static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002637 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002638 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002639
Rafael Espindola15684b22009-04-24 12:40:33 +00002640 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002641 for (int i = 4; i != 8; ++i)
2642 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002643 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002644
Rafael Espindola15684b22009-04-24 12:40:33 +00002645 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002646 for (int i = 0; i != 4; ++i)
2647 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002648 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002649
Rafael Espindola15684b22009-04-24 12:40:33 +00002650 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002651}
2652
Nate Begeman9008ca62009-04-27 18:41:29 +00002653bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002654 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002655 N->getMask(M);
2656 return ::isPSHUFLWMask(M, N->getValueType(0));
2657}
2658
Nate Begemana09008b2009-10-19 02:17:23 +00002659/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2660/// is suitable for input to PALIGNR.
2661static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2662 bool hasSSSE3) {
2663 int i, e = VT.getVectorNumElements();
2664
2665 // Do not handle v2i64 / v2f64 shuffles with palignr.
2666 if (e < 4 || !hasSSSE3)
2667 return false;
2668
2669 for (i = 0; i != e; ++i)
2670 if (Mask[i] >= 0)
2671 break;
2672
2673 // All undef, not a palignr.
2674 if (i == e)
2675 return false;
2676
2677 // Determine if it's ok to perform a palignr with only the LHS, since we
2678 // don't have access to the actual shuffle elements to see if RHS is undef.
2679 bool Unary = Mask[i] < (int)e;
2680 bool NeedsUnary = false;
2681
2682 int s = Mask[i] - i;
2683
2684 // Check the rest of the elements to see if they are consecutive.
2685 for (++i; i != e; ++i) {
2686 int m = Mask[i];
2687 if (m < 0)
2688 continue;
2689
2690 Unary = Unary && (m < (int)e);
2691 NeedsUnary = NeedsUnary || (m < s);
2692
2693 if (NeedsUnary && !Unary)
2694 return false;
2695 if (Unary && m != ((s+i) & (e-1)))
2696 return false;
2697 if (!Unary && m != (s+i))
2698 return false;
2699 }
2700 return true;
2701}
2702
2703bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2704 SmallVector<int, 8> M;
2705 N->getMask(M);
2706 return ::isPALIGNRMask(M, N->getValueType(0), true);
2707}
2708
Evan Cheng14aed5e2006-03-24 01:18:28 +00002709/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2710/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002711static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002712 int NumElems = VT.getVectorNumElements();
2713 if (NumElems != 2 && NumElems != 4)
2714 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002715
Nate Begeman9008ca62009-04-27 18:41:29 +00002716 int Half = NumElems / 2;
2717 for (int i = 0; i < Half; ++i)
2718 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002719 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002720 for (int i = Half; i < NumElems; ++i)
2721 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002722 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002723
Evan Cheng14aed5e2006-03-24 01:18:28 +00002724 return true;
2725}
2726
Nate Begeman9008ca62009-04-27 18:41:29 +00002727bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2728 SmallVector<int, 8> M;
2729 N->getMask(M);
2730 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002731}
2732
Evan Cheng213d2cf2007-05-17 18:45:50 +00002733/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002734/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2735/// half elements to come from vector 1 (which would equal the dest.) and
2736/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002737static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002738 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002739
2740 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002741 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002742
Nate Begeman9008ca62009-04-27 18:41:29 +00002743 int Half = NumElems / 2;
2744 for (int i = 0; i < Half; ++i)
2745 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002746 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002747 for (int i = Half; i < NumElems; ++i)
2748 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002749 return false;
2750 return true;
2751}
2752
Nate Begeman9008ca62009-04-27 18:41:29 +00002753static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2754 SmallVector<int, 8> M;
2755 N->getMask(M);
2756 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002757}
2758
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002759/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2760/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002761bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2762 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002763 return false;
2764
Evan Cheng2064a2b2006-03-28 06:50:32 +00002765 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002766 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2767 isUndefOrEqual(N->getMaskElt(1), 7) &&
2768 isUndefOrEqual(N->getMaskElt(2), 2) &&
2769 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002770}
2771
Nate Begeman0b10b912009-11-07 23:17:15 +00002772/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2773/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2774/// <2, 3, 2, 3>
2775bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2776 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2777
2778 if (NumElems != 4)
2779 return false;
2780
2781 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2782 isUndefOrEqual(N->getMaskElt(1), 3) &&
2783 isUndefOrEqual(N->getMaskElt(2), 2) &&
2784 isUndefOrEqual(N->getMaskElt(3), 3);
2785}
2786
Evan Cheng5ced1d82006-04-06 23:23:56 +00002787/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2788/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002789bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2790 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002791
Evan Cheng5ced1d82006-04-06 23:23:56 +00002792 if (NumElems != 2 && NumElems != 4)
2793 return false;
2794
Evan Chengc5cdff22006-04-07 21:53:05 +00002795 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002796 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002797 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002798
Evan Chengc5cdff22006-04-07 21:53:05 +00002799 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002800 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002801 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002802
2803 return true;
2804}
2805
Nate Begeman0b10b912009-11-07 23:17:15 +00002806/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2807/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2808bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002809 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002810
Evan Cheng5ced1d82006-04-06 23:23:56 +00002811 if (NumElems != 2 && NumElems != 4)
2812 return false;
2813
Evan Chengc5cdff22006-04-07 21:53:05 +00002814 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002815 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002816 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002817
Nate Begeman9008ca62009-04-27 18:41:29 +00002818 for (unsigned i = 0; i < NumElems/2; ++i)
2819 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002820 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002821
2822 return true;
2823}
2824
Evan Cheng0038e592006-03-28 00:39:58 +00002825/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2826/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002827static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002828 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002830 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002831 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002832
Nate Begeman9008ca62009-04-27 18:41:29 +00002833 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2834 int BitI = Mask[i];
2835 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002836 if (!isUndefOrEqual(BitI, j))
2837 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002838 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002839 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002840 return false;
2841 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002842 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002843 return false;
2844 }
Evan Cheng0038e592006-03-28 00:39:58 +00002845 }
Evan Cheng0038e592006-03-28 00:39:58 +00002846 return true;
2847}
2848
Nate Begeman9008ca62009-04-27 18:41:29 +00002849bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2850 SmallVector<int, 8> M;
2851 N->getMask(M);
2852 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002853}
2854
Evan Cheng4fcb9222006-03-28 02:43:26 +00002855/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2856/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002857static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002858 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002859 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002860 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002861 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002862
Nate Begeman9008ca62009-04-27 18:41:29 +00002863 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2864 int BitI = Mask[i];
2865 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002866 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002867 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002868 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002869 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002870 return false;
2871 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002872 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002873 return false;
2874 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002875 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002876 return true;
2877}
2878
Nate Begeman9008ca62009-04-27 18:41:29 +00002879bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2880 SmallVector<int, 8> M;
2881 N->getMask(M);
2882 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002883}
2884
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002885/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2886/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2887/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002888static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002889 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002890 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002891 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002892
Nate Begeman9008ca62009-04-27 18:41:29 +00002893 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2894 int BitI = Mask[i];
2895 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002896 if (!isUndefOrEqual(BitI, j))
2897 return false;
2898 if (!isUndefOrEqual(BitI1, j))
2899 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002900 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002901 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002902}
2903
Nate Begeman9008ca62009-04-27 18:41:29 +00002904bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2905 SmallVector<int, 8> M;
2906 N->getMask(M);
2907 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2908}
2909
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002910/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2911/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2912/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002913static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002914 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002915 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2916 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002917
Nate Begeman9008ca62009-04-27 18:41:29 +00002918 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2919 int BitI = Mask[i];
2920 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002921 if (!isUndefOrEqual(BitI, j))
2922 return false;
2923 if (!isUndefOrEqual(BitI1, j))
2924 return false;
2925 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002926 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002927}
2928
Nate Begeman9008ca62009-04-27 18:41:29 +00002929bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2930 SmallVector<int, 8> M;
2931 N->getMask(M);
2932 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2933}
2934
Evan Cheng017dcc62006-04-21 01:05:10 +00002935/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2936/// specifies a shuffle of elements that is suitable for input to MOVSS,
2937/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002938static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002939 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002940 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002941
2942 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002945 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002946
Nate Begeman9008ca62009-04-27 18:41:29 +00002947 for (int i = 1; i < NumElts; ++i)
2948 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002949 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002950
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002951 return true;
2952}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002953
Nate Begeman9008ca62009-04-27 18:41:29 +00002954bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2955 SmallVector<int, 8> M;
2956 N->getMask(M);
2957 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002958}
2959
Evan Cheng017dcc62006-04-21 01:05:10 +00002960/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2961/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002962/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002963static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 bool V2IsSplat = false, bool V2IsUndef = false) {
2965 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002966 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002967 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002968
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002970 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002971
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 for (int i = 1; i < NumOps; ++i)
2973 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2974 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2975 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002976 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002977
Evan Cheng39623da2006-04-20 08:58:49 +00002978 return true;
2979}
2980
Nate Begeman9008ca62009-04-27 18:41:29 +00002981static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002982 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002983 SmallVector<int, 8> M;
2984 N->getMask(M);
2985 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002986}
2987
Evan Chengd9539472006-04-14 21:59:03 +00002988/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2989/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002990bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2991 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002992 return false;
2993
2994 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002995 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002996 int Elt = N->getMaskElt(i);
2997 if (Elt >= 0 && Elt != 1)
2998 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002999 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003000
3001 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003002 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003003 int Elt = N->getMaskElt(i);
3004 if (Elt >= 0 && Elt != 3)
3005 return false;
3006 if (Elt == 3)
3007 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003008 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003009 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003010 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003011 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003012}
3013
3014/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3015/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003016bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3017 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003018 return false;
3019
3020 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 for (unsigned i = 0; i < 2; ++i)
3022 if (N->getMaskElt(i) > 0)
3023 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003024
3025 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003026 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003027 int Elt = N->getMaskElt(i);
3028 if (Elt >= 0 && Elt != 2)
3029 return false;
3030 if (Elt == 2)
3031 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003032 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003034 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003035}
3036
Evan Cheng0b457f02008-09-25 20:50:48 +00003037/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3038/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003039bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3040 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003041
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 for (int i = 0; i < e; ++i)
3043 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003044 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003045 for (int i = 0; i < e; ++i)
3046 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003047 return false;
3048 return true;
3049}
3050
Evan Cheng63d33002006-03-22 08:01:21 +00003051/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003052/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003053unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003054 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3055 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3056
Evan Chengb9df0ca2006-03-22 02:53:00 +00003057 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3058 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003059 for (int i = 0; i < NumOperands; ++i) {
3060 int Val = SVOp->getMaskElt(NumOperands-i-1);
3061 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003062 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003063 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003064 if (i != NumOperands - 1)
3065 Mask <<= Shift;
3066 }
Evan Cheng63d33002006-03-22 08:01:21 +00003067 return Mask;
3068}
3069
Evan Cheng506d3df2006-03-29 23:07:14 +00003070/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003071/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003072unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003073 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003074 unsigned Mask = 0;
3075 // 8 nodes, but we only care about the last 4.
3076 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 int Val = SVOp->getMaskElt(i);
3078 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003079 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003080 if (i != 4)
3081 Mask <<= 2;
3082 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003083 return Mask;
3084}
3085
3086/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003087/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003088unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003089 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003090 unsigned Mask = 0;
3091 // 8 nodes, but we only care about the first 4.
3092 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003093 int Val = SVOp->getMaskElt(i);
3094 if (Val >= 0)
3095 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003096 if (i != 0)
3097 Mask <<= 2;
3098 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003099 return Mask;
3100}
3101
Nate Begemana09008b2009-10-19 02:17:23 +00003102/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3103/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3104unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3105 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3106 EVT VVT = N->getValueType(0);
3107 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3108 int Val = 0;
3109
3110 unsigned i, e;
3111 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3112 Val = SVOp->getMaskElt(i);
3113 if (Val >= 0)
3114 break;
3115 }
3116 return (Val - i) * EltSize;
3117}
3118
Evan Cheng37b73872009-07-30 08:33:02 +00003119/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3120/// constant +0.0.
3121bool X86::isZeroNode(SDValue Elt) {
3122 return ((isa<ConstantSDNode>(Elt) &&
3123 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3124 (isa<ConstantFPSDNode>(Elt) &&
3125 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3126}
3127
Nate Begeman9008ca62009-04-27 18:41:29 +00003128/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3129/// their permute mask.
3130static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3131 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003132 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003133 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003135
Nate Begeman5a5ca152009-04-29 05:20:52 +00003136 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003137 int idx = SVOp->getMaskElt(i);
3138 if (idx < 0)
3139 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003140 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003141 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003142 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003143 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003144 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003145 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3146 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003147}
3148
Evan Cheng779ccea2007-12-07 21:30:01 +00003149/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3150/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003151static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003152 unsigned NumElems = VT.getVectorNumElements();
3153 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 int idx = Mask[i];
3155 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003156 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003157 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003159 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003161 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003162}
3163
Evan Cheng533a0aa2006-04-19 20:35:22 +00003164/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3165/// match movhlps. The lower half elements should come from upper half of
3166/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003167/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003168static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3169 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003170 return false;
3171 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003173 return false;
3174 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003176 return false;
3177 return true;
3178}
3179
Evan Cheng5ced1d82006-04-06 23:23:56 +00003180/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003181/// is promoted to a vector. It also returns the LoadSDNode by reference if
3182/// required.
3183static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003184 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3185 return false;
3186 N = N->getOperand(0).getNode();
3187 if (!ISD::isNON_EXTLoad(N))
3188 return false;
3189 if (LD)
3190 *LD = cast<LoadSDNode>(N);
3191 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003192}
3193
Evan Cheng533a0aa2006-04-19 20:35:22 +00003194/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3195/// match movlp{s|d}. The lower half elements should come from lower half of
3196/// V1 (and in order), and the upper half elements should come from the upper
3197/// half of V2 (and in order). And since V1 will become the source of the
3198/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003199static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3200 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003201 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003202 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003203 // Is V2 is a vector load, don't do this transformation. We will try to use
3204 // load folding shufps op.
3205 if (ISD::isNON_EXTLoad(V2))
3206 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003207
Nate Begeman5a5ca152009-04-29 05:20:52 +00003208 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003209
Evan Cheng533a0aa2006-04-19 20:35:22 +00003210 if (NumElems != 2 && NumElems != 4)
3211 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003212 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003214 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003215 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003216 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003217 return false;
3218 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003219}
3220
Evan Cheng39623da2006-04-20 08:58:49 +00003221/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3222/// all the same.
3223static bool isSplatVector(SDNode *N) {
3224 if (N->getOpcode() != ISD::BUILD_VECTOR)
3225 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003226
Dan Gohman475871a2008-07-27 21:46:04 +00003227 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003228 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3229 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003230 return false;
3231 return true;
3232}
3233
Evan Cheng213d2cf2007-05-17 18:45:50 +00003234/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003235/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003236/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003237static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003238 SDValue V1 = N->getOperand(0);
3239 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003240 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3241 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003243 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003244 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003245 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3246 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003247 if (Opc != ISD::BUILD_VECTOR ||
3248 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 return false;
3250 } else if (Idx >= 0) {
3251 unsigned Opc = V1.getOpcode();
3252 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3253 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003254 if (Opc != ISD::BUILD_VECTOR ||
3255 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003256 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003257 }
3258 }
3259 return true;
3260}
3261
3262/// getZeroVector - Returns a vector of specified type with all zero elements.
3263///
Owen Andersone50ed302009-08-10 22:56:29 +00003264static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003265 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003266 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003267
Chris Lattner8a594482007-11-25 00:24:49 +00003268 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3269 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003270 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003271 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003272 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3273 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003274 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3276 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003277 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003278 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3279 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003280 }
Dale Johannesenace16102009-02-03 19:33:06 +00003281 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003282}
3283
Chris Lattner8a594482007-11-25 00:24:49 +00003284/// getOnesVector - Returns a vector of specified type with all bits set.
3285///
Owen Andersone50ed302009-08-10 22:56:29 +00003286static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003287 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003288
Chris Lattner8a594482007-11-25 00:24:49 +00003289 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3290 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003292 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003293 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003295 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003296 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003297 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003298}
3299
3300
Evan Cheng39623da2006-04-20 08:58:49 +00003301/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3302/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003303static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003304 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003305 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003306
Evan Cheng39623da2006-04-20 08:58:49 +00003307 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003308 SmallVector<int, 8> MaskVec;
3309 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003310
Nate Begeman5a5ca152009-04-29 05:20:52 +00003311 for (unsigned i = 0; i != NumElems; ++i) {
3312 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003313 MaskVec[i] = NumElems;
3314 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003315 }
Evan Cheng39623da2006-04-20 08:58:49 +00003316 }
Evan Cheng39623da2006-04-20 08:58:49 +00003317 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003318 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3319 SVOp->getOperand(1), &MaskVec[0]);
3320 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003321}
3322
Evan Cheng017dcc62006-04-21 01:05:10 +00003323/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3324/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003325static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003326 SDValue V2) {
3327 unsigned NumElems = VT.getVectorNumElements();
3328 SmallVector<int, 8> Mask;
3329 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003330 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003331 Mask.push_back(i);
3332 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003333}
3334
Nate Begeman9008ca62009-04-27 18:41:29 +00003335/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003336static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003337 SDValue V2) {
3338 unsigned NumElems = VT.getVectorNumElements();
3339 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003340 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003341 Mask.push_back(i);
3342 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003343 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003345}
3346
Nate Begeman9008ca62009-04-27 18:41:29 +00003347/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003348static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 SDValue V2) {
3350 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003351 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003352 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003353 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003354 Mask.push_back(i + Half);
3355 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003356 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003358}
3359
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003360/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003361static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 bool HasSSE2) {
3363 if (SV->getValueType(0).getVectorNumElements() <= 4)
3364 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003365
Owen Anderson825b72b2009-08-11 20:47:22 +00003366 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003367 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003368 DebugLoc dl = SV->getDebugLoc();
3369 SDValue V1 = SV->getOperand(0);
3370 int NumElems = VT.getVectorNumElements();
3371 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003372
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 // unpack elements to the correct location
3374 while (NumElems > 4) {
3375 if (EltNo < NumElems/2) {
3376 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3377 } else {
3378 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3379 EltNo -= NumElems/2;
3380 }
3381 NumElems >>= 1;
3382 }
Eric Christopherfd179292009-08-27 18:07:15 +00003383
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 // Perform the splat.
3385 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003386 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3388 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003389}
3390
Evan Chengba05f722006-04-21 23:03:30 +00003391/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003392/// vector of zero or undef vector. This produces a shuffle where the low
3393/// element of V2 is swizzled into the zero/undef vector, landing at element
3394/// 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 +00003395static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003396 bool isZero, bool HasSSE2,
3397 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003398 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003399 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3401 unsigned NumElems = VT.getVectorNumElements();
3402 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003403 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 // If this is the insertion idx, put the low elt of V2 here.
3405 MaskVec.push_back(i == Idx ? NumElems : i);
3406 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003407}
3408
Evan Chengf26ffe92008-05-29 08:22:04 +00003409/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3410/// a shuffle that is zero.
3411static
Nate Begeman9008ca62009-04-27 18:41:29 +00003412unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3413 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003414 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003416 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003417 int Idx = SVOp->getMaskElt(Index);
3418 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003419 ++NumZeros;
3420 continue;
3421 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003423 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003424 ++NumZeros;
3425 else
3426 break;
3427 }
3428 return NumZeros;
3429}
3430
3431/// isVectorShift - Returns true if the shuffle can be implemented as a
3432/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003433/// FIXME: split into pslldqi, psrldqi, palignr variants.
3434static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003435 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003436 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003437
3438 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003440 if (!NumZeros) {
3441 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003443 if (!NumZeros)
3444 return false;
3445 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003446 bool SeenV1 = false;
3447 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 for (int i = NumZeros; i < NumElems; ++i) {
3449 int Val = isLeft ? (i - NumZeros) : i;
3450 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3451 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003452 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003454 SeenV1 = true;
3455 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003457 SeenV2 = true;
3458 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003459 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003460 return false;
3461 }
3462 if (SeenV1 && SeenV2)
3463 return false;
3464
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003466 ShAmt = NumZeros;
3467 return true;
3468}
3469
3470
Evan Chengc78d3b42006-04-24 18:01:45 +00003471/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3472///
Dan Gohman475871a2008-07-27 21:46:04 +00003473static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003474 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003475 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003476 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003477 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003478
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003479 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003480 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003481 bool First = true;
3482 for (unsigned i = 0; i < 16; ++i) {
3483 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3484 if (ThisIsNonZero && First) {
3485 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003487 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003488 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003489 First = false;
3490 }
3491
3492 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003493 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003494 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3495 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003496 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003498 }
3499 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003500 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3501 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3502 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003503 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003505 } else
3506 ThisElt = LastElt;
3507
Gabor Greifba36cb52008-08-28 21:40:38 +00003508 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003509 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003510 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003511 }
3512 }
3513
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003515}
3516
Bill Wendlinga348c562007-03-22 18:42:45 +00003517/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003518///
Dan Gohman475871a2008-07-27 21:46:04 +00003519static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003520 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003521 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003522 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003523 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003524
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003525 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003526 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003527 bool First = true;
3528 for (unsigned i = 0; i < 8; ++i) {
3529 bool isNonZero = (NonZeros & (1 << i)) != 0;
3530 if (isNonZero) {
3531 if (First) {
3532 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003534 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003536 First = false;
3537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003538 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003540 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003541 }
3542 }
3543
3544 return V;
3545}
3546
Evan Chengf26ffe92008-05-29 08:22:04 +00003547/// getVShift - Return a vector logical shift node.
3548///
Owen Andersone50ed302009-08-10 22:56:29 +00003549static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003550 unsigned NumBits, SelectionDAG &DAG,
3551 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003552 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003554 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003555 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3556 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3557 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003558 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003559}
3560
Dan Gohman475871a2008-07-27 21:46:04 +00003561SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003562X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3563 SelectionDAG &DAG) {
3564
3565 // Check if the scalar load can be widened into a vector load. And if
3566 // the address is "base + cst" see if the cst can be "absorbed" into
3567 // the shuffle mask.
3568 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3569 SDValue Ptr = LD->getBasePtr();
3570 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3571 return SDValue();
3572 EVT PVT = LD->getValueType(0);
3573 if (PVT != MVT::i32 && PVT != MVT::f32)
3574 return SDValue();
3575
3576 int FI = -1;
3577 int64_t Offset = 0;
3578 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3579 FI = FINode->getIndex();
3580 Offset = 0;
3581 } else if (Ptr.getOpcode() == ISD::ADD &&
3582 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3583 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3584 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3585 Offset = Ptr.getConstantOperandVal(1);
3586 Ptr = Ptr.getOperand(0);
3587 } else {
3588 return SDValue();
3589 }
3590
3591 SDValue Chain = LD->getChain();
3592 // Make sure the stack object alignment is at least 16.
3593 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3594 if (DAG.InferPtrAlignment(Ptr) < 16) {
3595 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003596 // Can't change the alignment. FIXME: It's possible to compute
3597 // the exact stack offset and reference FI + adjust offset instead.
3598 // If someone *really* cares about this. That's the way to implement it.
3599 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003600 } else {
3601 MFI->setObjectAlignment(FI, 16);
3602 }
3603 }
3604
3605 // (Offset % 16) must be multiple of 4. Then address is then
3606 // Ptr + (Offset & ~15).
3607 if (Offset < 0)
3608 return SDValue();
3609 if ((Offset % 16) & 3)
3610 return SDValue();
3611 int64_t StartOffset = Offset & ~15;
3612 if (StartOffset)
3613 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3614 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3615
3616 int EltNo = (Offset - StartOffset) >> 2;
3617 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3618 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003619 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3620 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003621 // Canonicalize it to a v4i32 shuffle.
3622 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3623 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3624 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3625 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3626 }
3627
3628 return SDValue();
3629}
3630
3631SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003632X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003633 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003634 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003635 if (ISD::isBuildVectorAllZeros(Op.getNode())
3636 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003637 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3638 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3639 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003640 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003641 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003642
Gabor Greifba36cb52008-08-28 21:40:38 +00003643 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003644 return getOnesVector(Op.getValueType(), DAG, dl);
3645 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003646 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003647
Owen Andersone50ed302009-08-10 22:56:29 +00003648 EVT VT = Op.getValueType();
3649 EVT ExtVT = VT.getVectorElementType();
3650 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003651
3652 unsigned NumElems = Op.getNumOperands();
3653 unsigned NumZero = 0;
3654 unsigned NumNonZero = 0;
3655 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003656 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003657 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003658 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003659 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003660 if (Elt.getOpcode() == ISD::UNDEF)
3661 continue;
3662 Values.insert(Elt);
3663 if (Elt.getOpcode() != ISD::Constant &&
3664 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003665 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003666 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003667 NumZero++;
3668 else {
3669 NonZeros |= (1 << i);
3670 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003671 }
3672 }
3673
Dan Gohman7f321562007-06-25 16:23:39 +00003674 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003675 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003676 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003677 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003678
Chris Lattner67f453a2008-03-09 05:42:06 +00003679 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003680 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003681 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003682 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003683
Chris Lattner62098042008-03-09 01:05:04 +00003684 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3685 // the value are obviously zero, truncate the value to i32 and do the
3686 // insertion that way. Only do this if the value is non-constant or if the
3687 // value is a constant being inserted into element 0. It is cheaper to do
3688 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003689 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003690 (!IsAllConstants || Idx == 0)) {
3691 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3692 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003693 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3694 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003695
Chris Lattner62098042008-03-09 01:05:04 +00003696 // Truncate the value (which may itself be a constant) to i32, and
3697 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003698 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003699 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003700 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3701 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003702
Chris Lattner62098042008-03-09 01:05:04 +00003703 // Now we have our 32-bit value zero extended in the low element of
3704 // a vector. If Idx != 0, swizzle it into place.
3705 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003706 SmallVector<int, 4> Mask;
3707 Mask.push_back(Idx);
3708 for (unsigned i = 1; i != VecElts; ++i)
3709 Mask.push_back(i);
3710 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003711 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003713 }
Dale Johannesenace16102009-02-03 19:33:06 +00003714 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003715 }
3716 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003717
Chris Lattner19f79692008-03-08 22:59:52 +00003718 // If we have a constant or non-constant insertion into the low element of
3719 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3720 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003721 // depending on what the source datatype is.
3722 if (Idx == 0) {
3723 if (NumZero == 0) {
3724 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003725 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3726 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003727 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3728 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3729 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3730 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003731 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3732 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3733 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003734 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3735 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3736 Subtarget->hasSSE2(), DAG);
3737 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3738 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003739 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003740
3741 // Is it a vector logical left shift?
3742 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003743 X86::isZeroNode(Op.getOperand(0)) &&
3744 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003745 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003746 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003747 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003748 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003749 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003751
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003752 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003753 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003754
Chris Lattner19f79692008-03-08 22:59:52 +00003755 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3756 // is a non-constant being inserted into an element other than the low one,
3757 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3758 // movd/movss) to move this into the low element, then shuffle it into
3759 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003760 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003761 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003762
Evan Cheng0db9fe62006-04-25 20:13:52 +00003763 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003764 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3765 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003766 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003767 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003768 MaskVec.push_back(i == Idx ? 0 : 1);
3769 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 }
3771 }
3772
Chris Lattner67f453a2008-03-09 05:42:06 +00003773 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003774 if (Values.size() == 1) {
3775 if (EVTBits == 32) {
3776 // Instead of a shuffle like this:
3777 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3778 // Check if it's possible to issue this instead.
3779 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3780 unsigned Idx = CountTrailingZeros_32(NonZeros);
3781 SDValue Item = Op.getOperand(Idx);
3782 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3783 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3784 }
Dan Gohman475871a2008-07-27 21:46:04 +00003785 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003786 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003787
Dan Gohmana3941172007-07-24 22:55:08 +00003788 // A vector full of immediates; various special cases are already
3789 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003790 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003791 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003792
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003793 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003794 if (EVTBits == 64) {
3795 if (NumNonZero == 1) {
3796 // One half is zero or undef.
3797 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003798 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003799 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003800 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3801 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003802 }
Dan Gohman475871a2008-07-27 21:46:04 +00003803 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003804 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003805
3806 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003807 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003808 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003809 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003810 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003811 }
3812
Bill Wendling826f36f2007-03-28 00:57:11 +00003813 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003814 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003815 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003816 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003817 }
3818
3819 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003820 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003821 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003822 if (NumElems == 4 && NumZero > 0) {
3823 for (unsigned i = 0; i < 4; ++i) {
3824 bool isZero = !(NonZeros & (1 << i));
3825 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003826 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003827 else
Dale Johannesenace16102009-02-03 19:33:06 +00003828 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003829 }
3830
3831 for (unsigned i = 0; i < 2; ++i) {
3832 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3833 default: break;
3834 case 0:
3835 V[i] = V[i*2]; // Must be a zero vector.
3836 break;
3837 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003839 break;
3840 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003841 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003842 break;
3843 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003844 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003845 break;
3846 }
3847 }
3848
Nate Begeman9008ca62009-04-27 18:41:29 +00003849 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003850 bool Reverse = (NonZeros & 0x3) == 2;
3851 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003852 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003853 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3854 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003855 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3856 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003857 }
3858
3859 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003860 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3861 // values to be inserted is equal to the number of elements, in which case
3862 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003863 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003864 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003865 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003866 getSubtarget()->hasSSE41()) {
3867 V[0] = DAG.getUNDEF(VT);
3868 for (unsigned i = 0; i < NumElems; ++i)
3869 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3870 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3871 Op.getOperand(i), DAG.getIntPtrConstant(i));
3872 return V[0];
3873 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003874 // Expand into a number of unpckl*.
3875 // e.g. for v4f32
3876 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3877 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3878 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003879 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003880 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003881 NumElems >>= 1;
3882 while (NumElems != 0) {
3883 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003885 NumElems >>= 1;
3886 }
3887 return V[0];
3888 }
3889
Dan Gohman475871a2008-07-27 21:46:04 +00003890 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003891}
3892
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003893SDValue
3894X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3895 // We support concatenate two MMX registers and place them in a MMX
3896 // register. This is better than doing a stack convert.
3897 DebugLoc dl = Op.getDebugLoc();
3898 EVT ResVT = Op.getValueType();
3899 assert(Op.getNumOperands() == 2);
3900 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3901 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3902 int Mask[2];
3903 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3904 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3905 InVec = Op.getOperand(1);
3906 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3907 unsigned NumElts = ResVT.getVectorNumElements();
3908 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3909 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3910 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3911 } else {
3912 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3913 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3914 Mask[0] = 0; Mask[1] = 2;
3915 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3916 }
3917 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3918}
3919
Nate Begemanb9a47b82009-02-23 08:49:38 +00003920// v8i16 shuffles - Prefer shuffles in the following order:
3921// 1. [all] pshuflw, pshufhw, optional move
3922// 2. [ssse3] 1 x pshufb
3923// 3. [ssse3] 2 x pshufb + 1 x por
3924// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003925static
Nate Begeman9008ca62009-04-27 18:41:29 +00003926SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3927 SelectionDAG &DAG, X86TargetLowering &TLI) {
3928 SDValue V1 = SVOp->getOperand(0);
3929 SDValue V2 = SVOp->getOperand(1);
3930 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003931 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003932
Nate Begemanb9a47b82009-02-23 08:49:38 +00003933 // Determine if more than 1 of the words in each of the low and high quadwords
3934 // of the result come from the same quadword of one of the two inputs. Undef
3935 // mask values count as coming from any quadword, for better codegen.
3936 SmallVector<unsigned, 4> LoQuad(4);
3937 SmallVector<unsigned, 4> HiQuad(4);
3938 BitVector InputQuads(4);
3939 for (unsigned i = 0; i < 8; ++i) {
3940 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003942 MaskVals.push_back(EltIdx);
3943 if (EltIdx < 0) {
3944 ++Quad[0];
3945 ++Quad[1];
3946 ++Quad[2];
3947 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003948 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003949 }
3950 ++Quad[EltIdx / 4];
3951 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003952 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003953
Nate Begemanb9a47b82009-02-23 08:49:38 +00003954 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003955 unsigned MaxQuad = 1;
3956 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003957 if (LoQuad[i] > MaxQuad) {
3958 BestLoQuad = i;
3959 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003960 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003961 }
3962
Nate Begemanb9a47b82009-02-23 08:49:38 +00003963 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003964 MaxQuad = 1;
3965 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003966 if (HiQuad[i] > MaxQuad) {
3967 BestHiQuad = i;
3968 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003969 }
3970 }
3971
Nate Begemanb9a47b82009-02-23 08:49:38 +00003972 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003973 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003974 // single pshufb instruction is necessary. If There are more than 2 input
3975 // quads, disable the next transformation since it does not help SSSE3.
3976 bool V1Used = InputQuads[0] || InputQuads[1];
3977 bool V2Used = InputQuads[2] || InputQuads[3];
3978 if (TLI.getSubtarget()->hasSSSE3()) {
3979 if (InputQuads.count() == 2 && V1Used && V2Used) {
3980 BestLoQuad = InputQuads.find_first();
3981 BestHiQuad = InputQuads.find_next(BestLoQuad);
3982 }
3983 if (InputQuads.count() > 2) {
3984 BestLoQuad = -1;
3985 BestHiQuad = -1;
3986 }
3987 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003988
Nate Begemanb9a47b82009-02-23 08:49:38 +00003989 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3990 // the shuffle mask. If a quad is scored as -1, that means that it contains
3991 // words from all 4 input quadwords.
3992 SDValue NewV;
3993 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003994 SmallVector<int, 8> MaskV;
3995 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3996 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003997 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003998 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3999 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4000 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004001
Nate Begemanb9a47b82009-02-23 08:49:38 +00004002 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4003 // source words for the shuffle, to aid later transformations.
4004 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004005 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004006 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004007 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004008 if (idx != (int)i)
4009 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004010 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004011 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004012 AllWordsInNewV = false;
4013 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004014 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004015
Nate Begemanb9a47b82009-02-23 08:49:38 +00004016 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4017 if (AllWordsInNewV) {
4018 for (int i = 0; i != 8; ++i) {
4019 int idx = MaskVals[i];
4020 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004021 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004022 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004023 if ((idx != i) && idx < 4)
4024 pshufhw = false;
4025 if ((idx != i) && idx > 3)
4026 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004027 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004028 V1 = NewV;
4029 V2Used = false;
4030 BestLoQuad = 0;
4031 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004032 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004033
Nate Begemanb9a47b82009-02-23 08:49:38 +00004034 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4035 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004036 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004037 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004038 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004039 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004040 }
Eric Christopherfd179292009-08-27 18:07:15 +00004041
Nate Begemanb9a47b82009-02-23 08:49:38 +00004042 // If we have SSSE3, and all words of the result are from 1 input vector,
4043 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4044 // is present, fall back to case 4.
4045 if (TLI.getSubtarget()->hasSSSE3()) {
4046 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004047
Nate Begemanb9a47b82009-02-23 08:49:38 +00004048 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004049 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004050 // mask, and elements that come from V1 in the V2 mask, so that the two
4051 // results can be OR'd together.
4052 bool TwoInputs = V1Used && V2Used;
4053 for (unsigned i = 0; i != 8; ++i) {
4054 int EltIdx = MaskVals[i] * 2;
4055 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004056 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4057 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004058 continue;
4059 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004060 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4061 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004062 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004063 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004064 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004065 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004067 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004069
Nate Begemanb9a47b82009-02-23 08:49:38 +00004070 // Calculate the shuffle mask for the second input, shuffle it, and
4071 // OR it with the first shuffled input.
4072 pshufbMask.clear();
4073 for (unsigned i = 0; i != 8; ++i) {
4074 int EltIdx = MaskVals[i] * 2;
4075 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004076 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4077 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004078 continue;
4079 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004080 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4081 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004082 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004084 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004085 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004086 MVT::v16i8, &pshufbMask[0], 16));
4087 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4088 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004089 }
4090
4091 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4092 // and update MaskVals with new element order.
4093 BitVector InOrder(8);
4094 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004095 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004096 for (int i = 0; i != 4; ++i) {
4097 int idx = MaskVals[i];
4098 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004099 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004100 InOrder.set(i);
4101 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004102 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004103 InOrder.set(i);
4104 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004105 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004106 }
4107 }
4108 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004109 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004111 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004112 }
Eric Christopherfd179292009-08-27 18:07:15 +00004113
Nate Begemanb9a47b82009-02-23 08:49:38 +00004114 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4115 // and update MaskVals with the new element order.
4116 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004117 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004118 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004119 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004120 for (unsigned i = 4; i != 8; ++i) {
4121 int idx = MaskVals[i];
4122 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004123 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004124 InOrder.set(i);
4125 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004126 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004127 InOrder.set(i);
4128 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004129 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004130 }
4131 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004132 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004133 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004134 }
Eric Christopherfd179292009-08-27 18:07:15 +00004135
Nate Begemanb9a47b82009-02-23 08:49:38 +00004136 // In case BestHi & BestLo were both -1, which means each quadword has a word
4137 // from each of the four input quadwords, calculate the InOrder bitvector now
4138 // before falling through to the insert/extract cleanup.
4139 if (BestLoQuad == -1 && BestHiQuad == -1) {
4140 NewV = V1;
4141 for (int i = 0; i != 8; ++i)
4142 if (MaskVals[i] < 0 || MaskVals[i] == i)
4143 InOrder.set(i);
4144 }
Eric Christopherfd179292009-08-27 18:07:15 +00004145
Nate Begemanb9a47b82009-02-23 08:49:38 +00004146 // The other elements are put in the right place using pextrw and pinsrw.
4147 for (unsigned i = 0; i != 8; ++i) {
4148 if (InOrder[i])
4149 continue;
4150 int EltIdx = MaskVals[i];
4151 if (EltIdx < 0)
4152 continue;
4153 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004155 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004157 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004158 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004159 DAG.getIntPtrConstant(i));
4160 }
4161 return NewV;
4162}
4163
4164// v16i8 shuffles - Prefer shuffles in the following order:
4165// 1. [ssse3] 1 x pshufb
4166// 2. [ssse3] 2 x pshufb + 1 x por
4167// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4168static
Nate Begeman9008ca62009-04-27 18:41:29 +00004169SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4170 SelectionDAG &DAG, X86TargetLowering &TLI) {
4171 SDValue V1 = SVOp->getOperand(0);
4172 SDValue V2 = SVOp->getOperand(1);
4173 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004174 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004175 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004176
Nate Begemanb9a47b82009-02-23 08:49:38 +00004177 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004178 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004179 // present, fall back to case 3.
4180 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4181 bool V1Only = true;
4182 bool V2Only = true;
4183 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004184 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004185 if (EltIdx < 0)
4186 continue;
4187 if (EltIdx < 16)
4188 V2Only = false;
4189 else
4190 V1Only = false;
4191 }
Eric Christopherfd179292009-08-27 18:07:15 +00004192
Nate Begemanb9a47b82009-02-23 08:49:38 +00004193 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4194 if (TLI.getSubtarget()->hasSSSE3()) {
4195 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004196
Nate Begemanb9a47b82009-02-23 08:49:38 +00004197 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004198 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004199 //
4200 // Otherwise, we have elements from both input vectors, and must zero out
4201 // elements that come from V2 in the first mask, and V1 in the second mask
4202 // so that we can OR them together.
4203 bool TwoInputs = !(V1Only || V2Only);
4204 for (unsigned i = 0; i != 16; ++i) {
4205 int EltIdx = MaskVals[i];
4206 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004207 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004208 continue;
4209 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004210 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004211 }
4212 // If all the elements are from V2, assign it to V1 and return after
4213 // building the first pshufb.
4214 if (V2Only)
4215 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004217 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004219 if (!TwoInputs)
4220 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004221
Nate Begemanb9a47b82009-02-23 08:49:38 +00004222 // Calculate the shuffle mask for the second input, shuffle it, and
4223 // OR it with the first shuffled input.
4224 pshufbMask.clear();
4225 for (unsigned i = 0; i != 16; ++i) {
4226 int EltIdx = MaskVals[i];
4227 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004229 continue;
4230 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004231 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004232 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004234 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 MVT::v16i8, &pshufbMask[0], 16));
4236 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004237 }
Eric Christopherfd179292009-08-27 18:07:15 +00004238
Nate Begemanb9a47b82009-02-23 08:49:38 +00004239 // No SSSE3 - Calculate in place words and then fix all out of place words
4240 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4241 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4243 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004244 SDValue NewV = V2Only ? V2 : V1;
4245 for (int i = 0; i != 8; ++i) {
4246 int Elt0 = MaskVals[i*2];
4247 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004248
Nate Begemanb9a47b82009-02-23 08:49:38 +00004249 // This word of the result is all undef, skip it.
4250 if (Elt0 < 0 && Elt1 < 0)
4251 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004252
Nate Begemanb9a47b82009-02-23 08:49:38 +00004253 // This word of the result is already in the correct place, skip it.
4254 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4255 continue;
4256 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4257 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004258
Nate Begemanb9a47b82009-02-23 08:49:38 +00004259 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4260 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4261 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004262
4263 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4264 // using a single extract together, load it and store it.
4265 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004266 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004267 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004268 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004269 DAG.getIntPtrConstant(i));
4270 continue;
4271 }
4272
Nate Begemanb9a47b82009-02-23 08:49:38 +00004273 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004274 // source byte is not also odd, shift the extracted word left 8 bits
4275 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004276 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004277 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004278 DAG.getIntPtrConstant(Elt1 / 2));
4279 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004280 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004281 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004282 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004283 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4284 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004285 }
4286 // If Elt0 is defined, extract it from the appropriate source. If the
4287 // source byte is not also even, shift the extracted word right 8 bits. If
4288 // Elt1 was also defined, OR the extracted values together before
4289 // inserting them in the result.
4290 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004291 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004292 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4293 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004294 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004295 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004296 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004297 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4298 DAG.getConstant(0x00FF, MVT::i16));
4299 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004300 : InsElt0;
4301 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004302 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004303 DAG.getIntPtrConstant(i));
4304 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004305 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004306}
4307
Evan Cheng7a831ce2007-12-15 03:00:47 +00004308/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4309/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4310/// done when every pair / quad of shuffle mask elements point to elements in
4311/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004312/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4313static
Nate Begeman9008ca62009-04-27 18:41:29 +00004314SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4315 SelectionDAG &DAG,
4316 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004317 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004318 SDValue V1 = SVOp->getOperand(0);
4319 SDValue V2 = SVOp->getOperand(1);
4320 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004321 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004322 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004323 EVT MaskEltVT = MaskVT.getVectorElementType();
4324 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004326 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004327 case MVT::v4f32: NewVT = MVT::v2f64; break;
4328 case MVT::v4i32: NewVT = MVT::v2i64; break;
4329 case MVT::v8i16: NewVT = MVT::v4i32; break;
4330 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004331 }
4332
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004333 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004334 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004335 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004336 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004337 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004338 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004339 int Scale = NumElems / NewWidth;
4340 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004341 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 int StartIdx = -1;
4343 for (int j = 0; j < Scale; ++j) {
4344 int EltIdx = SVOp->getMaskElt(i+j);
4345 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004346 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004347 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004348 StartIdx = EltIdx - (EltIdx % Scale);
4349 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004350 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004351 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004352 if (StartIdx == -1)
4353 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004354 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004356 }
4357
Dale Johannesenace16102009-02-03 19:33:06 +00004358 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4359 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004361}
4362
Evan Chengd880b972008-05-09 21:53:03 +00004363/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004364///
Owen Andersone50ed302009-08-10 22:56:29 +00004365static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 SDValue SrcOp, SelectionDAG &DAG,
4367 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004368 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004369 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004370 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004371 LD = dyn_cast<LoadSDNode>(SrcOp);
4372 if (!LD) {
4373 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4374 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004375 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4376 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004377 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4378 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004379 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004380 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004381 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004382 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4383 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4384 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4385 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004386 SrcOp.getOperand(0)
4387 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004388 }
4389 }
4390 }
4391
Dale Johannesenace16102009-02-03 19:33:06 +00004392 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4393 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004394 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004395 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004396}
4397
Evan Chengace3c172008-07-22 21:13:36 +00004398/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4399/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004400static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004401LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4402 SDValue V1 = SVOp->getOperand(0);
4403 SDValue V2 = SVOp->getOperand(1);
4404 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004405 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004406
Evan Chengace3c172008-07-22 21:13:36 +00004407 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004408 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 SmallVector<int, 8> Mask1(4U, -1);
4410 SmallVector<int, 8> PermMask;
4411 SVOp->getMask(PermMask);
4412
Evan Chengace3c172008-07-22 21:13:36 +00004413 unsigned NumHi = 0;
4414 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004415 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004416 int Idx = PermMask[i];
4417 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004418 Locs[i] = std::make_pair(-1, -1);
4419 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004420 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4421 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004422 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004424 NumLo++;
4425 } else {
4426 Locs[i] = std::make_pair(1, NumHi);
4427 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004429 NumHi++;
4430 }
4431 }
4432 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004433
Evan Chengace3c172008-07-22 21:13:36 +00004434 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004435 // If no more than two elements come from either vector. This can be
4436 // implemented with two shuffles. First shuffle gather the elements.
4437 // The second shuffle, which takes the first shuffle as both of its
4438 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004440
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004442
Evan Chengace3c172008-07-22 21:13:36 +00004443 for (unsigned i = 0; i != 4; ++i) {
4444 if (Locs[i].first == -1)
4445 continue;
4446 else {
4447 unsigned Idx = (i < 2) ? 0 : 4;
4448 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004450 }
4451 }
4452
Nate Begeman9008ca62009-04-27 18:41:29 +00004453 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004454 } else if (NumLo == 3 || NumHi == 3) {
4455 // Otherwise, we must have three elements from one vector, call it X, and
4456 // one element from the other, call it Y. First, use a shufps to build an
4457 // intermediate vector with the one element from Y and the element from X
4458 // that will be in the same half in the final destination (the indexes don't
4459 // matter). Then, use a shufps to build the final vector, taking the half
4460 // containing the element from Y from the intermediate, and the other half
4461 // from X.
4462 if (NumHi == 3) {
4463 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004464 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004465 std::swap(V1, V2);
4466 }
4467
4468 // Find the element from V2.
4469 unsigned HiIndex;
4470 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004471 int Val = PermMask[HiIndex];
4472 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004473 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004474 if (Val >= 4)
4475 break;
4476 }
4477
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 Mask1[0] = PermMask[HiIndex];
4479 Mask1[1] = -1;
4480 Mask1[2] = PermMask[HiIndex^1];
4481 Mask1[3] = -1;
4482 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004483
4484 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 Mask1[0] = PermMask[0];
4486 Mask1[1] = PermMask[1];
4487 Mask1[2] = HiIndex & 1 ? 6 : 4;
4488 Mask1[3] = HiIndex & 1 ? 4 : 6;
4489 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004490 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004491 Mask1[0] = HiIndex & 1 ? 2 : 0;
4492 Mask1[1] = HiIndex & 1 ? 0 : 2;
4493 Mask1[2] = PermMask[2];
4494 Mask1[3] = PermMask[3];
4495 if (Mask1[2] >= 0)
4496 Mask1[2] += 4;
4497 if (Mask1[3] >= 0)
4498 Mask1[3] += 4;
4499 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004500 }
Evan Chengace3c172008-07-22 21:13:36 +00004501 }
4502
4503 // Break it into (shuffle shuffle_hi, shuffle_lo).
4504 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004505 SmallVector<int,8> LoMask(4U, -1);
4506 SmallVector<int,8> HiMask(4U, -1);
4507
4508 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004509 unsigned MaskIdx = 0;
4510 unsigned LoIdx = 0;
4511 unsigned HiIdx = 2;
4512 for (unsigned i = 0; i != 4; ++i) {
4513 if (i == 2) {
4514 MaskPtr = &HiMask;
4515 MaskIdx = 1;
4516 LoIdx = 0;
4517 HiIdx = 2;
4518 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004519 int Idx = PermMask[i];
4520 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004521 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004522 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004523 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004524 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004525 LoIdx++;
4526 } else {
4527 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004528 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004529 HiIdx++;
4530 }
4531 }
4532
Nate Begeman9008ca62009-04-27 18:41:29 +00004533 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4534 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4535 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004536 for (unsigned i = 0; i != 4; ++i) {
4537 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004538 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004539 } else {
4540 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004541 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004542 }
4543 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004544 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004545}
4546
Dan Gohman475871a2008-07-27 21:46:04 +00004547SDValue
4548X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004549 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004550 SDValue V1 = Op.getOperand(0);
4551 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004552 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004553 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004554 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004555 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004556 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4557 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004558 bool V1IsSplat = false;
4559 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004560
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004562 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004563
Nate Begeman9008ca62009-04-27 18:41:29 +00004564 // Promote splats to v4f32.
4565 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004566 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004567 return Op;
4568 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004569 }
4570
Evan Cheng7a831ce2007-12-15 03:00:47 +00004571 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4572 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004573 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004574 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004575 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004576 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004577 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004578 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004579 // FIXME: Figure out a cleaner way to do this.
4580 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004581 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004582 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004583 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004584 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4585 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4586 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004587 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004588 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004589 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4590 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004591 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004592 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004593 }
4594 }
Eric Christopherfd179292009-08-27 18:07:15 +00004595
Nate Begeman9008ca62009-04-27 18:41:29 +00004596 if (X86::isPSHUFDMask(SVOp))
4597 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004598
Evan Chengf26ffe92008-05-29 08:22:04 +00004599 // Check if this can be converted into a logical shift.
4600 bool isLeft = false;
4601 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004602 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004603 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004604 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004605 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004606 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004607 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004608 EVT EltVT = VT.getVectorElementType();
4609 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004610 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004611 }
Eric Christopherfd179292009-08-27 18:07:15 +00004612
Nate Begeman9008ca62009-04-27 18:41:29 +00004613 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004614 if (V1IsUndef)
4615 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004616 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004617 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004618 if (!isMMX)
4619 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004620 }
Eric Christopherfd179292009-08-27 18:07:15 +00004621
Nate Begeman9008ca62009-04-27 18:41:29 +00004622 // FIXME: fold these into legal mask.
4623 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4624 X86::isMOVSLDUPMask(SVOp) ||
4625 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004626 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004627 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004628 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004629
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 if (ShouldXformToMOVHLPS(SVOp) ||
4631 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4632 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004633
Evan Chengf26ffe92008-05-29 08:22:04 +00004634 if (isShift) {
4635 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004636 EVT EltVT = VT.getVectorElementType();
4637 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004638 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004639 }
Eric Christopherfd179292009-08-27 18:07:15 +00004640
Evan Cheng9eca5e82006-10-25 21:49:50 +00004641 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004642 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4643 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004644 V1IsSplat = isSplatVector(V1.getNode());
4645 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004646
Chris Lattner8a594482007-11-25 00:24:49 +00004647 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004648 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004649 Op = CommuteVectorShuffle(SVOp, DAG);
4650 SVOp = cast<ShuffleVectorSDNode>(Op);
4651 V1 = SVOp->getOperand(0);
4652 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004653 std::swap(V1IsSplat, V2IsSplat);
4654 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004655 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004656 }
4657
Nate Begeman9008ca62009-04-27 18:41:29 +00004658 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4659 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004660 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004661 return V1;
4662 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4663 // the instruction selector will not match, so get a canonical MOVL with
4664 // swapped operands to undo the commute.
4665 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004666 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667
Nate Begeman9008ca62009-04-27 18:41:29 +00004668 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4669 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4670 X86::isUNPCKLMask(SVOp) ||
4671 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004672 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004673
Evan Cheng9bbbb982006-10-25 20:48:19 +00004674 if (V2IsSplat) {
4675 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004676 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004677 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004678 SDValue NewMask = NormalizeMask(SVOp, DAG);
4679 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4680 if (NSVOp != SVOp) {
4681 if (X86::isUNPCKLMask(NSVOp, true)) {
4682 return NewMask;
4683 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4684 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004685 }
4686 }
4687 }
4688
Evan Cheng9eca5e82006-10-25 21:49:50 +00004689 if (Commuted) {
4690 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 // FIXME: this seems wrong.
4692 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4693 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4694 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4695 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4696 X86::isUNPCKLMask(NewSVOp) ||
4697 X86::isUNPCKHMask(NewSVOp))
4698 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004699 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004700
Nate Begemanb9a47b82009-02-23 08:49:38 +00004701 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004702
4703 // Normalize the node to match x86 shuffle ops if needed
4704 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4705 return CommuteVectorShuffle(SVOp, DAG);
4706
4707 // Check for legal shuffle and return?
4708 SmallVector<int, 16> PermMask;
4709 SVOp->getMask(PermMask);
4710 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004711 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004712
Evan Cheng14b32e12007-12-11 01:46:18 +00004713 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004714 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004715 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004716 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004717 return NewOp;
4718 }
4719
Owen Anderson825b72b2009-08-11 20:47:22 +00004720 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004721 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004722 if (NewOp.getNode())
4723 return NewOp;
4724 }
Eric Christopherfd179292009-08-27 18:07:15 +00004725
Evan Chengace3c172008-07-22 21:13:36 +00004726 // Handle all 4 wide cases with a number of shuffles except for MMX.
4727 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004728 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004729
Dan Gohman475871a2008-07-27 21:46:04 +00004730 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004731}
4732
Dan Gohman475871a2008-07-27 21:46:04 +00004733SDValue
4734X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004735 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004736 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004737 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004738 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004740 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004741 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004742 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004743 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004744 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004745 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4746 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4747 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004748 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4749 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004750 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004751 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004752 Op.getOperand(0)),
4753 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004754 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004755 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004756 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004757 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004758 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004759 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004760 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4761 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004762 // result has a single use which is a store or a bitcast to i32. And in
4763 // the case of a store, it's not worth it if the index is a constant 0,
4764 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004765 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004766 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004767 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004768 if ((User->getOpcode() != ISD::STORE ||
4769 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4770 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004771 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004772 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004773 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004774 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4775 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004776 Op.getOperand(0)),
4777 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004778 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4779 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004780 // ExtractPS works with constant index.
4781 if (isa<ConstantSDNode>(Op.getOperand(1)))
4782 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004783 }
Dan Gohman475871a2008-07-27 21:46:04 +00004784 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004785}
4786
4787
Dan Gohman475871a2008-07-27 21:46:04 +00004788SDValue
4789X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004790 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004791 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004792
Evan Cheng62a3f152008-03-24 21:52:23 +00004793 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004794 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004795 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004796 return Res;
4797 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004798
Owen Andersone50ed302009-08-10 22:56:29 +00004799 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004800 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004801 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004802 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004803 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004804 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004805 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004806 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4807 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004808 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004809 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004810 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004811 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004812 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004813 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004814 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004815 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004816 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004817 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004818 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004819 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004820 if (Idx == 0)
4821 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004822
Evan Cheng0db9fe62006-04-25 20:13:52 +00004823 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004824 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004825 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004826 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004827 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004828 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004829 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004830 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004831 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4832 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4833 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004834 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004835 if (Idx == 0)
4836 return Op;
4837
4838 // UNPCKHPD the element to the lowest double word, then movsd.
4839 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4840 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004841 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004842 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004843 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004844 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004845 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004846 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004847 }
4848
Dan Gohman475871a2008-07-27 21:46:04 +00004849 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004850}
4851
Dan Gohman475871a2008-07-27 21:46:04 +00004852SDValue
4853X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004854 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004855 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004856 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004857
Dan Gohman475871a2008-07-27 21:46:04 +00004858 SDValue N0 = Op.getOperand(0);
4859 SDValue N1 = Op.getOperand(1);
4860 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004861
Dan Gohman8a55ce42009-09-23 21:02:20 +00004862 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004863 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004864 unsigned Opc;
4865 if (VT == MVT::v8i16)
4866 Opc = X86ISD::PINSRW;
4867 else if (VT == MVT::v4i16)
4868 Opc = X86ISD::MMX_PINSRW;
4869 else if (VT == MVT::v16i8)
4870 Opc = X86ISD::PINSRB;
4871 else
4872 Opc = X86ISD::PINSRB;
4873
Nate Begeman14d12ca2008-02-11 04:19:36 +00004874 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4875 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 if (N1.getValueType() != MVT::i32)
4877 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4878 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004879 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004880 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004881 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004882 // Bits [7:6] of the constant are the source select. This will always be
4883 // zero here. The DAG Combiner may combine an extract_elt index into these
4884 // bits. For example (insert (extract, 3), 2) could be matched by putting
4885 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004886 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004887 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004888 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004889 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004890 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004891 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004892 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004893 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004894 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004895 // PINSR* works with constant index.
4896 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004897 }
Dan Gohman475871a2008-07-27 21:46:04 +00004898 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004899}
4900
Dan Gohman475871a2008-07-27 21:46:04 +00004901SDValue
4902X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004903 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004904 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004905
4906 if (Subtarget->hasSSE41())
4907 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4908
Dan Gohman8a55ce42009-09-23 21:02:20 +00004909 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004910 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004911
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004912 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004913 SDValue N0 = Op.getOperand(0);
4914 SDValue N1 = Op.getOperand(1);
4915 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004916
Dan Gohman8a55ce42009-09-23 21:02:20 +00004917 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004918 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4919 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004920 if (N1.getValueType() != MVT::i32)
4921 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4922 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004923 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004924 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
4925 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004926 }
Dan Gohman475871a2008-07-27 21:46:04 +00004927 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004928}
4929
Dan Gohman475871a2008-07-27 21:46:04 +00004930SDValue
4931X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004932 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 if (Op.getValueType() == MVT::v2f32)
4934 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4935 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4936 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004937 Op.getOperand(0))));
4938
Owen Anderson825b72b2009-08-11 20:47:22 +00004939 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4940 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004941
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4943 EVT VT = MVT::v2i32;
4944 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004945 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004946 case MVT::v16i8:
4947 case MVT::v8i16:
4948 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004949 break;
4950 }
Dale Johannesenace16102009-02-03 19:33:06 +00004951 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4952 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004953}
4954
Bill Wendling056292f2008-09-16 21:48:12 +00004955// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4956// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4957// one of the above mentioned nodes. It has to be wrapped because otherwise
4958// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4959// be used to form addressing mode. These wrapped nodes will be selected
4960// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004961SDValue
4962X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004963 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004964
Chris Lattner41621a22009-06-26 19:22:52 +00004965 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4966 // global base reg.
4967 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004968 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004969 CodeModel::Model M = getTargetMachine().getCodeModel();
4970
Chris Lattner4f066492009-07-11 20:29:19 +00004971 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004972 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004973 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004974 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004975 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004976 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004977 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004978
Evan Cheng1606e8e2009-03-13 07:51:59 +00004979 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004980 CP->getAlignment(),
4981 CP->getOffset(), OpFlag);
4982 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004983 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004984 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004985 if (OpFlag) {
4986 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004987 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004988 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004989 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004990 }
4991
4992 return Result;
4993}
4994
Chris Lattner18c59872009-06-27 04:16:01 +00004995SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4996 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004997
Chris Lattner18c59872009-06-27 04:16:01 +00004998 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4999 // global base reg.
5000 unsigned char OpFlag = 0;
5001 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005002 CodeModel::Model M = getTargetMachine().getCodeModel();
5003
Chris Lattner4f066492009-07-11 20:29:19 +00005004 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005005 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005006 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005007 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005008 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005009 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005010 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005011
Chris Lattner18c59872009-06-27 04:16:01 +00005012 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5013 OpFlag);
5014 DebugLoc DL = JT->getDebugLoc();
5015 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005016
Chris Lattner18c59872009-06-27 04:16:01 +00005017 // With PIC, the address is actually $g + Offset.
5018 if (OpFlag) {
5019 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5020 DAG.getNode(X86ISD::GlobalBaseReg,
5021 DebugLoc::getUnknownLoc(), getPointerTy()),
5022 Result);
5023 }
Eric Christopherfd179292009-08-27 18:07:15 +00005024
Chris Lattner18c59872009-06-27 04:16:01 +00005025 return Result;
5026}
5027
5028SDValue
5029X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
5030 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005031
Chris Lattner18c59872009-06-27 04:16:01 +00005032 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5033 // global base reg.
5034 unsigned char OpFlag = 0;
5035 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005036 CodeModel::Model M = getTargetMachine().getCodeModel();
5037
Chris Lattner4f066492009-07-11 20:29:19 +00005038 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005039 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005040 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005041 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005042 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005043 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005044 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005045
Chris Lattner18c59872009-06-27 04:16:01 +00005046 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005047
Chris Lattner18c59872009-06-27 04:16:01 +00005048 DebugLoc DL = Op.getDebugLoc();
5049 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005050
5051
Chris Lattner18c59872009-06-27 04:16:01 +00005052 // With PIC, the address is actually $g + Offset.
5053 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005054 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005055 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5056 DAG.getNode(X86ISD::GlobalBaseReg,
5057 DebugLoc::getUnknownLoc(),
5058 getPointerTy()),
5059 Result);
5060 }
Eric Christopherfd179292009-08-27 18:07:15 +00005061
Chris Lattner18c59872009-06-27 04:16:01 +00005062 return Result;
5063}
5064
Dan Gohman475871a2008-07-27 21:46:04 +00005065SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00005066X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00005067 // Create the TargetBlockAddressAddress node.
5068 unsigned char OpFlags =
5069 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005070 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00005071 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
5072 DebugLoc dl = Op.getDebugLoc();
5073 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5074 /*isTarget=*/true, OpFlags);
5075
Dan Gohmanf705adb2009-10-30 01:28:02 +00005076 if (Subtarget->isPICStyleRIPRel() &&
5077 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005078 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5079 else
5080 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005081
Dan Gohman29cbade2009-11-20 23:18:13 +00005082 // With PIC, the address is actually $g + Offset.
5083 if (isGlobalRelativeToPICBase(OpFlags)) {
5084 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5085 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5086 Result);
5087 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005088
5089 return Result;
5090}
5091
5092SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005093X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005094 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005095 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005096 // Create the TargetGlobalAddress node, folding in the constant
5097 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005098 unsigned char OpFlags =
5099 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005100 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005101 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005102 if (OpFlags == X86II::MO_NO_FLAG &&
5103 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005104 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005105 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005106 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005107 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005108 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005109 }
Eric Christopherfd179292009-08-27 18:07:15 +00005110
Chris Lattner4f066492009-07-11 20:29:19 +00005111 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005112 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005113 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5114 else
5115 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005116
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005117 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005118 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005119 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5120 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005121 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005122 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005123
Chris Lattner36c25012009-07-10 07:34:39 +00005124 // For globals that require a load from a stub to get the address, emit the
5125 // load.
5126 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005127 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005128 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005129
Dan Gohman6520e202008-10-18 02:06:02 +00005130 // If there was a non-zero offset that we didn't fold, create an explicit
5131 // addition for it.
5132 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005133 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005134 DAG.getConstant(Offset, getPointerTy()));
5135
Evan Cheng0db9fe62006-04-25 20:13:52 +00005136 return Result;
5137}
5138
Evan Chengda43bcf2008-09-24 00:05:32 +00005139SDValue
5140X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
5141 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005142 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005143 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005144}
5145
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005146static SDValue
5147GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005148 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005149 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005150 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005151 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005152 DebugLoc dl = GA->getDebugLoc();
5153 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5154 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005155 GA->getOffset(),
5156 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005157 if (InFlag) {
5158 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005159 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005160 } else {
5161 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005162 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005163 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005164
5165 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5166 MFI->setHasCalls(true);
5167
Rafael Espindola15f1b662009-04-24 12:59:40 +00005168 SDValue Flag = Chain.getValue(1);
5169 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005170}
5171
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005172// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005173static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005174LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005175 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005176 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005177 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5178 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005179 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005180 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005181 PtrVT), InFlag);
5182 InFlag = Chain.getValue(1);
5183
Chris Lattnerb903bed2009-06-26 21:20:29 +00005184 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005185}
5186
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005187// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005188static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005189LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005190 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005191 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5192 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005193}
5194
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005195// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5196// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005197static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005198 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005199 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005200 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005201 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005202 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5203 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005204 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005205 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005206
5207 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005208 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005209
Chris Lattnerb903bed2009-06-26 21:20:29 +00005210 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005211 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5212 // initialexec.
5213 unsigned WrapperKind = X86ISD::Wrapper;
5214 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005215 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005216 } else if (is64Bit) {
5217 assert(model == TLSModel::InitialExec);
5218 OperandFlags = X86II::MO_GOTTPOFF;
5219 WrapperKind = X86ISD::WrapperRIP;
5220 } else {
5221 assert(model == TLSModel::InitialExec);
5222 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005223 }
Eric Christopherfd179292009-08-27 18:07:15 +00005224
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005225 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5226 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005227 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005228 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005229 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005230
Rafael Espindola9a580232009-02-27 13:37:18 +00005231 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005232 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005233 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005234
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005235 // The address of the thread local variable is the add of the thread
5236 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005237 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005238}
5239
Dan Gohman475871a2008-07-27 21:46:04 +00005240SDValue
5241X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005242 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005243 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005244 assert(Subtarget->isTargetELF() &&
5245 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005246 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005247 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005248
Chris Lattnerb903bed2009-06-26 21:20:29 +00005249 // If GV is an alias then use the aliasee for determining
5250 // thread-localness.
5251 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5252 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005253
Chris Lattnerb903bed2009-06-26 21:20:29 +00005254 TLSModel::Model model = getTLSModel(GV,
5255 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005256
Chris Lattnerb903bed2009-06-26 21:20:29 +00005257 switch (model) {
5258 case TLSModel::GeneralDynamic:
5259 case TLSModel::LocalDynamic: // not implemented
5260 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005261 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005262 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005263
Chris Lattnerb903bed2009-06-26 21:20:29 +00005264 case TLSModel::InitialExec:
5265 case TLSModel::LocalExec:
5266 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5267 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005268 }
Eric Christopherfd179292009-08-27 18:07:15 +00005269
Torok Edwinc23197a2009-07-14 16:55:14 +00005270 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005271 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005272}
5273
Evan Cheng0db9fe62006-04-25 20:13:52 +00005274
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005275/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005276/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005277SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005278 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005279 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005280 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005281 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005282 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005283 SDValue ShOpLo = Op.getOperand(0);
5284 SDValue ShOpHi = Op.getOperand(1);
5285 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005286 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005288 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005289
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005291 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005292 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5293 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005294 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005295 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5296 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005297 }
Evan Chenge3413162006-01-09 18:33:28 +00005298
Owen Anderson825b72b2009-08-11 20:47:22 +00005299 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5300 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005301 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005302 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005303
Dan Gohman475871a2008-07-27 21:46:04 +00005304 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005305 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005306 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5307 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005308
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005309 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005310 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5311 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005312 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005313 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5314 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005315 }
5316
Dan Gohman475871a2008-07-27 21:46:04 +00005317 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005318 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005319}
Evan Chenga3195e82006-01-12 22:54:21 +00005320
Dan Gohman475871a2008-07-27 21:46:04 +00005321SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005322 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005323
5324 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005325 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005326 return Op;
5327 }
5328 return SDValue();
5329 }
5330
Owen Anderson825b72b2009-08-11 20:47:22 +00005331 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005332 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005333
Eli Friedman36df4992009-05-27 00:47:34 +00005334 // These are really Legal; return the operand so the caller accepts it as
5335 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005336 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005337 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005339 Subtarget->is64Bit()) {
5340 return Op;
5341 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005342
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005343 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005344 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005345 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005346 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005347 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005348 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005349 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005350 PseudoSourceValue::getFixedStack(SSFI), 0,
5351 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005352 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5353}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005354
Owen Andersone50ed302009-08-10 22:56:29 +00005355SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005356 SDValue StackSlot,
5357 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005358 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005359 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005360 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005361 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005362 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005363 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005364 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005365 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005366 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005367 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005368 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005369
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005370 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005371 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005372 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005373
5374 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5375 // shouldn't be necessary except that RFP cannot be live across
5376 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005377 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005378 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005379 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005380 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005381 SDValue Ops[] = {
5382 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5383 };
5384 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005385 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005386 PseudoSourceValue::getFixedStack(SSFI), 0,
5387 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005388 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005389
Evan Cheng0db9fe62006-04-25 20:13:52 +00005390 return Result;
5391}
5392
Bill Wendling8b8a6362009-01-17 03:56:04 +00005393// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5394SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5395 // This algorithm is not obvious. Here it is in C code, more or less:
5396 /*
5397 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5398 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5399 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005400
Bill Wendling8b8a6362009-01-17 03:56:04 +00005401 // Copy ints to xmm registers.
5402 __m128i xh = _mm_cvtsi32_si128( hi );
5403 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005404
Bill Wendling8b8a6362009-01-17 03:56:04 +00005405 // Combine into low half of a single xmm register.
5406 __m128i x = _mm_unpacklo_epi32( xh, xl );
5407 __m128d d;
5408 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005409
Bill Wendling8b8a6362009-01-17 03:56:04 +00005410 // Merge in appropriate exponents to give the integer bits the right
5411 // magnitude.
5412 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005413
Bill Wendling8b8a6362009-01-17 03:56:04 +00005414 // Subtract away the biases to deal with the IEEE-754 double precision
5415 // implicit 1.
5416 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005417
Bill Wendling8b8a6362009-01-17 03:56:04 +00005418 // All conversions up to here are exact. The correctly rounded result is
5419 // calculated using the current rounding mode using the following
5420 // horizontal add.
5421 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5422 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5423 // store doesn't really need to be here (except
5424 // maybe to zero the other double)
5425 return sd;
5426 }
5427 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005428
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005429 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005430 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005431
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005432 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005433 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005434 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5435 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5436 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5437 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005438 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005439 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005440
Bill Wendling8b8a6362009-01-17 03:56:04 +00005441 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005442 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005443 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005444 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005445 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005446 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005447 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005448
Owen Anderson825b72b2009-08-11 20:47:22 +00005449 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5450 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005451 Op.getOperand(0),
5452 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005453 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5454 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005455 Op.getOperand(0),
5456 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005457 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5458 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005459 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005460 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005461 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5462 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5463 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005464 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005465 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005466 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005467
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005468 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005469 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005470 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5471 DAG.getUNDEF(MVT::v2f64), ShufMask);
5472 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5473 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005474 DAG.getIntPtrConstant(0));
5475}
5476
Bill Wendling8b8a6362009-01-17 03:56:04 +00005477// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5478SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005479 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005480 // FP constant to bias correct the final result.
5481 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005482 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005483
5484 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005485 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5486 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005487 Op.getOperand(0),
5488 DAG.getIntPtrConstant(0)));
5489
Owen Anderson825b72b2009-08-11 20:47:22 +00005490 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5491 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005492 DAG.getIntPtrConstant(0));
5493
5494 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005495 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5496 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005497 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005498 MVT::v2f64, Load)),
5499 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005500 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005501 MVT::v2f64, Bias)));
5502 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5503 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005504 DAG.getIntPtrConstant(0));
5505
5506 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005507 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005508
5509 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005510 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005511
Owen Anderson825b72b2009-08-11 20:47:22 +00005512 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005513 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005514 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005515 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005516 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005517 }
5518
5519 // Handle final rounding.
5520 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005521}
5522
5523SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005524 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005525 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005526
Evan Chenga06ec9e2009-01-19 08:08:22 +00005527 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5528 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5529 // the optimization here.
5530 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005531 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005532
Owen Andersone50ed302009-08-10 22:56:29 +00005533 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005534 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005535 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005536 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005537 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005538
Bill Wendling8b8a6362009-01-17 03:56:04 +00005539 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005540 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005541 return LowerUINT_TO_FP_i32(Op, DAG);
5542 }
5543
Owen Anderson825b72b2009-08-11 20:47:22 +00005544 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005545
5546 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005547 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005548 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5549 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5550 getPointerTy(), StackSlot, WordOff);
5551 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005552 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005553 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005554 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005555 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005556}
5557
Dan Gohman475871a2008-07-27 21:46:04 +00005558std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005559FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005560 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005561
Owen Andersone50ed302009-08-10 22:56:29 +00005562 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005563
5564 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005565 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5566 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005567 }
5568
Owen Anderson825b72b2009-08-11 20:47:22 +00005569 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5570 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005571 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005572
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005573 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005574 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005575 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005576 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005577 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005578 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005579 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005580 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005581
Evan Cheng87c89352007-10-15 20:11:21 +00005582 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5583 // stack slot.
5584 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005585 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005586 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005587 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005588
Evan Cheng0db9fe62006-04-25 20:13:52 +00005589 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005591 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005592 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5593 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5594 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005595 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005596
Dan Gohman475871a2008-07-27 21:46:04 +00005597 SDValue Chain = DAG.getEntryNode();
5598 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005599 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005600 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005601 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005602 PseudoSourceValue::getFixedStack(SSFI), 0,
5603 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005604 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005605 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005606 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5607 };
Dale Johannesenace16102009-02-03 19:33:06 +00005608 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005609 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005610 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005611 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5612 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005613
Evan Cheng0db9fe62006-04-25 20:13:52 +00005614 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005615 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005616 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005617
Chris Lattner27a6c732007-11-24 07:07:01 +00005618 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005619}
5620
Dan Gohman475871a2008-07-27 21:46:04 +00005621SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005622 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005623 if (Op.getValueType() == MVT::v2i32 &&
5624 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005625 return Op;
5626 }
5627 return SDValue();
5628 }
5629
Eli Friedman948e95a2009-05-23 09:59:16 +00005630 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005631 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005632 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5633 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005634
Chris Lattner27a6c732007-11-24 07:07:01 +00005635 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005636 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005637 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005638}
5639
Eli Friedman948e95a2009-05-23 09:59:16 +00005640SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5641 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5642 SDValue FIST = Vals.first, StackSlot = Vals.second;
5643 assert(FIST.getNode() && "Unexpected failure");
5644
5645 // Load the result.
5646 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005647 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005648}
5649
Dan Gohman475871a2008-07-27 21:46:04 +00005650SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005651 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005652 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005653 EVT VT = Op.getValueType();
5654 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005655 if (VT.isVector())
5656 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005657 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005658 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005659 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005660 CV.push_back(C);
5661 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005662 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005663 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005664 CV.push_back(C);
5665 CV.push_back(C);
5666 CV.push_back(C);
5667 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005668 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005669 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005670 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005671 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005672 PseudoSourceValue::getConstantPool(), 0,
5673 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005674 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005675}
5676
Dan Gohman475871a2008-07-27 21:46:04 +00005677SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005678 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005679 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005680 EVT VT = Op.getValueType();
5681 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005682 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005683 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005684 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005685 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005686 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005687 CV.push_back(C);
5688 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005689 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005690 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005691 CV.push_back(C);
5692 CV.push_back(C);
5693 CV.push_back(C);
5694 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005695 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005696 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005697 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005698 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005699 PseudoSourceValue::getConstantPool(), 0,
5700 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005701 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005702 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005703 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5704 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005705 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005706 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005707 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005708 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005709 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005710}
5711
Dan Gohman475871a2008-07-27 21:46:04 +00005712SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005713 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005714 SDValue Op0 = Op.getOperand(0);
5715 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005716 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005717 EVT VT = Op.getValueType();
5718 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005719
5720 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005721 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005722 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005723 SrcVT = VT;
5724 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005725 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005726 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005727 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005728 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005729 }
5730
5731 // At this point the operands and the result should have the same
5732 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005733
Evan Cheng68c47cb2007-01-05 07:55:56 +00005734 // First get the sign bit of second operand.
5735 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005736 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005737 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5738 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005739 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005740 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5741 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5742 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5743 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005744 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005745 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005746 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005747 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005748 PseudoSourceValue::getConstantPool(), 0,
5749 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005750 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005751
5752 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005753 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005754 // Op0 is MVT::f32, Op1 is MVT::f64.
5755 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5756 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5757 DAG.getConstant(32, MVT::i32));
5758 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5759 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005760 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005761 }
5762
Evan Cheng73d6cf12007-01-05 21:37:56 +00005763 // Clear first operand sign bit.
5764 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005765 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005766 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5767 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005768 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005769 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5770 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5771 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5772 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005773 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005774 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005775 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005776 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005777 PseudoSourceValue::getConstantPool(), 0,
5778 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005779 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005780
5781 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005782 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005783}
5784
Dan Gohman076aee32009-03-04 19:44:21 +00005785/// Emit nodes that will be selected as "test Op0,Op0", or something
5786/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005787SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5788 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005789 DebugLoc dl = Op.getDebugLoc();
5790
Dan Gohman31125812009-03-07 01:58:32 +00005791 // CF and OF aren't always set the way we want. Determine which
5792 // of these we need.
5793 bool NeedCF = false;
5794 bool NeedOF = false;
5795 switch (X86CC) {
5796 case X86::COND_A: case X86::COND_AE:
5797 case X86::COND_B: case X86::COND_BE:
5798 NeedCF = true;
5799 break;
5800 case X86::COND_G: case X86::COND_GE:
5801 case X86::COND_L: case X86::COND_LE:
5802 case X86::COND_O: case X86::COND_NO:
5803 NeedOF = true;
5804 break;
5805 default: break;
5806 }
5807
Dan Gohman076aee32009-03-04 19:44:21 +00005808 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005809 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5810 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5811 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005812 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005813 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005814 switch (Op.getNode()->getOpcode()) {
5815 case ISD::ADD:
5816 // Due to an isel shortcoming, be conservative if this add is likely to
5817 // be selected as part of a load-modify-store instruction. When the root
5818 // node in a match is a store, isel doesn't know how to remap non-chain
5819 // non-flag uses of other nodes in the match, such as the ADD in this
5820 // case. This leads to the ADD being left around and reselected, with
5821 // the result being two adds in the output.
5822 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5823 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5824 if (UI->getOpcode() == ISD::STORE)
5825 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005826 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005827 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5828 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005829 if (C->getAPIntValue() == 1) {
5830 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005831 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005832 break;
5833 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005834 // An add of negative one (subtract of one) will be selected as a DEC.
5835 if (C->getAPIntValue().isAllOnesValue()) {
5836 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005837 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005838 break;
5839 }
5840 }
Dan Gohman076aee32009-03-04 19:44:21 +00005841 // Otherwise use a regular EFLAGS-setting add.
5842 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005843 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005844 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005845 case ISD::AND: {
5846 // If the primary and result isn't used, don't bother using X86ISD::AND,
5847 // because a TEST instruction will be better.
5848 bool NonFlagUse = false;
5849 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005850 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5851 SDNode *User = *UI;
5852 unsigned UOpNo = UI.getOperandNo();
5853 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5854 // Look pass truncate.
5855 UOpNo = User->use_begin().getOperandNo();
5856 User = *User->use_begin();
5857 }
5858 if (User->getOpcode() != ISD::BRCOND &&
5859 User->getOpcode() != ISD::SETCC &&
5860 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005861 NonFlagUse = true;
5862 break;
5863 }
Evan Cheng17751da2010-01-07 00:54:06 +00005864 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005865 if (!NonFlagUse)
5866 break;
5867 }
5868 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005869 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005870 case ISD::OR:
5871 case ISD::XOR:
5872 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005873 // likely to be selected as part of a load-modify-store instruction.
5874 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5875 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5876 if (UI->getOpcode() == ISD::STORE)
5877 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005878 // Otherwise use a regular EFLAGS-setting instruction.
5879 switch (Op.getNode()->getOpcode()) {
5880 case ISD::SUB: Opcode = X86ISD::SUB; break;
5881 case ISD::OR: Opcode = X86ISD::OR; break;
5882 case ISD::XOR: Opcode = X86ISD::XOR; break;
5883 case ISD::AND: Opcode = X86ISD::AND; break;
5884 default: llvm_unreachable("unexpected operator!");
5885 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005886 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005887 break;
5888 case X86ISD::ADD:
5889 case X86ISD::SUB:
5890 case X86ISD::INC:
5891 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005892 case X86ISD::OR:
5893 case X86ISD::XOR:
5894 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005895 return SDValue(Op.getNode(), 1);
5896 default:
5897 default_case:
5898 break;
5899 }
5900 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005901 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005902 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005903 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005904 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005905 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005906 DAG.ReplaceAllUsesWith(Op, New);
5907 return SDValue(New.getNode(), 1);
5908 }
5909 }
5910
5911 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005912 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005913 DAG.getConstant(0, Op.getValueType()));
5914}
5915
5916/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5917/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005918SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5919 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005920 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5921 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005922 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005923
5924 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005925 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005926}
5927
Evan Chengd40d03e2010-01-06 19:38:29 +00005928/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5929/// if it's possible.
Evan Cheng2c755ba2010-02-27 07:36:59 +00005930static SDValue LowerToBT(SDValue And, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005931 DebugLoc dl, SelectionDAG &DAG) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00005932 SDValue Op0 = And.getOperand(0);
5933 SDValue Op1 = And.getOperand(1);
5934 if (Op0.getOpcode() == ISD::TRUNCATE)
5935 Op0 = Op0.getOperand(0);
5936 if (Op1.getOpcode() == ISD::TRUNCATE)
5937 Op1 = Op1.getOperand(0);
5938
Evan Chengd40d03e2010-01-06 19:38:29 +00005939 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00005940 if (Op1.getOpcode() == ISD::SHL) {
5941 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
5942 if (And10C->getZExtValue() == 1) {
5943 LHS = Op0;
5944 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005945 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00005946 } else if (Op0.getOpcode() == ISD::SHL) {
5947 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
5948 if (And00C->getZExtValue() == 1) {
5949 LHS = Op1;
5950 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00005951 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00005952 } else if (Op1.getOpcode() == ISD::Constant) {
5953 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
5954 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00005955 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5956 LHS = AndLHS.getOperand(0);
5957 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005958 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005959 }
Evan Cheng0488db92007-09-25 01:57:46 +00005960
Evan Chengd40d03e2010-01-06 19:38:29 +00005961 if (LHS.getNode()) {
5962 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5963 // instruction. Since the shift amount is in-range-or-undefined, we know
5964 // that doing a bittest on the i16 value is ok. We extend to i32 because
5965 // the encoding for the i16 version is larger than the i32 version.
5966 if (LHS.getValueType() == MVT::i8)
5967 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005968
Evan Chengd40d03e2010-01-06 19:38:29 +00005969 // If the operand types disagree, extend the shift amount to match. Since
5970 // BT ignores high bits (like shifts) we can use anyextend.
5971 if (LHS.getValueType() != RHS.getValueType())
5972 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005973
Evan Chengd40d03e2010-01-06 19:38:29 +00005974 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5975 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5976 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5977 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005978 }
5979
Evan Cheng54de3ea2010-01-05 06:52:31 +00005980 return SDValue();
5981}
5982
5983SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5984 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5985 SDValue Op0 = Op.getOperand(0);
5986 SDValue Op1 = Op.getOperand(1);
5987 DebugLoc dl = Op.getDebugLoc();
5988 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5989
5990 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005991 // Lower (X & (1 << N)) == 0 to BT(X, N).
5992 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5993 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5994 if (Op0.getOpcode() == ISD::AND &&
5995 Op0.hasOneUse() &&
5996 Op1.getOpcode() == ISD::Constant &&
5997 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5998 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5999 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6000 if (NewSetCC.getNode())
6001 return NewSetCC;
6002 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006003
Evan Cheng2c755ba2010-02-27 07:36:59 +00006004 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6005 if (Op0.getOpcode() == X86ISD::SETCC &&
6006 Op1.getOpcode() == ISD::Constant &&
6007 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6008 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6009 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6010 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6011 bool Invert = (CC == ISD::SETNE) ^
6012 cast<ConstantSDNode>(Op1)->isNullValue();
6013 if (Invert)
6014 CCode = X86::GetOppositeBranchCondition(CCode);
6015 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6016 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6017 }
6018
Chris Lattnere55484e2008-12-25 05:34:37 +00006019 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
6020 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006021 if (X86CC == X86::COND_INVALID)
6022 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006023
Dan Gohman31125812009-03-07 01:58:32 +00006024 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006025
6026 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006027 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006028 return DAG.getNode(ISD::AND, dl, MVT::i8,
6029 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6030 DAG.getConstant(X86CC, MVT::i8), Cond),
6031 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006032
Owen Anderson825b72b2009-08-11 20:47:22 +00006033 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6034 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006035}
6036
Dan Gohman475871a2008-07-27 21:46:04 +00006037SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
6038 SDValue Cond;
6039 SDValue Op0 = Op.getOperand(0);
6040 SDValue Op1 = Op.getOperand(1);
6041 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006042 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006043 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6044 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006045 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006046
6047 if (isFP) {
6048 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006049 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6051 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006052 bool Swap = false;
6053
6054 switch (SetCCOpcode) {
6055 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006056 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006057 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006058 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006059 case ISD::SETGT: Swap = true; // Fallthrough
6060 case ISD::SETLT:
6061 case ISD::SETOLT: SSECC = 1; break;
6062 case ISD::SETOGE:
6063 case ISD::SETGE: Swap = true; // Fallthrough
6064 case ISD::SETLE:
6065 case ISD::SETOLE: SSECC = 2; break;
6066 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006067 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006068 case ISD::SETNE: SSECC = 4; break;
6069 case ISD::SETULE: Swap = true;
6070 case ISD::SETUGE: SSECC = 5; break;
6071 case ISD::SETULT: Swap = true;
6072 case ISD::SETUGT: SSECC = 6; break;
6073 case ISD::SETO: SSECC = 7; break;
6074 }
6075 if (Swap)
6076 std::swap(Op0, Op1);
6077
Nate Begemanfb8ead02008-07-25 19:05:58 +00006078 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006079 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006080 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006081 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006082 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6083 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006084 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006085 }
6086 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006087 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006088 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6089 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006090 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006091 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006092 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006093 }
6094 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006095 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006097
Nate Begeman30a0de92008-07-17 16:51:19 +00006098 // We are handling one of the integer comparisons here. Since SSE only has
6099 // GT and EQ comparisons for integer, swapping operands and multiple
6100 // operations may be required for some comparisons.
6101 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6102 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006103
Owen Anderson825b72b2009-08-11 20:47:22 +00006104 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006105 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006106 case MVT::v8i8:
6107 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6108 case MVT::v4i16:
6109 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6110 case MVT::v2i32:
6111 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6112 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006114
Nate Begeman30a0de92008-07-17 16:51:19 +00006115 switch (SetCCOpcode) {
6116 default: break;
6117 case ISD::SETNE: Invert = true;
6118 case ISD::SETEQ: Opc = EQOpc; break;
6119 case ISD::SETLT: Swap = true;
6120 case ISD::SETGT: Opc = GTOpc; break;
6121 case ISD::SETGE: Swap = true;
6122 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6123 case ISD::SETULT: Swap = true;
6124 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6125 case ISD::SETUGE: Swap = true;
6126 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6127 }
6128 if (Swap)
6129 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006130
Nate Begeman30a0de92008-07-17 16:51:19 +00006131 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6132 // bits of the inputs before performing those operations.
6133 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006134 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006135 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6136 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006137 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006138 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6139 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006140 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6141 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006143
Dale Johannesenace16102009-02-03 19:33:06 +00006144 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006145
6146 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006147 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006148 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006149
Nate Begeman30a0de92008-07-17 16:51:19 +00006150 return Result;
6151}
Evan Cheng0488db92007-09-25 01:57:46 +00006152
Evan Cheng370e5342008-12-03 08:38:43 +00006153// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006154static bool isX86LogicalCmp(SDValue Op) {
6155 unsigned Opc = Op.getNode()->getOpcode();
6156 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6157 return true;
6158 if (Op.getResNo() == 1 &&
6159 (Opc == X86ISD::ADD ||
6160 Opc == X86ISD::SUB ||
6161 Opc == X86ISD::SMUL ||
6162 Opc == X86ISD::UMUL ||
6163 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006164 Opc == X86ISD::DEC ||
6165 Opc == X86ISD::OR ||
6166 Opc == X86ISD::XOR ||
6167 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006168 return true;
6169
6170 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006171}
6172
Dan Gohman475871a2008-07-27 21:46:04 +00006173SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006174 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006175 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006176 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006177 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006178
Dan Gohman1a492952009-10-20 16:22:37 +00006179 if (Cond.getOpcode() == ISD::SETCC) {
6180 SDValue NewCond = LowerSETCC(Cond, DAG);
6181 if (NewCond.getNode())
6182 Cond = NewCond;
6183 }
Evan Cheng734503b2006-09-11 02:19:56 +00006184
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006185 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6186 SDValue Op1 = Op.getOperand(1);
6187 SDValue Op2 = Op.getOperand(2);
6188 if (Cond.getOpcode() == X86ISD::SETCC &&
6189 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6190 SDValue Cmp = Cond.getOperand(1);
6191 if (Cmp.getOpcode() == X86ISD::CMP) {
6192 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6193 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6194 ConstantSDNode *RHSC =
6195 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6196 if (N1C && N1C->isAllOnesValue() &&
6197 N2C && N2C->isNullValue() &&
6198 RHSC && RHSC->isNullValue()) {
6199 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006200 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006201 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6202 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6203 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6204 }
6205 }
6206 }
6207
Evan Chengad9c0a32009-12-15 00:53:42 +00006208 // Look pass (and (setcc_carry (cmp ...)), 1).
6209 if (Cond.getOpcode() == ISD::AND &&
6210 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6211 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6212 if (C && C->getAPIntValue() == 1)
6213 Cond = Cond.getOperand(0);
6214 }
6215
Evan Cheng3f41d662007-10-08 22:16:29 +00006216 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6217 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006218 if (Cond.getOpcode() == X86ISD::SETCC ||
6219 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006220 CC = Cond.getOperand(0);
6221
Dan Gohman475871a2008-07-27 21:46:04 +00006222 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006223 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006224 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006225
Evan Cheng3f41d662007-10-08 22:16:29 +00006226 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006227 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006228 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006229 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006230
Chris Lattnerd1980a52009-03-12 06:52:53 +00006231 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6232 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006233 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006234 addTest = false;
6235 }
6236 }
6237
6238 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006239 // Look pass the truncate.
6240 if (Cond.getOpcode() == ISD::TRUNCATE)
6241 Cond = Cond.getOperand(0);
6242
6243 // We know the result of AND is compared against zero. Try to match
6244 // it to BT.
6245 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6246 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6247 if (NewSetCC.getNode()) {
6248 CC = NewSetCC.getOperand(0);
6249 Cond = NewSetCC.getOperand(1);
6250 addTest = false;
6251 }
6252 }
6253 }
6254
6255 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006256 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006257 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006258 }
6259
Evan Cheng0488db92007-09-25 01:57:46 +00006260 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6261 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006262 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6263 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006264 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006265}
6266
Evan Cheng370e5342008-12-03 08:38:43 +00006267// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6268// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6269// from the AND / OR.
6270static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6271 Opc = Op.getOpcode();
6272 if (Opc != ISD::OR && Opc != ISD::AND)
6273 return false;
6274 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6275 Op.getOperand(0).hasOneUse() &&
6276 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6277 Op.getOperand(1).hasOneUse());
6278}
6279
Evan Cheng961d6d42009-02-02 08:19:07 +00006280// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6281// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006282static bool isXor1OfSetCC(SDValue Op) {
6283 if (Op.getOpcode() != ISD::XOR)
6284 return false;
6285 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6286 if (N1C && N1C->getAPIntValue() == 1) {
6287 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6288 Op.getOperand(0).hasOneUse();
6289 }
6290 return false;
6291}
6292
Dan Gohman475871a2008-07-27 21:46:04 +00006293SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006294 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006295 SDValue Chain = Op.getOperand(0);
6296 SDValue Cond = Op.getOperand(1);
6297 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006298 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006299 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006300
Dan Gohman1a492952009-10-20 16:22:37 +00006301 if (Cond.getOpcode() == ISD::SETCC) {
6302 SDValue NewCond = LowerSETCC(Cond, DAG);
6303 if (NewCond.getNode())
6304 Cond = NewCond;
6305 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006306#if 0
6307 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006308 else if (Cond.getOpcode() == X86ISD::ADD ||
6309 Cond.getOpcode() == X86ISD::SUB ||
6310 Cond.getOpcode() == X86ISD::SMUL ||
6311 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006312 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006313#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006314
Evan Chengad9c0a32009-12-15 00:53:42 +00006315 // Look pass (and (setcc_carry (cmp ...)), 1).
6316 if (Cond.getOpcode() == ISD::AND &&
6317 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6318 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6319 if (C && C->getAPIntValue() == 1)
6320 Cond = Cond.getOperand(0);
6321 }
6322
Evan Cheng3f41d662007-10-08 22:16:29 +00006323 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6324 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006325 if (Cond.getOpcode() == X86ISD::SETCC ||
6326 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006327 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006328
Dan Gohman475871a2008-07-27 21:46:04 +00006329 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006330 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006331 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006332 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006333 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006334 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006335 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006336 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006337 default: break;
6338 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006339 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006340 // These can only come from an arithmetic instruction with overflow,
6341 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006342 Cond = Cond.getNode()->getOperand(1);
6343 addTest = false;
6344 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006345 }
Evan Cheng0488db92007-09-25 01:57:46 +00006346 }
Evan Cheng370e5342008-12-03 08:38:43 +00006347 } else {
6348 unsigned CondOpc;
6349 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6350 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006351 if (CondOpc == ISD::OR) {
6352 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6353 // two branches instead of an explicit OR instruction with a
6354 // separate test.
6355 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006356 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006357 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006358 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006359 Chain, Dest, CC, Cmp);
6360 CC = Cond.getOperand(1).getOperand(0);
6361 Cond = Cmp;
6362 addTest = false;
6363 }
6364 } else { // ISD::AND
6365 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6366 // two branches instead of an explicit AND instruction with a
6367 // separate test. However, we only do this if this block doesn't
6368 // have a fall-through edge, because this requires an explicit
6369 // jmp when the condition is false.
6370 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006371 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006372 Op.getNode()->hasOneUse()) {
6373 X86::CondCode CCode =
6374 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6375 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006376 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006377 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6378 // Look for an unconditional branch following this conditional branch.
6379 // We need this because we need to reverse the successors in order
6380 // to implement FCMP_OEQ.
6381 if (User.getOpcode() == ISD::BR) {
6382 SDValue FalseBB = User.getOperand(1);
6383 SDValue NewBR =
6384 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6385 assert(NewBR == User);
6386 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006387
Dale Johannesene4d209d2009-02-03 20:21:25 +00006388 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006389 Chain, Dest, CC, Cmp);
6390 X86::CondCode CCode =
6391 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6392 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006393 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006394 Cond = Cmp;
6395 addTest = false;
6396 }
6397 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006398 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006399 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6400 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6401 // It should be transformed during dag combiner except when the condition
6402 // is set by a arithmetics with overflow node.
6403 X86::CondCode CCode =
6404 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6405 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006406 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006407 Cond = Cond.getOperand(0).getOperand(1);
6408 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006409 }
Evan Cheng0488db92007-09-25 01:57:46 +00006410 }
6411
6412 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006413 // Look pass the truncate.
6414 if (Cond.getOpcode() == ISD::TRUNCATE)
6415 Cond = Cond.getOperand(0);
6416
6417 // We know the result of AND is compared against zero. Try to match
6418 // it to BT.
6419 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6420 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6421 if (NewSetCC.getNode()) {
6422 CC = NewSetCC.getOperand(0);
6423 Cond = NewSetCC.getOperand(1);
6424 addTest = false;
6425 }
6426 }
6427 }
6428
6429 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006431 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006432 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006433 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006434 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006435}
6436
Anton Korobeynikove060b532007-04-17 19:34:00 +00006437
6438// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6439// Calls to _alloca is needed to probe the stack when allocating more than 4k
6440// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6441// that the guard pages used by the OS virtual memory manager are allocated in
6442// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006443SDValue
6444X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006445 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006446 assert(Subtarget->isTargetCygMing() &&
6447 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006448 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006449
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006450 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006451 SDValue Chain = Op.getOperand(0);
6452 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006453 // FIXME: Ensure alignment here
6454
Dan Gohman475871a2008-07-27 21:46:04 +00006455 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006456
Owen Andersone50ed302009-08-10 22:56:29 +00006457 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006458 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006459
Dale Johannesendd64c412009-02-04 00:33:20 +00006460 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006461 Flag = Chain.getValue(1);
6462
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006463 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006464
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006465 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6466 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006467
Dale Johannesendd64c412009-02-04 00:33:20 +00006468 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006469
Dan Gohman475871a2008-07-27 21:46:04 +00006470 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006471 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006472}
6473
Dan Gohman475871a2008-07-27 21:46:04 +00006474SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006475X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006476 SDValue Chain,
6477 SDValue Dst, SDValue Src,
6478 SDValue Size, unsigned Align,
6479 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006480 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006481 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006482
Bill Wendling6f287b22008-09-30 21:22:07 +00006483 // If not DWORD aligned or size is more than the threshold, call the library.
6484 // The libc version is likely to be faster for these cases. It can use the
6485 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006486 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006487 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006488 ConstantSize->getZExtValue() >
6489 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006490 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006491
6492 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006493 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006494
Bill Wendling6158d842008-10-01 00:59:58 +00006495 if (const char *bzeroEntry = V &&
6496 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006497 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006498 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006499 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006500 TargetLowering::ArgListEntry Entry;
6501 Entry.Node = Dst;
6502 Entry.Ty = IntPtrTy;
6503 Args.push_back(Entry);
6504 Entry.Node = Size;
6505 Args.push_back(Entry);
6506 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006507 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6508 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006509 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006510 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006511 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006512 }
6513
Dan Gohman707e0182008-04-12 04:36:06 +00006514 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006515 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006516 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006517
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006518 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006519 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006520 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006521 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006522 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006523 unsigned BytesLeft = 0;
6524 bool TwoRepStos = false;
6525 if (ValC) {
6526 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006527 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006528
Evan Cheng0db9fe62006-04-25 20:13:52 +00006529 // If the value is a constant, then we can potentially use larger sets.
6530 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006531 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006532 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006533 ValReg = X86::AX;
6534 Val = (Val << 8) | Val;
6535 break;
6536 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006537 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006538 ValReg = X86::EAX;
6539 Val = (Val << 8) | Val;
6540 Val = (Val << 16) | Val;
6541 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006542 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006543 ValReg = X86::RAX;
6544 Val = (Val << 32) | Val;
6545 }
6546 break;
6547 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006548 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006549 ValReg = X86::AL;
6550 Count = DAG.getIntPtrConstant(SizeVal);
6551 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006552 }
6553
Owen Anderson825b72b2009-08-11 20:47:22 +00006554 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006555 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006556 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6557 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006558 }
6559
Dale Johannesen0f502f62009-02-03 22:26:09 +00006560 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006561 InFlag);
6562 InFlag = Chain.getValue(1);
6563 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006564 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006565 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006566 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006567 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006568 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006569
Scott Michelfdc40a02009-02-17 22:15:04 +00006570 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006571 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006572 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006573 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006574 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006575 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006576 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006577 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006578
Owen Anderson825b72b2009-08-11 20:47:22 +00006579 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006580 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6581 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006582
Evan Cheng0db9fe62006-04-25 20:13:52 +00006583 if (TwoRepStos) {
6584 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006585 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006586 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006587 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006588 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6589 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006590 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006591 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006592 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006593 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006594 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6595 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006596 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006597 // Handle the last 1 - 7 bytes.
6598 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006599 EVT AddrVT = Dst.getValueType();
6600 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006601
Dale Johannesen0f502f62009-02-03 22:26:09 +00006602 Chain = DAG.getMemset(Chain, dl,
6603 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006604 DAG.getConstant(Offset, AddrVT)),
6605 Src,
6606 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006607 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006608 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006609
Dan Gohman707e0182008-04-12 04:36:06 +00006610 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006611 return Chain;
6612}
Evan Cheng11e15b32006-04-03 20:53:28 +00006613
Dan Gohman475871a2008-07-27 21:46:04 +00006614SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006615X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006616 SDValue Chain, SDValue Dst, SDValue Src,
6617 SDValue Size, unsigned Align,
6618 bool AlwaysInline,
6619 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006620 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006621 // This requires the copy size to be a constant, preferrably
6622 // within a subtarget-specific limit.
6623 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6624 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006625 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006626 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006627 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006628 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006629
Evan Cheng1887c1c2008-08-21 21:00:15 +00006630 /// If not DWORD aligned, call the library.
6631 if ((Align & 3) != 0)
6632 return SDValue();
6633
6634 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006635 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006636 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006637 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006638
Duncan Sands83ec4b62008-06-06 12:08:01 +00006639 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006640 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006641 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006642 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006643
Dan Gohman475871a2008-07-27 21:46:04 +00006644 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006645 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006646 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006647 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006648 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006649 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006650 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006651 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006652 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006653 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006654 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006655 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006656 InFlag = Chain.getValue(1);
6657
Owen Anderson825b72b2009-08-11 20:47:22 +00006658 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006659 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6660 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6661 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006662
Dan Gohman475871a2008-07-27 21:46:04 +00006663 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006664 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006665 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006666 // Handle the last 1 - 7 bytes.
6667 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006668 EVT DstVT = Dst.getValueType();
6669 EVT SrcVT = Src.getValueType();
6670 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006671 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006672 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006673 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006674 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006675 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006676 DAG.getConstant(BytesLeft, SizeVT),
6677 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006678 DstSV, DstSVOff + Offset,
6679 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006680 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006681
Owen Anderson825b72b2009-08-11 20:47:22 +00006682 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006683 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006684}
6685
Dan Gohman475871a2008-07-27 21:46:04 +00006686SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006687 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006688 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006689
Evan Cheng25ab6902006-09-08 06:48:29 +00006690 if (!Subtarget->is64Bit()) {
6691 // vastart just stores the address of the VarArgsFrameIndex slot into the
6692 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006693 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006694 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6695 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006696 }
6697
6698 // __va_list_tag:
6699 // gp_offset (0 - 6 * 8)
6700 // fp_offset (48 - 48 + 8 * 16)
6701 // overflow_arg_area (point to parameters coming in memory).
6702 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006703 SmallVector<SDValue, 8> MemOps;
6704 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006705 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006706 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
David Greene67c9d422010-02-15 16:53:33 +00006707 DAG.getConstant(VarArgsGPOffset, MVT::i32),
6708 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006709 MemOps.push_back(Store);
6710
6711 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006712 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006713 FIN, DAG.getIntPtrConstant(4));
6714 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006715 DAG.getConstant(VarArgsFPOffset, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006716 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006717 MemOps.push_back(Store);
6718
6719 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006720 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006721 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006722 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006723 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6724 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006725 MemOps.push_back(Store);
6726
6727 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006728 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006729 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006730 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006731 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6732 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006733 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006734 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006735 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006736}
6737
Dan Gohman475871a2008-07-27 21:46:04 +00006738SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006739 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6740 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006741 SDValue Chain = Op.getOperand(0);
6742 SDValue SrcPtr = Op.getOperand(1);
6743 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006744
Torok Edwindac237e2009-07-08 20:53:28 +00006745 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006746 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006747}
6748
Dan Gohman475871a2008-07-27 21:46:04 +00006749SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006750 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006751 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006752 SDValue Chain = Op.getOperand(0);
6753 SDValue DstPtr = Op.getOperand(1);
6754 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006755 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6756 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006757 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006758
Dale Johannesendd64c412009-02-04 00:33:20 +00006759 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006760 DAG.getIntPtrConstant(24), 8, false,
6761 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006762}
6763
Dan Gohman475871a2008-07-27 21:46:04 +00006764SDValue
6765X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006766 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006767 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006768 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006769 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006770 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006771 case Intrinsic::x86_sse_comieq_ss:
6772 case Intrinsic::x86_sse_comilt_ss:
6773 case Intrinsic::x86_sse_comile_ss:
6774 case Intrinsic::x86_sse_comigt_ss:
6775 case Intrinsic::x86_sse_comige_ss:
6776 case Intrinsic::x86_sse_comineq_ss:
6777 case Intrinsic::x86_sse_ucomieq_ss:
6778 case Intrinsic::x86_sse_ucomilt_ss:
6779 case Intrinsic::x86_sse_ucomile_ss:
6780 case Intrinsic::x86_sse_ucomigt_ss:
6781 case Intrinsic::x86_sse_ucomige_ss:
6782 case Intrinsic::x86_sse_ucomineq_ss:
6783 case Intrinsic::x86_sse2_comieq_sd:
6784 case Intrinsic::x86_sse2_comilt_sd:
6785 case Intrinsic::x86_sse2_comile_sd:
6786 case Intrinsic::x86_sse2_comigt_sd:
6787 case Intrinsic::x86_sse2_comige_sd:
6788 case Intrinsic::x86_sse2_comineq_sd:
6789 case Intrinsic::x86_sse2_ucomieq_sd:
6790 case Intrinsic::x86_sse2_ucomilt_sd:
6791 case Intrinsic::x86_sse2_ucomile_sd:
6792 case Intrinsic::x86_sse2_ucomigt_sd:
6793 case Intrinsic::x86_sse2_ucomige_sd:
6794 case Intrinsic::x86_sse2_ucomineq_sd: {
6795 unsigned Opc = 0;
6796 ISD::CondCode CC = ISD::SETCC_INVALID;
6797 switch (IntNo) {
6798 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006799 case Intrinsic::x86_sse_comieq_ss:
6800 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801 Opc = X86ISD::COMI;
6802 CC = ISD::SETEQ;
6803 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006804 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006805 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006806 Opc = X86ISD::COMI;
6807 CC = ISD::SETLT;
6808 break;
6809 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006810 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006811 Opc = X86ISD::COMI;
6812 CC = ISD::SETLE;
6813 break;
6814 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006815 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006816 Opc = X86ISD::COMI;
6817 CC = ISD::SETGT;
6818 break;
6819 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006820 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006821 Opc = X86ISD::COMI;
6822 CC = ISD::SETGE;
6823 break;
6824 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006825 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006826 Opc = X86ISD::COMI;
6827 CC = ISD::SETNE;
6828 break;
6829 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006830 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006831 Opc = X86ISD::UCOMI;
6832 CC = ISD::SETEQ;
6833 break;
6834 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006835 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006836 Opc = X86ISD::UCOMI;
6837 CC = ISD::SETLT;
6838 break;
6839 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006840 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006841 Opc = X86ISD::UCOMI;
6842 CC = ISD::SETLE;
6843 break;
6844 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006845 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006846 Opc = X86ISD::UCOMI;
6847 CC = ISD::SETGT;
6848 break;
6849 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006850 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006851 Opc = X86ISD::UCOMI;
6852 CC = ISD::SETGE;
6853 break;
6854 case Intrinsic::x86_sse_ucomineq_ss:
6855 case Intrinsic::x86_sse2_ucomineq_sd:
6856 Opc = X86ISD::UCOMI;
6857 CC = ISD::SETNE;
6858 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006859 }
Evan Cheng734503b2006-09-11 02:19:56 +00006860
Dan Gohman475871a2008-07-27 21:46:04 +00006861 SDValue LHS = Op.getOperand(1);
6862 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006863 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006864 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006865 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6866 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6867 DAG.getConstant(X86CC, MVT::i8), Cond);
6868 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006869 }
Eric Christopher71c67532009-07-29 00:28:05 +00006870 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006871 // an integer value, not just an instruction so lower it to the ptest
6872 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006873 case Intrinsic::x86_sse41_ptestz:
6874 case Intrinsic::x86_sse41_ptestc:
6875 case Intrinsic::x86_sse41_ptestnzc:{
6876 unsigned X86CC = 0;
6877 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006878 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006879 case Intrinsic::x86_sse41_ptestz:
6880 // ZF = 1
6881 X86CC = X86::COND_E;
6882 break;
6883 case Intrinsic::x86_sse41_ptestc:
6884 // CF = 1
6885 X86CC = X86::COND_B;
6886 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006887 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006888 // ZF and CF = 0
6889 X86CC = X86::COND_A;
6890 break;
6891 }
Eric Christopherfd179292009-08-27 18:07:15 +00006892
Eric Christopher71c67532009-07-29 00:28:05 +00006893 SDValue LHS = Op.getOperand(1);
6894 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006895 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6896 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6897 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6898 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006899 }
Evan Cheng5759f972008-05-04 09:15:50 +00006900
6901 // Fix vector shift instructions where the last operand is a non-immediate
6902 // i32 value.
6903 case Intrinsic::x86_sse2_pslli_w:
6904 case Intrinsic::x86_sse2_pslli_d:
6905 case Intrinsic::x86_sse2_pslli_q:
6906 case Intrinsic::x86_sse2_psrli_w:
6907 case Intrinsic::x86_sse2_psrli_d:
6908 case Intrinsic::x86_sse2_psrli_q:
6909 case Intrinsic::x86_sse2_psrai_w:
6910 case Intrinsic::x86_sse2_psrai_d:
6911 case Intrinsic::x86_mmx_pslli_w:
6912 case Intrinsic::x86_mmx_pslli_d:
6913 case Intrinsic::x86_mmx_pslli_q:
6914 case Intrinsic::x86_mmx_psrli_w:
6915 case Intrinsic::x86_mmx_psrli_d:
6916 case Intrinsic::x86_mmx_psrli_q:
6917 case Intrinsic::x86_mmx_psrai_w:
6918 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006919 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006920 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006921 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006922
6923 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006925 switch (IntNo) {
6926 case Intrinsic::x86_sse2_pslli_w:
6927 NewIntNo = Intrinsic::x86_sse2_psll_w;
6928 break;
6929 case Intrinsic::x86_sse2_pslli_d:
6930 NewIntNo = Intrinsic::x86_sse2_psll_d;
6931 break;
6932 case Intrinsic::x86_sse2_pslli_q:
6933 NewIntNo = Intrinsic::x86_sse2_psll_q;
6934 break;
6935 case Intrinsic::x86_sse2_psrli_w:
6936 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6937 break;
6938 case Intrinsic::x86_sse2_psrli_d:
6939 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6940 break;
6941 case Intrinsic::x86_sse2_psrli_q:
6942 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6943 break;
6944 case Intrinsic::x86_sse2_psrai_w:
6945 NewIntNo = Intrinsic::x86_sse2_psra_w;
6946 break;
6947 case Intrinsic::x86_sse2_psrai_d:
6948 NewIntNo = Intrinsic::x86_sse2_psra_d;
6949 break;
6950 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006951 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006952 switch (IntNo) {
6953 case Intrinsic::x86_mmx_pslli_w:
6954 NewIntNo = Intrinsic::x86_mmx_psll_w;
6955 break;
6956 case Intrinsic::x86_mmx_pslli_d:
6957 NewIntNo = Intrinsic::x86_mmx_psll_d;
6958 break;
6959 case Intrinsic::x86_mmx_pslli_q:
6960 NewIntNo = Intrinsic::x86_mmx_psll_q;
6961 break;
6962 case Intrinsic::x86_mmx_psrli_w:
6963 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6964 break;
6965 case Intrinsic::x86_mmx_psrli_d:
6966 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6967 break;
6968 case Intrinsic::x86_mmx_psrli_q:
6969 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6970 break;
6971 case Intrinsic::x86_mmx_psrai_w:
6972 NewIntNo = Intrinsic::x86_mmx_psra_w;
6973 break;
6974 case Intrinsic::x86_mmx_psrai_d:
6975 NewIntNo = Intrinsic::x86_mmx_psra_d;
6976 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006977 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006978 }
6979 break;
6980 }
6981 }
Mon P Wangefa42202009-09-03 19:56:25 +00006982
6983 // The vector shift intrinsics with scalars uses 32b shift amounts but
6984 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6985 // to be zero.
6986 SDValue ShOps[4];
6987 ShOps[0] = ShAmt;
6988 ShOps[1] = DAG.getConstant(0, MVT::i32);
6989 if (ShAmtVT == MVT::v4i32) {
6990 ShOps[2] = DAG.getUNDEF(MVT::i32);
6991 ShOps[3] = DAG.getUNDEF(MVT::i32);
6992 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6993 } else {
6994 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6995 }
6996
Owen Andersone50ed302009-08-10 22:56:29 +00006997 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006998 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006999 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007000 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007001 Op.getOperand(1), ShAmt);
7002 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007003 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007004}
Evan Cheng72261582005-12-20 06:22:03 +00007005
Dan Gohman475871a2008-07-27 21:46:04 +00007006SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00007007 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007008 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007009
7010 if (Depth > 0) {
7011 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7012 SDValue Offset =
7013 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007014 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007015 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007016 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007017 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007018 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007019 }
7020
7021 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007022 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007023 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007024 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007025}
7026
Dan Gohman475871a2008-07-27 21:46:04 +00007027SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00007028 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7029 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007030 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007031 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007032 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7033 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007034 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007035 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007036 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7037 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007038 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007039}
7040
Dan Gohman475871a2008-07-27 21:46:04 +00007041SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00007042 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007043 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007044}
7045
Dan Gohman475871a2008-07-27 21:46:04 +00007046SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007047{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007048 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007049 SDValue Chain = Op.getOperand(0);
7050 SDValue Offset = Op.getOperand(1);
7051 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007052 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007053
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007054 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7055 getPointerTy());
7056 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007057
Dale Johannesene4d209d2009-02-03 20:21:25 +00007058 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007059 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007060 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007061 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007062 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007063 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007064
Dale Johannesene4d209d2009-02-03 20:21:25 +00007065 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007066 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007067 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007068}
7069
Dan Gohman475871a2008-07-27 21:46:04 +00007070SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00007071 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00007072 SDValue Root = Op.getOperand(0);
7073 SDValue Trmp = Op.getOperand(1); // trampoline
7074 SDValue FPtr = Op.getOperand(2); // nested function
7075 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007076 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007077
Dan Gohman69de1932008-02-06 22:27:42 +00007078 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007079
7080 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007081 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007082
7083 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007084 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7085 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007086
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007087 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7088 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007089
7090 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7091
7092 // Load the pointer to the nested function into R11.
7093 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007094 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007095 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007096 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007097
Owen Anderson825b72b2009-08-11 20:47:22 +00007098 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7099 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007100 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7101 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007102
7103 // Load the 'nest' parameter value into R10.
7104 // R10 is specified in X86CallingConv.td
7105 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7107 DAG.getConstant(10, MVT::i64));
7108 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007109 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007110
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7112 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007113 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7114 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007115
7116 // Jump to the nested function.
7117 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7119 DAG.getConstant(20, MVT::i64));
7120 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007121 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007122
7123 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007124 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7125 DAG.getConstant(22, MVT::i64));
7126 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007127 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007128
Dan Gohman475871a2008-07-27 21:46:04 +00007129 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007130 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007131 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007132 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007133 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007134 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007135 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007136 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007137
7138 switch (CC) {
7139 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007140 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007141 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007142 case CallingConv::X86_StdCall: {
7143 // Pass 'nest' parameter in ECX.
7144 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007145 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007146
7147 // Check that ECX wasn't needed by an 'inreg' parameter.
7148 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007149 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007150
Chris Lattner58d74912008-03-12 17:45:29 +00007151 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007152 unsigned InRegCount = 0;
7153 unsigned Idx = 1;
7154
7155 for (FunctionType::param_iterator I = FTy->param_begin(),
7156 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007157 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007158 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007159 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007160
7161 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00007162 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007163 }
7164 }
7165 break;
7166 }
7167 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007168 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007169 // Pass 'nest' parameter in EAX.
7170 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007171 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007172 break;
7173 }
7174
Dan Gohman475871a2008-07-27 21:46:04 +00007175 SDValue OutChains[4];
7176 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007177
Owen Anderson825b72b2009-08-11 20:47:22 +00007178 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7179 DAG.getConstant(10, MVT::i32));
7180 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007181
Chris Lattnera62fe662010-02-05 19:20:30 +00007182 // This is storing the opcode for MOV32ri.
7183 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007184 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007185 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007186 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007187 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007188
Owen Anderson825b72b2009-08-11 20:47:22 +00007189 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7190 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007191 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7192 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007193
Chris Lattnera62fe662010-02-05 19:20:30 +00007194 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007195 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7196 DAG.getConstant(5, MVT::i32));
7197 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007198 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007199
Owen Anderson825b72b2009-08-11 20:47:22 +00007200 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7201 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007202 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7203 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007204
Dan Gohman475871a2008-07-27 21:46:04 +00007205 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007206 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007207 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007208 }
7209}
7210
Dan Gohman475871a2008-07-27 21:46:04 +00007211SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007212 /*
7213 The rounding mode is in bits 11:10 of FPSR, and has the following
7214 settings:
7215 00 Round to nearest
7216 01 Round to -inf
7217 10 Round to +inf
7218 11 Round to 0
7219
7220 FLT_ROUNDS, on the other hand, expects the following:
7221 -1 Undefined
7222 0 Round to 0
7223 1 Round to nearest
7224 2 Round to +inf
7225 3 Round to -inf
7226
7227 To perform the conversion, we do:
7228 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7229 */
7230
7231 MachineFunction &MF = DAG.getMachineFunction();
7232 const TargetMachine &TM = MF.getTarget();
7233 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7234 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007235 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007236 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007237
7238 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007239 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007240 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007241
Owen Anderson825b72b2009-08-11 20:47:22 +00007242 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007243 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007244
7245 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007246 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7247 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007248
7249 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007250 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007251 DAG.getNode(ISD::SRL, dl, MVT::i16,
7252 DAG.getNode(ISD::AND, dl, MVT::i16,
7253 CWD, DAG.getConstant(0x800, MVT::i16)),
7254 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007255 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007256 DAG.getNode(ISD::SRL, dl, MVT::i16,
7257 DAG.getNode(ISD::AND, dl, MVT::i16,
7258 CWD, DAG.getConstant(0x400, MVT::i16)),
7259 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007260
Dan Gohman475871a2008-07-27 21:46:04 +00007261 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007262 DAG.getNode(ISD::AND, dl, MVT::i16,
7263 DAG.getNode(ISD::ADD, dl, MVT::i16,
7264 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7265 DAG.getConstant(1, MVT::i16)),
7266 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007267
7268
Duncan Sands83ec4b62008-06-06 12:08:01 +00007269 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007270 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007271}
7272
Dan Gohman475871a2008-07-27 21:46:04 +00007273SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007274 EVT VT = Op.getValueType();
7275 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007276 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007277 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007278
7279 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007280 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007281 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007282 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007283 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007284 }
Evan Cheng18efe262007-12-14 02:13:44 +00007285
Evan Cheng152804e2007-12-14 08:30:15 +00007286 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007287 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007288 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007289
7290 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007291 SDValue Ops[] = {
7292 Op,
7293 DAG.getConstant(NumBits+NumBits-1, OpVT),
7294 DAG.getConstant(X86::COND_E, MVT::i8),
7295 Op.getValue(1)
7296 };
7297 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007298
7299 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007300 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007301
Owen Anderson825b72b2009-08-11 20:47:22 +00007302 if (VT == MVT::i8)
7303 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007304 return Op;
7305}
7306
Dan Gohman475871a2008-07-27 21:46:04 +00007307SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007308 EVT VT = Op.getValueType();
7309 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007310 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007311 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007312
7313 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 if (VT == MVT::i8) {
7315 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007316 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007317 }
Evan Cheng152804e2007-12-14 08:30:15 +00007318
7319 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007320 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007321 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007322
7323 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007324 SDValue Ops[] = {
7325 Op,
7326 DAG.getConstant(NumBits, OpVT),
7327 DAG.getConstant(X86::COND_E, MVT::i8),
7328 Op.getValue(1)
7329 };
7330 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007331
Owen Anderson825b72b2009-08-11 20:47:22 +00007332 if (VT == MVT::i8)
7333 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007334 return Op;
7335}
7336
Mon P Wangaf9b9522008-12-18 21:42:19 +00007337SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007338 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007339 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007340 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007341
Mon P Wangaf9b9522008-12-18 21:42:19 +00007342 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7343 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7344 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7345 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7346 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7347 //
7348 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7349 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7350 // return AloBlo + AloBhi + AhiBlo;
7351
7352 SDValue A = Op.getOperand(0);
7353 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007354
Dale Johannesene4d209d2009-02-03 20:21:25 +00007355 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007356 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7357 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007358 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007359 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7360 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007361 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007362 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007363 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007364 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007365 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007366 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007367 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007368 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007369 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007370 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007371 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7372 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007373 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007374 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7375 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007376 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7377 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007378 return Res;
7379}
7380
7381
Bill Wendling74c37652008-12-09 22:08:41 +00007382SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7383 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7384 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007385 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7386 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007387 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007388 SDValue LHS = N->getOperand(0);
7389 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007390 unsigned BaseOp = 0;
7391 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007392 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007393
7394 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007395 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007396 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007397 // A subtract of one will be selected as a INC. Note that INC doesn't
7398 // set CF, so we can't do this for UADDO.
7399 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7400 if (C->getAPIntValue() == 1) {
7401 BaseOp = X86ISD::INC;
7402 Cond = X86::COND_O;
7403 break;
7404 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007405 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007406 Cond = X86::COND_O;
7407 break;
7408 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007409 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007410 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007411 break;
7412 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007413 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7414 // set CF, so we can't do this for USUBO.
7415 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7416 if (C->getAPIntValue() == 1) {
7417 BaseOp = X86ISD::DEC;
7418 Cond = X86::COND_O;
7419 break;
7420 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007421 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007422 Cond = X86::COND_O;
7423 break;
7424 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007425 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007426 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007427 break;
7428 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007429 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007430 Cond = X86::COND_O;
7431 break;
7432 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007433 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007434 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007435 break;
7436 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007437
Bill Wendling61edeb52008-12-02 01:06:39 +00007438 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007439 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007440 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007441
Bill Wendling61edeb52008-12-02 01:06:39 +00007442 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007443 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007444 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007445
Bill Wendling61edeb52008-12-02 01:06:39 +00007446 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7447 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007448}
7449
Dan Gohman475871a2008-07-27 21:46:04 +00007450SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007451 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007452 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007453 unsigned Reg = 0;
7454 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007455 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007456 default:
7457 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 case MVT::i8: Reg = X86::AL; size = 1; break;
7459 case MVT::i16: Reg = X86::AX; size = 2; break;
7460 case MVT::i32: Reg = X86::EAX; size = 4; break;
7461 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007462 assert(Subtarget->is64Bit() && "Node not type legal!");
7463 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007464 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007465 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007466 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007467 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007468 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007469 Op.getOperand(1),
7470 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007471 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007472 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007473 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007474 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007475 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007476 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007477 return cpOut;
7478}
7479
Duncan Sands1607f052008-12-01 11:39:25 +00007480SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007481 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007482 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007483 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007484 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007485 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007486 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007487 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7488 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007489 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007490 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7491 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007492 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007493 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007494 rdx.getValue(1)
7495 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007496 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007497}
7498
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007499SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7500 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007501 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007502 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007503 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007504 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007505 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007506 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007507 Node->getOperand(0),
7508 Node->getOperand(1), negOp,
7509 cast<AtomicSDNode>(Node)->getSrcValue(),
7510 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007511}
7512
Evan Cheng0db9fe62006-04-25 20:13:52 +00007513/// LowerOperation - Provide custom lowering hooks for some operations.
7514///
Dan Gohman475871a2008-07-27 21:46:04 +00007515SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007516 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007517 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007518 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7519 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007520 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007521 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007522 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7523 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7524 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7525 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7526 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7527 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007528 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007529 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007530 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007531 case ISD::SHL_PARTS:
7532 case ISD::SRA_PARTS:
7533 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7534 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007535 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007536 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007537 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007538 case ISD::FABS: return LowerFABS(Op, DAG);
7539 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007540 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007541 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007542 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007543 case ISD::SELECT: return LowerSELECT(Op, DAG);
7544 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007545 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007546 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007547 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007548 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007549 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007550 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7551 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007552 case ISD::FRAME_TO_ARGS_OFFSET:
7553 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007554 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007555 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007556 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007557 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007558 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7559 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007560 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007561 case ISD::SADDO:
7562 case ISD::UADDO:
7563 case ISD::SSUBO:
7564 case ISD::USUBO:
7565 case ISD::SMULO:
7566 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007567 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007568 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007569}
7570
Duncan Sands1607f052008-12-01 11:39:25 +00007571void X86TargetLowering::
7572ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7573 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007574 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007575 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007576 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007577
7578 SDValue Chain = Node->getOperand(0);
7579 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007580 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007581 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007583 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007584 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007585 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007586 SDValue Result =
7587 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7588 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007589 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007590 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007591 Results.push_back(Result.getValue(2));
7592}
7593
Duncan Sands126d9072008-07-04 11:47:58 +00007594/// ReplaceNodeResults - Replace a node with an illegal result type
7595/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007596void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7597 SmallVectorImpl<SDValue>&Results,
7598 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007599 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007600 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007601 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007602 assert(false && "Do not know how to custom type legalize this operation!");
7603 return;
7604 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007605 std::pair<SDValue,SDValue> Vals =
7606 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007607 SDValue FIST = Vals.first, StackSlot = Vals.second;
7608 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007609 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007610 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007611 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7612 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007613 }
7614 return;
7615 }
7616 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007617 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007618 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007619 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007620 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007621 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007622 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007623 eax.getValue(2));
7624 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7625 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007626 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007627 Results.push_back(edx.getValue(1));
7628 return;
7629 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007630 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007631 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007632 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007633 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007634 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7635 DAG.getConstant(0, MVT::i32));
7636 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7637 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007638 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7639 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007640 cpInL.getValue(1));
7641 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7643 DAG.getConstant(0, MVT::i32));
7644 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7645 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007646 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007647 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007648 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007649 swapInL.getValue(1));
7650 SDValue Ops[] = { swapInH.getValue(0),
7651 N->getOperand(1),
7652 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007653 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007654 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007655 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007656 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007657 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007658 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007659 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007660 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007661 Results.push_back(cpOutH.getValue(1));
7662 return;
7663 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007664 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007665 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7666 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007667 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007668 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7669 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007670 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007671 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7672 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007673 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007674 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7675 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007676 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007677 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7678 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007679 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007680 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7681 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007682 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007683 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7684 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007685 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007686}
7687
Evan Cheng72261582005-12-20 06:22:03 +00007688const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7689 switch (Opcode) {
7690 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007691 case X86ISD::BSF: return "X86ISD::BSF";
7692 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007693 case X86ISD::SHLD: return "X86ISD::SHLD";
7694 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007695 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007696 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007697 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007698 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007699 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007700 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007701 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7702 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7703 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007704 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007705 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007706 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007707 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007708 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007709 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007710 case X86ISD::COMI: return "X86ISD::COMI";
7711 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007712 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007713 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007714 case X86ISD::CMOV: return "X86ISD::CMOV";
7715 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007716 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007717 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7718 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007719 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007720 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007721 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007722 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007723 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007724 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7725 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007726 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007727 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007728 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007729 case X86ISD::FMAX: return "X86ISD::FMAX";
7730 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007731 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7732 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007733 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007734 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007735 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007736 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007737 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007738 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7739 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007740 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7741 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7742 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7743 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7744 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7745 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007746 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7747 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007748 case X86ISD::VSHL: return "X86ISD::VSHL";
7749 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007750 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7751 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7752 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7753 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7754 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7755 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7756 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7757 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7758 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7759 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007760 case X86ISD::ADD: return "X86ISD::ADD";
7761 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007762 case X86ISD::SMUL: return "X86ISD::SMUL";
7763 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007764 case X86ISD::INC: return "X86ISD::INC";
7765 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007766 case X86ISD::OR: return "X86ISD::OR";
7767 case X86ISD::XOR: return "X86ISD::XOR";
7768 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007769 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007770 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007771 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007772 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007773 }
7774}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007775
Chris Lattnerc9addb72007-03-30 23:15:24 +00007776// isLegalAddressingMode - Return true if the addressing mode represented
7777// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007778bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007779 const Type *Ty) const {
7780 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007781 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007782
Chris Lattnerc9addb72007-03-30 23:15:24 +00007783 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007784 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007785 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007786
Chris Lattnerc9addb72007-03-30 23:15:24 +00007787 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007788 unsigned GVFlags =
7789 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007790
Chris Lattnerdfed4132009-07-10 07:38:24 +00007791 // If a reference to this global requires an extra load, we can't fold it.
7792 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007793 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007794
Chris Lattnerdfed4132009-07-10 07:38:24 +00007795 // If BaseGV requires a register for the PIC base, we cannot also have a
7796 // BaseReg specified.
7797 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007798 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007799
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007800 // If lower 4G is not available, then we must use rip-relative addressing.
7801 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7802 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007803 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007804
Chris Lattnerc9addb72007-03-30 23:15:24 +00007805 switch (AM.Scale) {
7806 case 0:
7807 case 1:
7808 case 2:
7809 case 4:
7810 case 8:
7811 // These scales always work.
7812 break;
7813 case 3:
7814 case 5:
7815 case 9:
7816 // These scales are formed with basereg+scalereg. Only accept if there is
7817 // no basereg yet.
7818 if (AM.HasBaseReg)
7819 return false;
7820 break;
7821 default: // Other stuff never works.
7822 return false;
7823 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007824
Chris Lattnerc9addb72007-03-30 23:15:24 +00007825 return true;
7826}
7827
7828
Evan Cheng2bd122c2007-10-26 01:56:11 +00007829bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007830 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007831 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007832 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7833 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007834 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007835 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007836 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007837}
7838
Owen Andersone50ed302009-08-10 22:56:29 +00007839bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007840 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007841 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007842 unsigned NumBits1 = VT1.getSizeInBits();
7843 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007844 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007845 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007846 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007847}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007848
Dan Gohman97121ba2009-04-08 00:15:30 +00007849bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007850 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007851 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007852}
7853
Owen Andersone50ed302009-08-10 22:56:29 +00007854bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007855 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007856 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007857}
7858
Owen Andersone50ed302009-08-10 22:56:29 +00007859bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007860 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007861 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007862}
7863
Evan Cheng60c07e12006-07-05 22:17:51 +00007864/// isShuffleMaskLegal - Targets can use this to indicate that they only
7865/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7866/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7867/// are assumed to be legal.
7868bool
Eric Christopherfd179292009-08-27 18:07:15 +00007869X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007870 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007871 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007872 if (VT.getSizeInBits() == 64)
7873 return false;
7874
Nate Begemana09008b2009-10-19 02:17:23 +00007875 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007876 return (VT.getVectorNumElements() == 2 ||
7877 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7878 isMOVLMask(M, VT) ||
7879 isSHUFPMask(M, VT) ||
7880 isPSHUFDMask(M, VT) ||
7881 isPSHUFHWMask(M, VT) ||
7882 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007883 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007884 isUNPCKLMask(M, VT) ||
7885 isUNPCKHMask(M, VT) ||
7886 isUNPCKL_v_undef_Mask(M, VT) ||
7887 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007888}
7889
Dan Gohman7d8143f2008-04-09 20:09:42 +00007890bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007891X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007892 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007893 unsigned NumElts = VT.getVectorNumElements();
7894 // FIXME: This collection of masks seems suspect.
7895 if (NumElts == 2)
7896 return true;
7897 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7898 return (isMOVLMask(Mask, VT) ||
7899 isCommutedMOVLMask(Mask, VT, true) ||
7900 isSHUFPMask(Mask, VT) ||
7901 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007902 }
7903 return false;
7904}
7905
7906//===----------------------------------------------------------------------===//
7907// X86 Scheduler Hooks
7908//===----------------------------------------------------------------------===//
7909
Mon P Wang63307c32008-05-05 19:05:59 +00007910// private utility function
7911MachineBasicBlock *
7912X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7913 MachineBasicBlock *MBB,
7914 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007915 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007916 unsigned LoadOpc,
7917 unsigned CXchgOpc,
7918 unsigned copyOpc,
7919 unsigned notOpc,
7920 unsigned EAXreg,
7921 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007922 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007923 // For the atomic bitwise operator, we generate
7924 // thisMBB:
7925 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007926 // ld t1 = [bitinstr.addr]
7927 // op t2 = t1, [bitinstr.val]
7928 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007929 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7930 // bz newMBB
7931 // fallthrough -->nextMBB
7932 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7933 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007934 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007935 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007936
Mon P Wang63307c32008-05-05 19:05:59 +00007937 /// First build the CFG
7938 MachineFunction *F = MBB->getParent();
7939 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007940 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7941 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7942 F->insert(MBBIter, newMBB);
7943 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007944
Mon P Wang63307c32008-05-05 19:05:59 +00007945 // Move all successors to thisMBB to nextMBB
7946 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007947
Mon P Wang63307c32008-05-05 19:05:59 +00007948 // Update thisMBB to fall through to newMBB
7949 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007950
Mon P Wang63307c32008-05-05 19:05:59 +00007951 // newMBB jumps to itself and fall through to nextMBB
7952 newMBB->addSuccessor(nextMBB);
7953 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007954
Mon P Wang63307c32008-05-05 19:05:59 +00007955 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007956 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007957 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007958 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007959 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007960 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007961 int numArgs = bInstr->getNumOperands() - 1;
7962 for (int i=0; i < numArgs; ++i)
7963 argOpers[i] = &bInstr->getOperand(i+1);
7964
7965 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007966 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7967 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007968
Dale Johannesen140be2d2008-08-19 18:47:28 +00007969 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007970 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007971 for (int i=0; i <= lastAddrIndx; ++i)
7972 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007973
Dale Johannesen140be2d2008-08-19 18:47:28 +00007974 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007975 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007976 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007977 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007978 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007979 tt = t1;
7980
Dale Johannesen140be2d2008-08-19 18:47:28 +00007981 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007982 assert((argOpers[valArgIndx]->isReg() ||
7983 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007984 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007985 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007986 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007987 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007988 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007989 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007990 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007991
Dale Johannesene4d209d2009-02-03 20:21:25 +00007992 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007993 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007994
Dale Johannesene4d209d2009-02-03 20:21:25 +00007995 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007996 for (int i=0; i <= lastAddrIndx; ++i)
7997 (*MIB).addOperand(*argOpers[i]);
7998 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007999 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008000 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8001 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008002
Dale Johannesene4d209d2009-02-03 20:21:25 +00008003 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008004 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008005
Mon P Wang63307c32008-05-05 19:05:59 +00008006 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008007 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008008
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008009 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008010 return nextMBB;
8011}
8012
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008013// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008014MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008015X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8016 MachineBasicBlock *MBB,
8017 unsigned regOpcL,
8018 unsigned regOpcH,
8019 unsigned immOpcL,
8020 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008021 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008022 // For the atomic bitwise operator, we generate
8023 // thisMBB (instructions are in pairs, except cmpxchg8b)
8024 // ld t1,t2 = [bitinstr.addr]
8025 // newMBB:
8026 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8027 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008028 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008029 // mov ECX, EBX <- t5, t6
8030 // mov EAX, EDX <- t1, t2
8031 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8032 // mov t3, t4 <- EAX, EDX
8033 // bz newMBB
8034 // result in out1, out2
8035 // fallthrough -->nextMBB
8036
8037 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8038 const unsigned LoadOpc = X86::MOV32rm;
8039 const unsigned copyOpc = X86::MOV32rr;
8040 const unsigned NotOpc = X86::NOT32r;
8041 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8042 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8043 MachineFunction::iterator MBBIter = MBB;
8044 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008045
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008046 /// First build the CFG
8047 MachineFunction *F = MBB->getParent();
8048 MachineBasicBlock *thisMBB = MBB;
8049 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8050 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8051 F->insert(MBBIter, newMBB);
8052 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008053
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008054 // Move all successors to thisMBB to nextMBB
8055 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008056
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008057 // Update thisMBB to fall through to newMBB
8058 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008059
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008060 // newMBB jumps to itself and fall through to nextMBB
8061 newMBB->addSuccessor(nextMBB);
8062 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008063
Dale Johannesene4d209d2009-02-03 20:21:25 +00008064 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008065 // Insert instructions into newMBB based on incoming instruction
8066 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008067 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008068 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008069 MachineOperand& dest1Oper = bInstr->getOperand(0);
8070 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008071 MachineOperand* argOpers[2 + X86AddrNumOperands];
8072 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008073 argOpers[i] = &bInstr->getOperand(i+2);
8074
Evan Chengad5b52f2010-01-08 19:14:57 +00008075 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008076 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008077
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008078 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008079 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008080 for (int i=0; i <= lastAddrIndx; ++i)
8081 (*MIB).addOperand(*argOpers[i]);
8082 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008083 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008084 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008085 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008086 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008087 MachineOperand newOp3 = *(argOpers[3]);
8088 if (newOp3.isImm())
8089 newOp3.setImm(newOp3.getImm()+4);
8090 else
8091 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008092 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008093 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008094
8095 // t3/4 are defined later, at the bottom of the loop
8096 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8097 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008098 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008099 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008100 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008101 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8102
Evan Cheng306b4ca2010-01-08 23:41:50 +00008103 // The subsequent operations should be using the destination registers of
8104 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008105 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008106 t1 = F->getRegInfo().createVirtualRegister(RC);
8107 t2 = F->getRegInfo().createVirtualRegister(RC);
8108 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8109 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008110 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008111 t1 = dest1Oper.getReg();
8112 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008113 }
8114
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008115 int valArgIndx = lastAddrIndx + 1;
8116 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008117 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008118 "invalid operand");
8119 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8120 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008121 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008122 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008123 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008124 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008125 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008126 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008127 (*MIB).addOperand(*argOpers[valArgIndx]);
8128 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008129 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008130 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008131 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008132 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008133 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008134 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008135 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008136 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008137 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008138 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008139
Dale Johannesene4d209d2009-02-03 20:21:25 +00008140 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008141 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008142 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008143 MIB.addReg(t2);
8144
Dale Johannesene4d209d2009-02-03 20:21:25 +00008145 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008146 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008147 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008148 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008149
Dale Johannesene4d209d2009-02-03 20:21:25 +00008150 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008151 for (int i=0; i <= lastAddrIndx; ++i)
8152 (*MIB).addOperand(*argOpers[i]);
8153
8154 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008155 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8156 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008157
Dale Johannesene4d209d2009-02-03 20:21:25 +00008158 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008159 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008160 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008161 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008162
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008163 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008164 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008165
8166 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8167 return nextMBB;
8168}
8169
8170// private utility function
8171MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008172X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8173 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008174 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008175 // For the atomic min/max operator, we generate
8176 // thisMBB:
8177 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008178 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008179 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008180 // cmp t1, t2
8181 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008182 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008183 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8184 // bz newMBB
8185 // fallthrough -->nextMBB
8186 //
8187 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8188 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008189 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008190 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008191
Mon P Wang63307c32008-05-05 19:05:59 +00008192 /// First build the CFG
8193 MachineFunction *F = MBB->getParent();
8194 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008195 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8196 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8197 F->insert(MBBIter, newMBB);
8198 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008199
Dan Gohmand6708ea2009-08-15 01:38:56 +00008200 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008201 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008202
Mon P Wang63307c32008-05-05 19:05:59 +00008203 // Update thisMBB to fall through to newMBB
8204 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008205
Mon P Wang63307c32008-05-05 19:05:59 +00008206 // newMBB jumps to newMBB and fall through to nextMBB
8207 newMBB->addSuccessor(nextMBB);
8208 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008209
Dale Johannesene4d209d2009-02-03 20:21:25 +00008210 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008211 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008212 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008213 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008214 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008215 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008216 int numArgs = mInstr->getNumOperands() - 1;
8217 for (int i=0; i < numArgs; ++i)
8218 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008219
Mon P Wang63307c32008-05-05 19:05:59 +00008220 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008221 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8222 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008223
Mon P Wangab3e7472008-05-05 22:56:23 +00008224 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008225 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008226 for (int i=0; i <= lastAddrIndx; ++i)
8227 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008228
Mon P Wang63307c32008-05-05 19:05:59 +00008229 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008230 assert((argOpers[valArgIndx]->isReg() ||
8231 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008232 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008233
8234 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008235 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008236 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008237 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008238 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008239 (*MIB).addOperand(*argOpers[valArgIndx]);
8240
Dale Johannesene4d209d2009-02-03 20:21:25 +00008241 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008242 MIB.addReg(t1);
8243
Dale Johannesene4d209d2009-02-03 20:21:25 +00008244 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008245 MIB.addReg(t1);
8246 MIB.addReg(t2);
8247
8248 // Generate movc
8249 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008250 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008251 MIB.addReg(t2);
8252 MIB.addReg(t1);
8253
8254 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008255 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008256 for (int i=0; i <= lastAddrIndx; ++i)
8257 (*MIB).addOperand(*argOpers[i]);
8258 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008259 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008260 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8261 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008262
Dale Johannesene4d209d2009-02-03 20:21:25 +00008263 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008264 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008265
Mon P Wang63307c32008-05-05 19:05:59 +00008266 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008267 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008268
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008269 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008270 return nextMBB;
8271}
8272
Eric Christopherf83a5de2009-08-27 18:08:16 +00008273// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8274// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008275MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008276X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008277 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008278
8279 MachineFunction *F = BB->getParent();
8280 DebugLoc dl = MI->getDebugLoc();
8281 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8282
8283 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008284 if (memArg)
8285 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8286 else
8287 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008288
8289 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8290
8291 for (unsigned i = 0; i < numArgs; ++i) {
8292 MachineOperand &Op = MI->getOperand(i+1);
8293
8294 if (!(Op.isReg() && Op.isImplicit()))
8295 MIB.addOperand(Op);
8296 }
8297
8298 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8299 .addReg(X86::XMM0);
8300
8301 F->DeleteMachineInstr(MI);
8302
8303 return BB;
8304}
8305
8306MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008307X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8308 MachineInstr *MI,
8309 MachineBasicBlock *MBB) const {
8310 // Emit code to save XMM registers to the stack. The ABI says that the
8311 // number of registers to save is given in %al, so it's theoretically
8312 // possible to do an indirect jump trick to avoid saving all of them,
8313 // however this code takes a simpler approach and just executes all
8314 // of the stores if %al is non-zero. It's less code, and it's probably
8315 // easier on the hardware branch predictor, and stores aren't all that
8316 // expensive anyway.
8317
8318 // Create the new basic blocks. One block contains all the XMM stores,
8319 // and one block is the final destination regardless of whether any
8320 // stores were performed.
8321 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8322 MachineFunction *F = MBB->getParent();
8323 MachineFunction::iterator MBBIter = MBB;
8324 ++MBBIter;
8325 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8326 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8327 F->insert(MBBIter, XMMSaveMBB);
8328 F->insert(MBBIter, EndMBB);
8329
8330 // Set up the CFG.
8331 // Move any original successors of MBB to the end block.
8332 EndMBB->transferSuccessors(MBB);
8333 // The original block will now fall through to the XMM save block.
8334 MBB->addSuccessor(XMMSaveMBB);
8335 // The XMMSaveMBB will fall through to the end block.
8336 XMMSaveMBB->addSuccessor(EndMBB);
8337
8338 // Now add the instructions.
8339 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8340 DebugLoc DL = MI->getDebugLoc();
8341
8342 unsigned CountReg = MI->getOperand(0).getReg();
8343 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8344 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8345
8346 if (!Subtarget->isTargetWin64()) {
8347 // If %al is 0, branch around the XMM save block.
8348 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008349 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008350 MBB->addSuccessor(EndMBB);
8351 }
8352
8353 // In the XMM save block, save all the XMM argument registers.
8354 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8355 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008356 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008357 F->getMachineMemOperand(
8358 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8359 MachineMemOperand::MOStore, Offset,
8360 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008361 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8362 .addFrameIndex(RegSaveFrameIndex)
8363 .addImm(/*Scale=*/1)
8364 .addReg(/*IndexReg=*/0)
8365 .addImm(/*Disp=*/Offset)
8366 .addReg(/*Segment=*/0)
8367 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008368 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008369 }
8370
8371 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8372
8373 return EndMBB;
8374}
Mon P Wang63307c32008-05-05 19:05:59 +00008375
Evan Cheng60c07e12006-07-05 22:17:51 +00008376MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008377X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008378 MachineBasicBlock *BB,
8379 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008380 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8381 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008382
Chris Lattner52600972009-09-02 05:57:00 +00008383 // To "insert" a SELECT_CC instruction, we actually have to insert the
8384 // diamond control-flow pattern. The incoming instruction knows the
8385 // destination vreg to set, the condition code register to branch on, the
8386 // true/false values to select between, and a branch opcode to use.
8387 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8388 MachineFunction::iterator It = BB;
8389 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008390
Chris Lattner52600972009-09-02 05:57:00 +00008391 // thisMBB:
8392 // ...
8393 // TrueVal = ...
8394 // cmpTY ccX, r1, r2
8395 // bCC copy1MBB
8396 // fallthrough --> copy0MBB
8397 MachineBasicBlock *thisMBB = BB;
8398 MachineFunction *F = BB->getParent();
8399 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8400 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8401 unsigned Opc =
8402 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8403 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8404 F->insert(It, copy0MBB);
8405 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008406 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008407 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008408 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008409 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008410 E = BB->succ_end(); I != E; ++I) {
8411 EM->insert(std::make_pair(*I, sinkMBB));
8412 sinkMBB->addSuccessor(*I);
8413 }
8414 // Next, remove all successors of the current block, and add the true
8415 // and fallthrough blocks as its successors.
8416 while (!BB->succ_empty())
8417 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008418 // Add the true and fallthrough blocks as its successors.
8419 BB->addSuccessor(copy0MBB);
8420 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008421
Chris Lattner52600972009-09-02 05:57:00 +00008422 // copy0MBB:
8423 // %FalseValue = ...
8424 // # fallthrough to sinkMBB
8425 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008426
Chris Lattner52600972009-09-02 05:57:00 +00008427 // Update machine-CFG edges
8428 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008429
Chris Lattner52600972009-09-02 05:57:00 +00008430 // sinkMBB:
8431 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8432 // ...
8433 BB = sinkMBB;
8434 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8435 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8436 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8437
8438 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8439 return BB;
8440}
8441
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008442MachineBasicBlock *
8443X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8444 MachineBasicBlock *BB,
8445 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8446 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8447 DebugLoc DL = MI->getDebugLoc();
8448 MachineFunction *F = BB->getParent();
8449
8450 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8451 // non-trivial part is impdef of ESP.
8452 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8453 // mingw-w64.
8454
8455 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8456 .addExternalSymbol("_alloca")
8457 .addReg(X86::EAX, RegState::Implicit)
8458 .addReg(X86::ESP, RegState::Implicit)
8459 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8460 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8461
8462 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8463 return BB;
8464}
Chris Lattner52600972009-09-02 05:57:00 +00008465
8466MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008467X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008468 MachineBasicBlock *BB,
8469 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008470 switch (MI->getOpcode()) {
8471 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008472 case X86::MINGW_ALLOCA:
8473 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008474 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008475 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008476 case X86::CMOV_FR32:
8477 case X86::CMOV_FR64:
8478 case X86::CMOV_V4F32:
8479 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008480 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008481 case X86::CMOV_GR16:
8482 case X86::CMOV_GR32:
8483 case X86::CMOV_RFP32:
8484 case X86::CMOV_RFP64:
8485 case X86::CMOV_RFP80:
Evan Chengce319102009-09-19 09:51:03 +00008486 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008487
Dale Johannesen849f2142007-07-03 00:53:03 +00008488 case X86::FP32_TO_INT16_IN_MEM:
8489 case X86::FP32_TO_INT32_IN_MEM:
8490 case X86::FP32_TO_INT64_IN_MEM:
8491 case X86::FP64_TO_INT16_IN_MEM:
8492 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008493 case X86::FP64_TO_INT64_IN_MEM:
8494 case X86::FP80_TO_INT16_IN_MEM:
8495 case X86::FP80_TO_INT32_IN_MEM:
8496 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008497 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8498 DebugLoc DL = MI->getDebugLoc();
8499
Evan Cheng60c07e12006-07-05 22:17:51 +00008500 // Change the floating point control register to use "round towards zero"
8501 // mode when truncating to an integer value.
8502 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008503 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008504 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008505
8506 // Load the old value of the high byte of the control word...
8507 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008508 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008509 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008510 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008511
8512 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008513 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008514 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008515
8516 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008517 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008518
8519 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008520 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008521 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008522
8523 // Get the X86 opcode to use.
8524 unsigned Opc;
8525 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008526 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008527 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8528 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8529 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8530 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8531 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8532 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008533 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8534 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8535 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008536 }
8537
8538 X86AddressMode AM;
8539 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008540 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008541 AM.BaseType = X86AddressMode::RegBase;
8542 AM.Base.Reg = Op.getReg();
8543 } else {
8544 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008545 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008546 }
8547 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008548 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008549 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008550 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008551 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008552 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008553 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008554 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008555 AM.GV = Op.getGlobal();
8556 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008557 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008558 }
Chris Lattner52600972009-09-02 05:57:00 +00008559 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008560 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008561
8562 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008563 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008564
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008565 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008566 return BB;
8567 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00008568 // DBG_VALUE. Only the frame index case is done here.
8569 case X86::DBG_VALUE: {
8570 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8571 DebugLoc DL = MI->getDebugLoc();
8572 X86AddressMode AM;
8573 MachineFunction *F = BB->getParent();
8574 AM.BaseType = X86AddressMode::FrameIndexBase;
8575 AM.Base.FrameIndex = MI->getOperand(0).getImm();
8576 addFullAddress(BuildMI(BB, DL, TII->get(X86::DBG_VALUE)), AM).
8577 addImm(MI->getOperand(1).getImm()).
8578 addMetadata(MI->getOperand(2).getMetadata());
8579 F->DeleteMachineInstr(MI); // Remove pseudo.
8580 return BB;
8581 }
8582
Eric Christopherb120ab42009-08-18 22:50:32 +00008583 // String/text processing lowering.
8584 case X86::PCMPISTRM128REG:
8585 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8586 case X86::PCMPISTRM128MEM:
8587 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8588 case X86::PCMPESTRM128REG:
8589 return EmitPCMP(MI, BB, 5, false /* in mem */);
8590 case X86::PCMPESTRM128MEM:
8591 return EmitPCMP(MI, BB, 5, true /* in mem */);
8592
8593 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008594 case X86::ATOMAND32:
8595 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008596 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008597 X86::LCMPXCHG32, X86::MOV32rr,
8598 X86::NOT32r, X86::EAX,
8599 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008600 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008601 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8602 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008603 X86::LCMPXCHG32, X86::MOV32rr,
8604 X86::NOT32r, X86::EAX,
8605 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008606 case X86::ATOMXOR32:
8607 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008608 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008609 X86::LCMPXCHG32, X86::MOV32rr,
8610 X86::NOT32r, X86::EAX,
8611 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008612 case X86::ATOMNAND32:
8613 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008614 X86::AND32ri, X86::MOV32rm,
8615 X86::LCMPXCHG32, X86::MOV32rr,
8616 X86::NOT32r, X86::EAX,
8617 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008618 case X86::ATOMMIN32:
8619 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8620 case X86::ATOMMAX32:
8621 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8622 case X86::ATOMUMIN32:
8623 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8624 case X86::ATOMUMAX32:
8625 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008626
8627 case X86::ATOMAND16:
8628 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8629 X86::AND16ri, X86::MOV16rm,
8630 X86::LCMPXCHG16, X86::MOV16rr,
8631 X86::NOT16r, X86::AX,
8632 X86::GR16RegisterClass);
8633 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008634 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008635 X86::OR16ri, X86::MOV16rm,
8636 X86::LCMPXCHG16, X86::MOV16rr,
8637 X86::NOT16r, X86::AX,
8638 X86::GR16RegisterClass);
8639 case X86::ATOMXOR16:
8640 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8641 X86::XOR16ri, X86::MOV16rm,
8642 X86::LCMPXCHG16, X86::MOV16rr,
8643 X86::NOT16r, X86::AX,
8644 X86::GR16RegisterClass);
8645 case X86::ATOMNAND16:
8646 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8647 X86::AND16ri, X86::MOV16rm,
8648 X86::LCMPXCHG16, X86::MOV16rr,
8649 X86::NOT16r, X86::AX,
8650 X86::GR16RegisterClass, true);
8651 case X86::ATOMMIN16:
8652 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8653 case X86::ATOMMAX16:
8654 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8655 case X86::ATOMUMIN16:
8656 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8657 case X86::ATOMUMAX16:
8658 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8659
8660 case X86::ATOMAND8:
8661 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8662 X86::AND8ri, X86::MOV8rm,
8663 X86::LCMPXCHG8, X86::MOV8rr,
8664 X86::NOT8r, X86::AL,
8665 X86::GR8RegisterClass);
8666 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008667 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008668 X86::OR8ri, X86::MOV8rm,
8669 X86::LCMPXCHG8, X86::MOV8rr,
8670 X86::NOT8r, X86::AL,
8671 X86::GR8RegisterClass);
8672 case X86::ATOMXOR8:
8673 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8674 X86::XOR8ri, X86::MOV8rm,
8675 X86::LCMPXCHG8, X86::MOV8rr,
8676 X86::NOT8r, X86::AL,
8677 X86::GR8RegisterClass);
8678 case X86::ATOMNAND8:
8679 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8680 X86::AND8ri, X86::MOV8rm,
8681 X86::LCMPXCHG8, X86::MOV8rr,
8682 X86::NOT8r, X86::AL,
8683 X86::GR8RegisterClass, true);
8684 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008685 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008686 case X86::ATOMAND64:
8687 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008688 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008689 X86::LCMPXCHG64, X86::MOV64rr,
8690 X86::NOT64r, X86::RAX,
8691 X86::GR64RegisterClass);
8692 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008693 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8694 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008695 X86::LCMPXCHG64, X86::MOV64rr,
8696 X86::NOT64r, X86::RAX,
8697 X86::GR64RegisterClass);
8698 case X86::ATOMXOR64:
8699 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008700 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008701 X86::LCMPXCHG64, X86::MOV64rr,
8702 X86::NOT64r, X86::RAX,
8703 X86::GR64RegisterClass);
8704 case X86::ATOMNAND64:
8705 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8706 X86::AND64ri32, X86::MOV64rm,
8707 X86::LCMPXCHG64, X86::MOV64rr,
8708 X86::NOT64r, X86::RAX,
8709 X86::GR64RegisterClass, true);
8710 case X86::ATOMMIN64:
8711 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8712 case X86::ATOMMAX64:
8713 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8714 case X86::ATOMUMIN64:
8715 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8716 case X86::ATOMUMAX64:
8717 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008718
8719 // This group does 64-bit operations on a 32-bit host.
8720 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008721 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008722 X86::AND32rr, X86::AND32rr,
8723 X86::AND32ri, X86::AND32ri,
8724 false);
8725 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008726 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008727 X86::OR32rr, X86::OR32rr,
8728 X86::OR32ri, X86::OR32ri,
8729 false);
8730 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008731 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008732 X86::XOR32rr, X86::XOR32rr,
8733 X86::XOR32ri, X86::XOR32ri,
8734 false);
8735 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008736 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008737 X86::AND32rr, X86::AND32rr,
8738 X86::AND32ri, X86::AND32ri,
8739 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008740 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008741 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008742 X86::ADD32rr, X86::ADC32rr,
8743 X86::ADD32ri, X86::ADC32ri,
8744 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008745 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008746 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008747 X86::SUB32rr, X86::SBB32rr,
8748 X86::SUB32ri, X86::SBB32ri,
8749 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008750 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008751 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008752 X86::MOV32rr, X86::MOV32rr,
8753 X86::MOV32ri, X86::MOV32ri,
8754 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008755 case X86::VASTART_SAVE_XMM_REGS:
8756 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008757 }
8758}
8759
8760//===----------------------------------------------------------------------===//
8761// X86 Optimization Hooks
8762//===----------------------------------------------------------------------===//
8763
Dan Gohman475871a2008-07-27 21:46:04 +00008764void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008765 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008766 APInt &KnownZero,
8767 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008768 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008769 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008770 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008771 assert((Opc >= ISD::BUILTIN_OP_END ||
8772 Opc == ISD::INTRINSIC_WO_CHAIN ||
8773 Opc == ISD::INTRINSIC_W_CHAIN ||
8774 Opc == ISD::INTRINSIC_VOID) &&
8775 "Should use MaskedValueIsZero if you don't know whether Op"
8776 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008777
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008778 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008779 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008780 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008781 case X86ISD::ADD:
8782 case X86ISD::SUB:
8783 case X86ISD::SMUL:
8784 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008785 case X86ISD::INC:
8786 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008787 case X86ISD::OR:
8788 case X86ISD::XOR:
8789 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008790 // These nodes' second result is a boolean.
8791 if (Op.getResNo() == 0)
8792 break;
8793 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008794 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008795 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8796 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008797 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008798 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008799}
Chris Lattner259e97c2006-01-31 19:43:35 +00008800
Evan Cheng206ee9d2006-07-07 08:33:52 +00008801/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008802/// node is a GlobalAddress + offset.
8803bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8804 GlobalValue* &GA, int64_t &Offset) const{
8805 if (N->getOpcode() == X86ISD::Wrapper) {
8806 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008807 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008808 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008809 return true;
8810 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008811 }
Evan Chengad4196b2008-05-12 19:56:52 +00008812 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008813}
8814
Nate Begeman9008ca62009-04-27 18:41:29 +00008815static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008816 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008817 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008818 SelectionDAG &DAG, MachineFrameInfo *MFI,
8819 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008820 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008821 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008822 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008823 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008824 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008825 return false;
8826 continue;
8827 }
8828
Dan Gohman475871a2008-07-27 21:46:04 +00008829 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008830 if (!Elt.getNode() ||
8831 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008832 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008833 if (!LDBase) {
8834 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008835 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008836 LDBase = cast<LoadSDNode>(Elt.getNode());
8837 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008838 continue;
8839 }
8840 if (Elt.getOpcode() == ISD::UNDEF)
8841 continue;
8842
Nate Begemanabc01992009-06-05 21:37:30 +00008843 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008844 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008845 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008846 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008847 }
8848 return true;
8849}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008850
8851/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8852/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8853/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008854/// order. In the case of v2i64, it will see if it can rewrite the
8855/// shuffle to be an appropriate build vector so it can take advantage of
8856// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008857static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008858 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008859 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008860 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008861 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008862 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8863 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008864
Eli Friedman7a5e5552009-06-07 06:52:44 +00008865 if (VT.getSizeInBits() != 128)
8866 return SDValue();
8867
Mon P Wang1e955802009-04-03 02:43:30 +00008868 // Try to combine a vector_shuffle into a 128-bit load.
8869 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008870 LoadSDNode *LD = NULL;
8871 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008872 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008873 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008874 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008875
Eli Friedman7a5e5552009-06-07 06:52:44 +00008876 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008877 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008878 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8879 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00008880 LD->isVolatile(), LD->isNonTemporal(), 0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008881 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008882 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00008883 LD->isVolatile(), LD->isNonTemporal(),
8884 LD->getAlignment());
Eli Friedman7a5e5552009-06-07 06:52:44 +00008885 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008886 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008887 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8888 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008889 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8890 }
8891 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008892}
Evan Chengd880b972008-05-09 21:53:03 +00008893
Chris Lattner83e6c992006-10-04 06:57:07 +00008894/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008895static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008896 const X86Subtarget *Subtarget) {
8897 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008898 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008899 // Get the LHS/RHS of the select.
8900 SDValue LHS = N->getOperand(1);
8901 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008902
Dan Gohman670e5392009-09-21 18:03:22 +00008903 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008904 // instructions match the semantics of the common C idiom x<y?x:y but not
8905 // x<=y?x:y, because of how they handle negative zero (which can be
8906 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008907 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008908 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008909 Cond.getOpcode() == ISD::SETCC) {
8910 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008911
Chris Lattner47b4ce82009-03-11 05:48:52 +00008912 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008913 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00008914 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
8915 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008916 switch (CC) {
8917 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008918 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008919 // Converting this to a min would handle NaNs incorrectly, and swapping
8920 // the operands would cause it to handle comparisons between positive
8921 // and negative zero incorrectly.
8922 if (!FiniteOnlyFPMath() &&
8923 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8924 if (!UnsafeFPMath &&
8925 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8926 break;
8927 std::swap(LHS, RHS);
8928 }
Dan Gohman670e5392009-09-21 18:03:22 +00008929 Opcode = X86ISD::FMIN;
8930 break;
8931 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008932 // Converting this to a min would handle comparisons between positive
8933 // and negative zero incorrectly.
8934 if (!UnsafeFPMath &&
8935 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
8936 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008937 Opcode = X86ISD::FMIN;
8938 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008939 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008940 // Converting this to a min would handle both negative zeros and NaNs
8941 // incorrectly, but we can swap the operands to fix both.
8942 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008943 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008944 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008945 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008946 Opcode = X86ISD::FMIN;
8947 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008948
Dan Gohman670e5392009-09-21 18:03:22 +00008949 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008950 // Converting this to a max would handle comparisons between positive
8951 // and negative zero incorrectly.
8952 if (!UnsafeFPMath &&
8953 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
8954 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008955 Opcode = X86ISD::FMAX;
8956 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008957 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008958 // Converting this to a max would handle NaNs incorrectly, and swapping
8959 // the operands would cause it to handle comparisons between positive
8960 // and negative zero incorrectly.
8961 if (!FiniteOnlyFPMath() &&
8962 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8963 if (!UnsafeFPMath &&
8964 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8965 break;
8966 std::swap(LHS, RHS);
8967 }
Dan Gohman670e5392009-09-21 18:03:22 +00008968 Opcode = X86ISD::FMAX;
8969 break;
8970 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008971 // Converting this to a max would handle both negative zeros and NaNs
8972 // incorrectly, but we can swap the operands to fix both.
8973 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008974 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008975 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008976 case ISD::SETGE:
8977 Opcode = X86ISD::FMAX;
8978 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008979 }
Dan Gohman670e5392009-09-21 18:03:22 +00008980 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00008981 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
8982 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008983 switch (CC) {
8984 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008985 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008986 // Converting this to a min would handle comparisons between positive
8987 // and negative zero incorrectly, and swapping the operands would
8988 // cause it to handle NaNs incorrectly.
8989 if (!UnsafeFPMath &&
8990 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
8991 if (!FiniteOnlyFPMath() &&
8992 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8993 break;
8994 std::swap(LHS, RHS);
8995 }
Dan Gohman670e5392009-09-21 18:03:22 +00008996 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008997 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008998 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008999 // Converting this to a min would handle NaNs incorrectly.
9000 if (!UnsafeFPMath &&
9001 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9002 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009003 Opcode = X86ISD::FMIN;
9004 break;
9005 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009006 // Converting this to a min would handle both negative zeros and NaNs
9007 // incorrectly, but we can swap the operands to fix both.
9008 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009009 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009010 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009011 case ISD::SETGE:
9012 Opcode = X86ISD::FMIN;
9013 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009014
Dan Gohman670e5392009-09-21 18:03:22 +00009015 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009016 // Converting this to a max would handle NaNs incorrectly.
9017 if (!FiniteOnlyFPMath() &&
9018 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9019 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009020 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009021 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009022 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009023 // Converting this to a max would handle comparisons between positive
9024 // and negative zero incorrectly, and swapping the operands would
9025 // cause it to handle NaNs incorrectly.
9026 if (!UnsafeFPMath &&
9027 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9028 if (!FiniteOnlyFPMath() &&
9029 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9030 break;
9031 std::swap(LHS, RHS);
9032 }
Dan Gohman670e5392009-09-21 18:03:22 +00009033 Opcode = X86ISD::FMAX;
9034 break;
9035 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009036 // Converting this to a max would handle both negative zeros and NaNs
9037 // incorrectly, but we can swap the operands to fix both.
9038 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009039 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009040 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009041 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009042 Opcode = X86ISD::FMAX;
9043 break;
9044 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009045 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009046
Chris Lattner47b4ce82009-03-11 05:48:52 +00009047 if (Opcode)
9048 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009049 }
Eric Christopherfd179292009-08-27 18:07:15 +00009050
Chris Lattnerd1980a52009-03-12 06:52:53 +00009051 // If this is a select between two integer constants, try to do some
9052 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009053 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9054 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009055 // Don't do this for crazy integer types.
9056 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9057 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009058 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009059 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009060
Chris Lattnercee56e72009-03-13 05:53:31 +00009061 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009062 // Efficiently invertible.
9063 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9064 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9065 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9066 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009067 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009068 }
Eric Christopherfd179292009-08-27 18:07:15 +00009069
Chris Lattnerd1980a52009-03-12 06:52:53 +00009070 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009071 if (FalseC->getAPIntValue() == 0 &&
9072 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009073 if (NeedsCondInvert) // Invert the condition if needed.
9074 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9075 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009076
Chris Lattnerd1980a52009-03-12 06:52:53 +00009077 // Zero extend the condition if needed.
9078 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009079
Chris Lattnercee56e72009-03-13 05:53:31 +00009080 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009081 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009082 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009083 }
Eric Christopherfd179292009-08-27 18:07:15 +00009084
Chris Lattner97a29a52009-03-13 05:22:11 +00009085 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009086 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009087 if (NeedsCondInvert) // Invert the condition if needed.
9088 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9089 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009090
Chris Lattner97a29a52009-03-13 05:22:11 +00009091 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009092 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9093 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009094 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009095 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009096 }
Eric Christopherfd179292009-08-27 18:07:15 +00009097
Chris Lattnercee56e72009-03-13 05:53:31 +00009098 // Optimize cases that will turn into an LEA instruction. This requires
9099 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009100 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009101 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009102 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009103
Chris Lattnercee56e72009-03-13 05:53:31 +00009104 bool isFastMultiplier = false;
9105 if (Diff < 10) {
9106 switch ((unsigned char)Diff) {
9107 default: break;
9108 case 1: // result = add base, cond
9109 case 2: // result = lea base( , cond*2)
9110 case 3: // result = lea base(cond, cond*2)
9111 case 4: // result = lea base( , cond*4)
9112 case 5: // result = lea base(cond, cond*4)
9113 case 8: // result = lea base( , cond*8)
9114 case 9: // result = lea base(cond, cond*8)
9115 isFastMultiplier = true;
9116 break;
9117 }
9118 }
Eric Christopherfd179292009-08-27 18:07:15 +00009119
Chris Lattnercee56e72009-03-13 05:53:31 +00009120 if (isFastMultiplier) {
9121 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9122 if (NeedsCondInvert) // Invert the condition if needed.
9123 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9124 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009125
Chris Lattnercee56e72009-03-13 05:53:31 +00009126 // Zero extend the condition if needed.
9127 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9128 Cond);
9129 // Scale the condition by the difference.
9130 if (Diff != 1)
9131 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9132 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009133
Chris Lattnercee56e72009-03-13 05:53:31 +00009134 // Add the base if non-zero.
9135 if (FalseC->getAPIntValue() != 0)
9136 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9137 SDValue(FalseC, 0));
9138 return Cond;
9139 }
Eric Christopherfd179292009-08-27 18:07:15 +00009140 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009141 }
9142 }
Eric Christopherfd179292009-08-27 18:07:15 +00009143
Dan Gohman475871a2008-07-27 21:46:04 +00009144 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009145}
9146
Chris Lattnerd1980a52009-03-12 06:52:53 +00009147/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9148static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9149 TargetLowering::DAGCombinerInfo &DCI) {
9150 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009151
Chris Lattnerd1980a52009-03-12 06:52:53 +00009152 // If the flag operand isn't dead, don't touch this CMOV.
9153 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9154 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009155
Chris Lattnerd1980a52009-03-12 06:52:53 +00009156 // If this is a select between two integer constants, try to do some
9157 // optimizations. Note that the operands are ordered the opposite of SELECT
9158 // operands.
9159 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9160 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9161 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9162 // larger than FalseC (the false value).
9163 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009164
Chris Lattnerd1980a52009-03-12 06:52:53 +00009165 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9166 CC = X86::GetOppositeBranchCondition(CC);
9167 std::swap(TrueC, FalseC);
9168 }
Eric Christopherfd179292009-08-27 18:07:15 +00009169
Chris Lattnerd1980a52009-03-12 06:52:53 +00009170 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009171 // This is efficient for any integer data type (including i8/i16) and
9172 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009173 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9174 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009175 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9176 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009177
Chris Lattnerd1980a52009-03-12 06:52:53 +00009178 // Zero extend the condition if needed.
9179 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009180
Chris Lattnerd1980a52009-03-12 06:52:53 +00009181 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9182 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009183 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009184 if (N->getNumValues() == 2) // Dead flag value?
9185 return DCI.CombineTo(N, Cond, SDValue());
9186 return Cond;
9187 }
Eric Christopherfd179292009-08-27 18:07:15 +00009188
Chris Lattnercee56e72009-03-13 05:53:31 +00009189 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9190 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009191 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9192 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009193 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9194 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009195
Chris Lattner97a29a52009-03-13 05:22:11 +00009196 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009197 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9198 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009199 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9200 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009201
Chris Lattner97a29a52009-03-13 05:22:11 +00009202 if (N->getNumValues() == 2) // Dead flag value?
9203 return DCI.CombineTo(N, Cond, SDValue());
9204 return Cond;
9205 }
Eric Christopherfd179292009-08-27 18:07:15 +00009206
Chris Lattnercee56e72009-03-13 05:53:31 +00009207 // Optimize cases that will turn into an LEA instruction. This requires
9208 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009209 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009210 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009211 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009212
Chris Lattnercee56e72009-03-13 05:53:31 +00009213 bool isFastMultiplier = false;
9214 if (Diff < 10) {
9215 switch ((unsigned char)Diff) {
9216 default: break;
9217 case 1: // result = add base, cond
9218 case 2: // result = lea base( , cond*2)
9219 case 3: // result = lea base(cond, cond*2)
9220 case 4: // result = lea base( , cond*4)
9221 case 5: // result = lea base(cond, cond*4)
9222 case 8: // result = lea base( , cond*8)
9223 case 9: // result = lea base(cond, cond*8)
9224 isFastMultiplier = true;
9225 break;
9226 }
9227 }
Eric Christopherfd179292009-08-27 18:07:15 +00009228
Chris Lattnercee56e72009-03-13 05:53:31 +00009229 if (isFastMultiplier) {
9230 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9231 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009232 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9233 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009234 // Zero extend the condition if needed.
9235 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9236 Cond);
9237 // Scale the condition by the difference.
9238 if (Diff != 1)
9239 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9240 DAG.getConstant(Diff, Cond.getValueType()));
9241
9242 // Add the base if non-zero.
9243 if (FalseC->getAPIntValue() != 0)
9244 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9245 SDValue(FalseC, 0));
9246 if (N->getNumValues() == 2) // Dead flag value?
9247 return DCI.CombineTo(N, Cond, SDValue());
9248 return Cond;
9249 }
Eric Christopherfd179292009-08-27 18:07:15 +00009250 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009251 }
9252 }
9253 return SDValue();
9254}
9255
9256
Evan Cheng0b0cd912009-03-28 05:57:29 +00009257/// PerformMulCombine - Optimize a single multiply with constant into two
9258/// in order to implement it with two cheaper instructions, e.g.
9259/// LEA + SHL, LEA + LEA.
9260static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9261 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009262 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9263 return SDValue();
9264
Owen Andersone50ed302009-08-10 22:56:29 +00009265 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009266 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009267 return SDValue();
9268
9269 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9270 if (!C)
9271 return SDValue();
9272 uint64_t MulAmt = C->getZExtValue();
9273 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9274 return SDValue();
9275
9276 uint64_t MulAmt1 = 0;
9277 uint64_t MulAmt2 = 0;
9278 if ((MulAmt % 9) == 0) {
9279 MulAmt1 = 9;
9280 MulAmt2 = MulAmt / 9;
9281 } else if ((MulAmt % 5) == 0) {
9282 MulAmt1 = 5;
9283 MulAmt2 = MulAmt / 5;
9284 } else if ((MulAmt % 3) == 0) {
9285 MulAmt1 = 3;
9286 MulAmt2 = MulAmt / 3;
9287 }
9288 if (MulAmt2 &&
9289 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9290 DebugLoc DL = N->getDebugLoc();
9291
9292 if (isPowerOf2_64(MulAmt2) &&
9293 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9294 // If second multiplifer is pow2, issue it first. We want the multiply by
9295 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9296 // is an add.
9297 std::swap(MulAmt1, MulAmt2);
9298
9299 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009300 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009301 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009302 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009303 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009304 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009305 DAG.getConstant(MulAmt1, VT));
9306
Eric Christopherfd179292009-08-27 18:07:15 +00009307 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009308 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009309 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009310 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009311 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009312 DAG.getConstant(MulAmt2, VT));
9313
9314 // Do not add new nodes to DAG combiner worklist.
9315 DCI.CombineTo(N, NewMul, false);
9316 }
9317 return SDValue();
9318}
9319
Evan Chengad9c0a32009-12-15 00:53:42 +00009320static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9321 SDValue N0 = N->getOperand(0);
9322 SDValue N1 = N->getOperand(1);
9323 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9324 EVT VT = N0.getValueType();
9325
9326 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9327 // since the result of setcc_c is all zero's or all ones.
9328 if (N1C && N0.getOpcode() == ISD::AND &&
9329 N0.getOperand(1).getOpcode() == ISD::Constant) {
9330 SDValue N00 = N0.getOperand(0);
9331 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9332 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9333 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9334 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9335 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9336 APInt ShAmt = N1C->getAPIntValue();
9337 Mask = Mask.shl(ShAmt);
9338 if (Mask != 0)
9339 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9340 N00, DAG.getConstant(Mask, VT));
9341 }
9342 }
9343
9344 return SDValue();
9345}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009346
Nate Begeman740ab032009-01-26 00:52:55 +00009347/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9348/// when possible.
9349static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9350 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009351 EVT VT = N->getValueType(0);
9352 if (!VT.isVector() && VT.isInteger() &&
9353 N->getOpcode() == ISD::SHL)
9354 return PerformSHLCombine(N, DAG);
9355
Nate Begeman740ab032009-01-26 00:52:55 +00009356 // On X86 with SSE2 support, we can transform this to a vector shift if
9357 // all elements are shifted by the same amount. We can't do this in legalize
9358 // because the a constant vector is typically transformed to a constant pool
9359 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009360 if (!Subtarget->hasSSE2())
9361 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009362
Owen Anderson825b72b2009-08-11 20:47:22 +00009363 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009364 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009365
Mon P Wang3becd092009-01-28 08:12:05 +00009366 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009367 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009368 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009369 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009370 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9371 unsigned NumElts = VT.getVectorNumElements();
9372 unsigned i = 0;
9373 for (; i != NumElts; ++i) {
9374 SDValue Arg = ShAmtOp.getOperand(i);
9375 if (Arg.getOpcode() == ISD::UNDEF) continue;
9376 BaseShAmt = Arg;
9377 break;
9378 }
9379 for (; i != NumElts; ++i) {
9380 SDValue Arg = ShAmtOp.getOperand(i);
9381 if (Arg.getOpcode() == ISD::UNDEF) continue;
9382 if (Arg != BaseShAmt) {
9383 return SDValue();
9384 }
9385 }
9386 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009387 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009388 SDValue InVec = ShAmtOp.getOperand(0);
9389 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9390 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9391 unsigned i = 0;
9392 for (; i != NumElts; ++i) {
9393 SDValue Arg = InVec.getOperand(i);
9394 if (Arg.getOpcode() == ISD::UNDEF) continue;
9395 BaseShAmt = Arg;
9396 break;
9397 }
9398 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9399 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009400 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009401 if (C->getZExtValue() == SplatIdx)
9402 BaseShAmt = InVec.getOperand(1);
9403 }
9404 }
9405 if (BaseShAmt.getNode() == 0)
9406 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9407 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009408 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009409 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009410
Mon P Wangefa42202009-09-03 19:56:25 +00009411 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009412 if (EltVT.bitsGT(MVT::i32))
9413 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9414 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009415 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009416
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009417 // The shift amount is identical so we can do a vector shift.
9418 SDValue ValOp = N->getOperand(0);
9419 switch (N->getOpcode()) {
9420 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009421 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009422 break;
9423 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009424 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009425 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009426 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009427 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009428 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009429 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009430 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009431 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009432 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009433 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009434 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009435 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009436 break;
9437 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009438 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009439 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009440 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009441 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009442 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009443 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009444 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009445 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009446 break;
9447 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009448 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009449 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009450 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009451 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009452 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009453 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009454 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009455 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009456 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009457 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009458 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009459 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009460 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009461 }
9462 return SDValue();
9463}
9464
Evan Cheng760d1942010-01-04 21:22:48 +00009465static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9466 const X86Subtarget *Subtarget) {
9467 EVT VT = N->getValueType(0);
9468 if (VT != MVT::i64 || !Subtarget->is64Bit())
9469 return SDValue();
9470
9471 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9472 SDValue N0 = N->getOperand(0);
9473 SDValue N1 = N->getOperand(1);
9474 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9475 std::swap(N0, N1);
9476 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9477 return SDValue();
9478
9479 SDValue ShAmt0 = N0.getOperand(1);
9480 if (ShAmt0.getValueType() != MVT::i8)
9481 return SDValue();
9482 SDValue ShAmt1 = N1.getOperand(1);
9483 if (ShAmt1.getValueType() != MVT::i8)
9484 return SDValue();
9485 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9486 ShAmt0 = ShAmt0.getOperand(0);
9487 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9488 ShAmt1 = ShAmt1.getOperand(0);
9489
9490 DebugLoc DL = N->getDebugLoc();
9491 unsigned Opc = X86ISD::SHLD;
9492 SDValue Op0 = N0.getOperand(0);
9493 SDValue Op1 = N1.getOperand(0);
9494 if (ShAmt0.getOpcode() == ISD::SUB) {
9495 Opc = X86ISD::SHRD;
9496 std::swap(Op0, Op1);
9497 std::swap(ShAmt0, ShAmt1);
9498 }
9499
9500 if (ShAmt1.getOpcode() == ISD::SUB) {
9501 SDValue Sum = ShAmt1.getOperand(0);
9502 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9503 if (SumC->getSExtValue() == 64 &&
9504 ShAmt1.getOperand(1) == ShAmt0)
9505 return DAG.getNode(Opc, DL, VT,
9506 Op0, Op1,
9507 DAG.getNode(ISD::TRUNCATE, DL,
9508 MVT::i8, ShAmt0));
9509 }
9510 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9511 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9512 if (ShAmt0C &&
9513 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9514 return DAG.getNode(Opc, DL, VT,
9515 N0.getOperand(0), N1.getOperand(0),
9516 DAG.getNode(ISD::TRUNCATE, DL,
9517 MVT::i8, ShAmt0));
9518 }
9519
9520 return SDValue();
9521}
9522
Chris Lattner149a4e52008-02-22 02:09:43 +00009523/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009524static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009525 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009526 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9527 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009528 // A preferable solution to the general problem is to figure out the right
9529 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009530
9531 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009532 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009533 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009534 if (VT.getSizeInBits() != 64)
9535 return SDValue();
9536
Devang Patel578efa92009-06-05 21:57:13 +00009537 const Function *F = DAG.getMachineFunction().getFunction();
9538 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009539 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009540 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009541 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009542 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009543 isa<LoadSDNode>(St->getValue()) &&
9544 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9545 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009546 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009547 LoadSDNode *Ld = 0;
9548 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009549 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009550 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009551 // Must be a store of a load. We currently handle two cases: the load
9552 // is a direct child, and it's under an intervening TokenFactor. It is
9553 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009554 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009555 Ld = cast<LoadSDNode>(St->getChain());
9556 else if (St->getValue().hasOneUse() &&
9557 ChainVal->getOpcode() == ISD::TokenFactor) {
9558 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009559 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009560 TokenFactorIndex = i;
9561 Ld = cast<LoadSDNode>(St->getValue());
9562 } else
9563 Ops.push_back(ChainVal->getOperand(i));
9564 }
9565 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009566
Evan Cheng536e6672009-03-12 05:59:15 +00009567 if (!Ld || !ISD::isNormalLoad(Ld))
9568 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009569
Evan Cheng536e6672009-03-12 05:59:15 +00009570 // If this is not the MMX case, i.e. we are just turning i64 load/store
9571 // into f64 load/store, avoid the transformation if there are multiple
9572 // uses of the loaded value.
9573 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9574 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009575
Evan Cheng536e6672009-03-12 05:59:15 +00009576 DebugLoc LdDL = Ld->getDebugLoc();
9577 DebugLoc StDL = N->getDebugLoc();
9578 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9579 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9580 // pair instead.
9581 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009582 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009583 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9584 Ld->getBasePtr(), Ld->getSrcValue(),
9585 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009586 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009587 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009588 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009589 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009591 Ops.size());
9592 }
Evan Cheng536e6672009-03-12 05:59:15 +00009593 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009594 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009595 St->isVolatile(), St->isNonTemporal(),
9596 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009597 }
Evan Cheng536e6672009-03-12 05:59:15 +00009598
9599 // Otherwise, lower to two pairs of 32-bit loads / stores.
9600 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009601 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9602 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009603
Owen Anderson825b72b2009-08-11 20:47:22 +00009604 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009605 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009606 Ld->isVolatile(), Ld->isNonTemporal(),
9607 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009609 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009610 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009611 MinAlign(Ld->getAlignment(), 4));
9612
9613 SDValue NewChain = LoLd.getValue(1);
9614 if (TokenFactorIndex != -1) {
9615 Ops.push_back(LoLd);
9616 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009617 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009618 Ops.size());
9619 }
9620
9621 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009622 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9623 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009624
9625 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9626 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009627 St->isVolatile(), St->isNonTemporal(),
9628 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009629 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9630 St->getSrcValue(),
9631 St->getSrcValueOffset() + 4,
9632 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009633 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009634 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009635 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009636 }
Dan Gohman475871a2008-07-27 21:46:04 +00009637 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009638}
9639
Chris Lattner6cf73262008-01-25 06:14:17 +00009640/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9641/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009642static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009643 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9644 // F[X]OR(0.0, x) -> x
9645 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009646 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9647 if (C->getValueAPF().isPosZero())
9648 return N->getOperand(1);
9649 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9650 if (C->getValueAPF().isPosZero())
9651 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009652 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009653}
9654
9655/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009656static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009657 // FAND(0.0, x) -> 0.0
9658 // FAND(x, 0.0) -> 0.0
9659 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9660 if (C->getValueAPF().isPosZero())
9661 return N->getOperand(0);
9662 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9663 if (C->getValueAPF().isPosZero())
9664 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009665 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009666}
9667
Dan Gohmane5af2d32009-01-29 01:59:02 +00009668static SDValue PerformBTCombine(SDNode *N,
9669 SelectionDAG &DAG,
9670 TargetLowering::DAGCombinerInfo &DCI) {
9671 // BT ignores high bits in the bit index operand.
9672 SDValue Op1 = N->getOperand(1);
9673 if (Op1.hasOneUse()) {
9674 unsigned BitWidth = Op1.getValueSizeInBits();
9675 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9676 APInt KnownZero, KnownOne;
9677 TargetLowering::TargetLoweringOpt TLO(DAG);
9678 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9679 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9680 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9681 DCI.CommitTargetLoweringOpt(TLO);
9682 }
9683 return SDValue();
9684}
Chris Lattner83e6c992006-10-04 06:57:07 +00009685
Eli Friedman7a5e5552009-06-07 06:52:44 +00009686static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9687 SDValue Op = N->getOperand(0);
9688 if (Op.getOpcode() == ISD::BIT_CONVERT)
9689 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009690 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009691 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009692 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009693 OpVT.getVectorElementType().getSizeInBits()) {
9694 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9695 }
9696 return SDValue();
9697}
9698
Owen Anderson99177002009-06-29 18:04:45 +00009699// On X86 and X86-64, atomic operations are lowered to locked instructions.
9700// Locked instructions, in turn, have implicit fence semantics (all memory
9701// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009702// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009703// fence-atomic-fence.
9704static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9705 SDValue atomic = N->getOperand(0);
9706 switch (atomic.getOpcode()) {
9707 case ISD::ATOMIC_CMP_SWAP:
9708 case ISD::ATOMIC_SWAP:
9709 case ISD::ATOMIC_LOAD_ADD:
9710 case ISD::ATOMIC_LOAD_SUB:
9711 case ISD::ATOMIC_LOAD_AND:
9712 case ISD::ATOMIC_LOAD_OR:
9713 case ISD::ATOMIC_LOAD_XOR:
9714 case ISD::ATOMIC_LOAD_NAND:
9715 case ISD::ATOMIC_LOAD_MIN:
9716 case ISD::ATOMIC_LOAD_MAX:
9717 case ISD::ATOMIC_LOAD_UMIN:
9718 case ISD::ATOMIC_LOAD_UMAX:
9719 break;
9720 default:
9721 return SDValue();
9722 }
Eric Christopherfd179292009-08-27 18:07:15 +00009723
Owen Anderson99177002009-06-29 18:04:45 +00009724 SDValue fence = atomic.getOperand(0);
9725 if (fence.getOpcode() != ISD::MEMBARRIER)
9726 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009727
Owen Anderson99177002009-06-29 18:04:45 +00009728 switch (atomic.getOpcode()) {
9729 case ISD::ATOMIC_CMP_SWAP:
9730 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9731 atomic.getOperand(1), atomic.getOperand(2),
9732 atomic.getOperand(3));
9733 case ISD::ATOMIC_SWAP:
9734 case ISD::ATOMIC_LOAD_ADD:
9735 case ISD::ATOMIC_LOAD_SUB:
9736 case ISD::ATOMIC_LOAD_AND:
9737 case ISD::ATOMIC_LOAD_OR:
9738 case ISD::ATOMIC_LOAD_XOR:
9739 case ISD::ATOMIC_LOAD_NAND:
9740 case ISD::ATOMIC_LOAD_MIN:
9741 case ISD::ATOMIC_LOAD_MAX:
9742 case ISD::ATOMIC_LOAD_UMIN:
9743 case ISD::ATOMIC_LOAD_UMAX:
9744 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9745 atomic.getOperand(1), atomic.getOperand(2));
9746 default:
9747 return SDValue();
9748 }
9749}
9750
Evan Cheng2e489c42009-12-16 00:53:11 +00009751static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9752 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9753 // (and (i32 x86isd::setcc_carry), 1)
9754 // This eliminates the zext. This transformation is necessary because
9755 // ISD::SETCC is always legalized to i8.
9756 DebugLoc dl = N->getDebugLoc();
9757 SDValue N0 = N->getOperand(0);
9758 EVT VT = N->getValueType(0);
9759 if (N0.getOpcode() == ISD::AND &&
9760 N0.hasOneUse() &&
9761 N0.getOperand(0).hasOneUse()) {
9762 SDValue N00 = N0.getOperand(0);
9763 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9764 return SDValue();
9765 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9766 if (!C || C->getZExtValue() != 1)
9767 return SDValue();
9768 return DAG.getNode(ISD::AND, dl, VT,
9769 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9770 N00.getOperand(0), N00.getOperand(1)),
9771 DAG.getConstant(1, VT));
9772 }
9773
9774 return SDValue();
9775}
9776
Dan Gohman475871a2008-07-27 21:46:04 +00009777SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009778 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009779 SelectionDAG &DAG = DCI.DAG;
9780 switch (N->getOpcode()) {
9781 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009782 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009783 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009784 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009785 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009786 case ISD::SHL:
9787 case ISD::SRA:
9788 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009789 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009790 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009791 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009792 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9793 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009794 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009795 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009796 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009797 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009798 }
9799
Dan Gohman475871a2008-07-27 21:46:04 +00009800 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009801}
9802
Evan Cheng60c07e12006-07-05 22:17:51 +00009803//===----------------------------------------------------------------------===//
9804// X86 Inline Assembly Support
9805//===----------------------------------------------------------------------===//
9806
Chris Lattnerb8105652009-07-20 17:51:36 +00009807static bool LowerToBSwap(CallInst *CI) {
9808 // FIXME: this should verify that we are targetting a 486 or better. If not,
9809 // we will turn this bswap into something that will be lowered to logical ops
9810 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9811 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009812
Chris Lattnerb8105652009-07-20 17:51:36 +00009813 // Verify this is a simple bswap.
9814 if (CI->getNumOperands() != 2 ||
9815 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009816 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009817 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009818
Chris Lattnerb8105652009-07-20 17:51:36 +00009819 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9820 if (!Ty || Ty->getBitWidth() % 16 != 0)
9821 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009822
Chris Lattnerb8105652009-07-20 17:51:36 +00009823 // Okay, we can do this xform, do so now.
9824 const Type *Tys[] = { Ty };
9825 Module *M = CI->getParent()->getParent()->getParent();
9826 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009827
Chris Lattnerb8105652009-07-20 17:51:36 +00009828 Value *Op = CI->getOperand(1);
9829 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009830
Chris Lattnerb8105652009-07-20 17:51:36 +00009831 CI->replaceAllUsesWith(Op);
9832 CI->eraseFromParent();
9833 return true;
9834}
9835
9836bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9837 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9838 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9839
9840 std::string AsmStr = IA->getAsmString();
9841
9842 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009843 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009844 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9845
9846 switch (AsmPieces.size()) {
9847 default: return false;
9848 case 1:
9849 AsmStr = AsmPieces[0];
9850 AsmPieces.clear();
9851 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9852
9853 // bswap $0
9854 if (AsmPieces.size() == 2 &&
9855 (AsmPieces[0] == "bswap" ||
9856 AsmPieces[0] == "bswapq" ||
9857 AsmPieces[0] == "bswapl") &&
9858 (AsmPieces[1] == "$0" ||
9859 AsmPieces[1] == "${0:q}")) {
9860 // No need to check constraints, nothing other than the equivalent of
9861 // "=r,0" would be valid here.
9862 return LowerToBSwap(CI);
9863 }
9864 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009865 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009866 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009867 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009868 AsmPieces[1] == "$$8," &&
9869 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009870 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9871 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009872 const std::string &Constraints = IA->getConstraintString();
9873 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +00009874 std::sort(AsmPieces.begin(), AsmPieces.end());
9875 if (AsmPieces.size() == 4 &&
9876 AsmPieces[0] == "~{cc}" &&
9877 AsmPieces[1] == "~{dirflag}" &&
9878 AsmPieces[2] == "~{flags}" &&
9879 AsmPieces[3] == "~{fpsr}") {
9880 return LowerToBSwap(CI);
9881 }
Chris Lattnerb8105652009-07-20 17:51:36 +00009882 }
9883 break;
9884 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009885 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009886 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009887 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9888 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9889 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009890 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009891 SplitString(AsmPieces[0], Words, " \t");
9892 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9893 Words.clear();
9894 SplitString(AsmPieces[1], Words, " \t");
9895 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9896 Words.clear();
9897 SplitString(AsmPieces[2], Words, " \t,");
9898 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9899 Words[2] == "%edx") {
9900 return LowerToBSwap(CI);
9901 }
9902 }
9903 }
9904 }
9905 break;
9906 }
9907 return false;
9908}
9909
9910
9911
Chris Lattnerf4dff842006-07-11 02:54:03 +00009912/// getConstraintType - Given a constraint letter, return the type of
9913/// constraint it is for this target.
9914X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009915X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9916 if (Constraint.size() == 1) {
9917 switch (Constraint[0]) {
9918 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009919 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009920 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009921 case 'r':
9922 case 'R':
9923 case 'l':
9924 case 'q':
9925 case 'Q':
9926 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009927 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009928 case 'Y':
9929 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009930 case 'e':
9931 case 'Z':
9932 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009933 default:
9934 break;
9935 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009936 }
Chris Lattner4234f572007-03-25 02:14:49 +00009937 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009938}
9939
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009940/// LowerXConstraint - try to replace an X constraint, which matches anything,
9941/// with another that has more specific requirements based on the type of the
9942/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009943const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009944LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009945 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9946 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009947 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009948 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009949 return "Y";
9950 if (Subtarget->hasSSE1())
9951 return "x";
9952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009953
Chris Lattner5e764232008-04-26 23:02:14 +00009954 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009955}
9956
Chris Lattner48884cd2007-08-25 00:47:38 +00009957/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9958/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009959void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009960 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009961 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009962 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009963 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009964 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009965
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009966 switch (Constraint) {
9967 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009968 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009969 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009970 if (C->getZExtValue() <= 31) {
9971 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009972 break;
9973 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009974 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009975 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009976 case 'J':
9977 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009978 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009979 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9980 break;
9981 }
9982 }
9983 return;
9984 case 'K':
9985 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009986 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009987 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9988 break;
9989 }
9990 }
9991 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009992 case 'N':
9993 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009994 if (C->getZExtValue() <= 255) {
9995 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009996 break;
9997 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009998 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009999 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010000 case 'e': {
10001 // 32-bit signed value
10002 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10003 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010004 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10005 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010006 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010007 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010008 break;
10009 }
10010 // FIXME gcc accepts some relocatable values here too, but only in certain
10011 // memory models; it's complicated.
10012 }
10013 return;
10014 }
10015 case 'Z': {
10016 // 32-bit unsigned value
10017 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10018 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010019 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10020 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010021 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10022 break;
10023 }
10024 }
10025 // FIXME gcc accepts some relocatable values here too, but only in certain
10026 // memory models; it's complicated.
10027 return;
10028 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010029 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010030 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010031 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010032 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010033 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010034 break;
10035 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010036
Chris Lattnerdc43a882007-05-03 16:52:29 +000010037 // If we are in non-pic codegen mode, we allow the address of a global (with
10038 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010039 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010040 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010041
Chris Lattner49921962009-05-08 18:23:14 +000010042 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10043 while (1) {
10044 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10045 Offset += GA->getOffset();
10046 break;
10047 } else if (Op.getOpcode() == ISD::ADD) {
10048 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10049 Offset += C->getZExtValue();
10050 Op = Op.getOperand(0);
10051 continue;
10052 }
10053 } else if (Op.getOpcode() == ISD::SUB) {
10054 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10055 Offset += -C->getZExtValue();
10056 Op = Op.getOperand(0);
10057 continue;
10058 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010059 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010060
Chris Lattner49921962009-05-08 18:23:14 +000010061 // Otherwise, this isn't something we can handle, reject it.
10062 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010063 }
Eric Christopherfd179292009-08-27 18:07:15 +000010064
Chris Lattner36c25012009-07-10 07:34:39 +000010065 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010066 // If we require an extra load to get this address, as in PIC mode, we
10067 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010068 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10069 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010070 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010071
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010072 if (hasMemory)
10073 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10074 else
10075 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010076 Result = Op;
10077 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010078 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010079 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010080
Gabor Greifba36cb52008-08-28 21:40:38 +000010081 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010082 Ops.push_back(Result);
10083 return;
10084 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010085 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10086 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010087}
10088
Chris Lattner259e97c2006-01-31 19:43:35 +000010089std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010090getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010091 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010092 if (Constraint.size() == 1) {
10093 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010094 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010095 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010096 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10097 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010098 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010099 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10100 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10101 X86::R10D,X86::R11D,X86::R12D,
10102 X86::R13D,X86::R14D,X86::R15D,
10103 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010104 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010105 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10106 X86::SI, X86::DI, X86::R8W,X86::R9W,
10107 X86::R10W,X86::R11W,X86::R12W,
10108 X86::R13W,X86::R14W,X86::R15W,
10109 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010110 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010111 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10112 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10113 X86::R10B,X86::R11B,X86::R12B,
10114 X86::R13B,X86::R14B,X86::R15B,
10115 X86::BPL, X86::SPL, 0);
10116
Owen Anderson825b72b2009-08-11 20:47:22 +000010117 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010118 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10119 X86::RSI, X86::RDI, X86::R8, X86::R9,
10120 X86::R10, X86::R11, X86::R12,
10121 X86::R13, X86::R14, X86::R15,
10122 X86::RBP, X86::RSP, 0);
10123
10124 break;
10125 }
Eric Christopherfd179292009-08-27 18:07:15 +000010126 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010127 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010128 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010129 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010130 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010131 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010132 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010133 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010134 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010135 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10136 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010137 }
10138 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010139
Chris Lattner1efa40f2006-02-22 00:56:39 +000010140 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010141}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010142
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010143std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010144X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010145 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010146 // First, see if this is a constraint that directly corresponds to an LLVM
10147 // register class.
10148 if (Constraint.size() == 1) {
10149 // GCC Constraint Letters
10150 switch (Constraint[0]) {
10151 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010152 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010153 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010154 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010155 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010156 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010157 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010158 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010159 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010160 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010161 case 'R': // LEGACY_REGS
10162 if (VT == MVT::i8)
10163 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10164 if (VT == MVT::i16)
10165 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10166 if (VT == MVT::i32 || !Subtarget->is64Bit())
10167 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10168 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010169 case 'f': // FP Stack registers.
10170 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10171 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010172 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010173 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010174 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010175 return std::make_pair(0U, X86::RFP64RegisterClass);
10176 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010177 case 'y': // MMX_REGS if MMX allowed.
10178 if (!Subtarget->hasMMX()) break;
10179 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010180 case 'Y': // SSE_REGS if SSE2 allowed
10181 if (!Subtarget->hasSSE2()) break;
10182 // FALL THROUGH.
10183 case 'x': // SSE_REGS if SSE1 allowed
10184 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010185
Owen Anderson825b72b2009-08-11 20:47:22 +000010186 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010187 default: break;
10188 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010189 case MVT::f32:
10190 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010191 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010192 case MVT::f64:
10193 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010194 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010195 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010196 case MVT::v16i8:
10197 case MVT::v8i16:
10198 case MVT::v4i32:
10199 case MVT::v2i64:
10200 case MVT::v4f32:
10201 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010202 return std::make_pair(0U, X86::VR128RegisterClass);
10203 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010204 break;
10205 }
10206 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010207
Chris Lattnerf76d1802006-07-31 23:26:50 +000010208 // Use the default implementation in TargetLowering to convert the register
10209 // constraint into a member of a register class.
10210 std::pair<unsigned, const TargetRegisterClass*> Res;
10211 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010212
10213 // Not found as a standard register?
10214 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010215 // Map st(0) -> st(7) -> ST0
10216 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10217 tolower(Constraint[1]) == 's' &&
10218 tolower(Constraint[2]) == 't' &&
10219 Constraint[3] == '(' &&
10220 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10221 Constraint[5] == ')' &&
10222 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010223
Chris Lattner56d77c72009-09-13 22:41:48 +000010224 Res.first = X86::ST0+Constraint[4]-'0';
10225 Res.second = X86::RFP80RegisterClass;
10226 return Res;
10227 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010228
Chris Lattner56d77c72009-09-13 22:41:48 +000010229 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010230 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010231 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010232 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010233 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010234 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010235
10236 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010237 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010238 Res.first = X86::EFLAGS;
10239 Res.second = X86::CCRRegisterClass;
10240 return Res;
10241 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010242
Dale Johannesen330169f2008-11-13 21:52:36 +000010243 // 'A' means EAX + EDX.
10244 if (Constraint == "A") {
10245 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010246 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010247 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010248 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010249 return Res;
10250 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010251
Chris Lattnerf76d1802006-07-31 23:26:50 +000010252 // Otherwise, check to see if this is a register class of the wrong value
10253 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10254 // turn into {ax},{dx}.
10255 if (Res.second->hasType(VT))
10256 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010257
Chris Lattnerf76d1802006-07-31 23:26:50 +000010258 // All of the single-register GCC register classes map their values onto
10259 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10260 // really want an 8-bit or 32-bit register, map to the appropriate register
10261 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010262 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010263 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010264 unsigned DestReg = 0;
10265 switch (Res.first) {
10266 default: break;
10267 case X86::AX: DestReg = X86::AL; break;
10268 case X86::DX: DestReg = X86::DL; break;
10269 case X86::CX: DestReg = X86::CL; break;
10270 case X86::BX: DestReg = X86::BL; break;
10271 }
10272 if (DestReg) {
10273 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010274 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010275 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010276 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010277 unsigned DestReg = 0;
10278 switch (Res.first) {
10279 default: break;
10280 case X86::AX: DestReg = X86::EAX; break;
10281 case X86::DX: DestReg = X86::EDX; break;
10282 case X86::CX: DestReg = X86::ECX; break;
10283 case X86::BX: DestReg = X86::EBX; break;
10284 case X86::SI: DestReg = X86::ESI; break;
10285 case X86::DI: DestReg = X86::EDI; break;
10286 case X86::BP: DestReg = X86::EBP; break;
10287 case X86::SP: DestReg = X86::ESP; break;
10288 }
10289 if (DestReg) {
10290 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010291 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010292 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010293 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010294 unsigned DestReg = 0;
10295 switch (Res.first) {
10296 default: break;
10297 case X86::AX: DestReg = X86::RAX; break;
10298 case X86::DX: DestReg = X86::RDX; break;
10299 case X86::CX: DestReg = X86::RCX; break;
10300 case X86::BX: DestReg = X86::RBX; break;
10301 case X86::SI: DestReg = X86::RSI; break;
10302 case X86::DI: DestReg = X86::RDI; break;
10303 case X86::BP: DestReg = X86::RBP; break;
10304 case X86::SP: DestReg = X86::RSP; break;
10305 }
10306 if (DestReg) {
10307 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010308 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010309 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010310 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010311 } else if (Res.second == X86::FR32RegisterClass ||
10312 Res.second == X86::FR64RegisterClass ||
10313 Res.second == X86::VR128RegisterClass) {
10314 // Handle references to XMM physical registers that got mapped into the
10315 // wrong class. This can happen with constraints like {xmm0} where the
10316 // target independent register mapper will just pick the first match it can
10317 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010318 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010319 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010320 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010321 Res.second = X86::FR64RegisterClass;
10322 else if (X86::VR128RegisterClass->hasType(VT))
10323 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010324 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010325
Chris Lattnerf76d1802006-07-31 23:26:50 +000010326 return Res;
10327}