blob: 7def15455f5f0ce2b28b25ba8eb0394bb5f43176 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
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.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
20#include "ARMISelLowering.h"
21#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000022#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000023#include "ARMRegisterInfo.h"
24#include "ARMSubtarget.h"
25#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000026#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Stuart Hastingsc7315872011-04-20 16:47:52 +000075// The APCS parameter registers.
76static const unsigned GPRArgRegs[] = {
77 ARM::R0, ARM::R1, ARM::R2, ARM::R3
78};
79
80static cl::opt<bool>
81UseDivMod("arm-divmod-libcall", cl::Hidden,
82 cl::desc("Use __{u}divmod libcalls for div / rem pairs"),
83 cl::init(false));
84
Owen Andersone50ed302009-08-10 22:56:29 +000085void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
86 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000087 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000088 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000089 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
90 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000091
Owen Anderson70671842009-08-10 20:18:46 +000092 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000093 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000094 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000095 }
96
Owen Andersone50ed302009-08-10 22:56:29 +000097 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000098 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000100 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000101 if (ElemTy != MVT::i32) {
102 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
104 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
105 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
106 }
Owen Anderson70671842009-08-10 20:18:46 +0000107 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
108 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000109 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000110 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000111 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000113 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000114 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
115 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000117 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
118 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000119 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
120 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
121 setTruncStoreAction(VT.getSimpleVT(),
122 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000124 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000125
126 // Promote all bit-wise operations.
127 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000128 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000129 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
130 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000131 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000132 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000133 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000134 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000135 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000136 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000137 }
Bob Wilson16330762009-09-16 00:17:28 +0000138
139 // Neon does not support vector divide/remainder operations.
140 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
141 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
142 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
143 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
144 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
145 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000146}
147
Owen Andersone50ed302009-08-10 22:56:29 +0000148void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000149 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000151}
152
Owen Andersone50ed302009-08-10 22:56:29 +0000153void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000154 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Chris Lattnerf0144122009-07-28 03:13:23 +0000158static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
159 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000160 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000161
Chris Lattner80ec2792009-08-02 00:34:36 +0000162 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000163}
164
Evan Chenga8e29892007-01-19 07:51:42 +0000165ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000166 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000167 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000168 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000169 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000170
Evan Chengb1df8f22007-04-27 08:15:43 +0000171 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000172 // Uses VFP for Thumb libfuncs if available.
173 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
174 // Single-precision floating-point arithmetic.
175 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
176 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
177 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
178 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000179
Evan Chengb1df8f22007-04-27 08:15:43 +0000180 // Double-precision floating-point arithmetic.
181 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
182 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
183 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
184 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000185
Evan Chengb1df8f22007-04-27 08:15:43 +0000186 // Single-precision comparisons.
187 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
188 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
189 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
190 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
191 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
192 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
193 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
194 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000204
Evan Chengb1df8f22007-04-27 08:15:43 +0000205 // Double-precision comparisons.
206 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
207 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
208 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
209 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
210 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
211 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
212 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
213 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000223
Evan Chengb1df8f22007-04-27 08:15:43 +0000224 // Floating-point to integer conversions.
225 // i64 conversions are done via library routines even when generating VFP
226 // instructions, so use the same ones.
227 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
228 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
229 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
230 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000231
Evan Chengb1df8f22007-04-27 08:15:43 +0000232 // Conversions between floating types.
233 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
234 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
235
236 // Integer to floating-point conversions.
237 // i64 conversions are done via library routines even when generating VFP
238 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000239 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
240 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000241 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
242 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
243 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
244 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
245 }
Evan Chenga8e29892007-01-19 07:51:42 +0000246 }
247
Bob Wilson2f954612009-05-22 17:38:41 +0000248 // These libcalls are not available in 32-bit.
249 setLibcallName(RTLIB::SHL_I128, 0);
250 setLibcallName(RTLIB::SRL_I128, 0);
251 setLibcallName(RTLIB::SRA_I128, 0);
252
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000253 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000254 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000255 // RTABI chapter 4.1.2, Table 2
256 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
257 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
258 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
259 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
260 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
261 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
262 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
263 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
264
265 // Double-precision floating-point comparison helper functions
266 // RTABI chapter 4.1.2, Table 3
267 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
268 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
269 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
270 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
271 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
272 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
273 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
274 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
275 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
276 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
277 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
278 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
280 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
281 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
282 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
283 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
284 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
285 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
286 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
287 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
288 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
291
292 // Single-precision floating-point arithmetic helper functions
293 // RTABI chapter 4.1.2, Table 4
294 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
295 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
296 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
297 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
298 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
302
303 // Single-precision floating-point comparison helper functions
304 // RTABI chapter 4.1.2, Table 5
305 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
306 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
307 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
308 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
309 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
310 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
311 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
312 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
313 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
314 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
315 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
316 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
318 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
319 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
320 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
321 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
322 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
323 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
324 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
325 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
326 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
327 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
329
330 // Floating-point to integer conversions.
331 // RTABI chapter 4.1.2, Table 6
332 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
333 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
334 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
335 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
336 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
337 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
338 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
339 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
340 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
341 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
342 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
343 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
344 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
345 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
346 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
348
349 // Conversions between floating types.
350 // RTABI chapter 4.1.2, Table 7
351 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
352 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
353 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000354 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000355
356 // Integer to floating-point conversions.
357 // RTABI chapter 4.1.2, Table 8
358 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
359 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
360 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
361 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
362 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
363 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
364 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
365 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
366 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
367 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
368 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
369 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
370 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
371 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
372 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
374
375 // Long long helper functions
376 // RTABI chapter 4.2, Table 9
377 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
378 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
379 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
380 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
381 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
382 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
383 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
386 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
389
390 // Integer division functions
391 // RTABI chapter 4.3.1
392 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
393 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
394 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
395 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
396 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
397 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
398 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
399 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
400 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
401 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
402 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000403 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000404 }
405
Evan Cheng2c69f8e2011-04-07 00:58:44 +0000406 if (HasDivModLibcall) {
Evan Cheng8e23e812011-04-01 00:42:02 +0000407 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
408 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
409 }
410
David Goodwinf1daf7d2009-07-08 23:10:31 +0000411 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000413 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000415 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000417 if (!Subtarget->isFPOnlySP())
418 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000419
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000421 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000422
423 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 addDRTypeForNEON(MVT::v2f32);
425 addDRTypeForNEON(MVT::v8i8);
426 addDRTypeForNEON(MVT::v4i16);
427 addDRTypeForNEON(MVT::v2i32);
428 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000429
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 addQRTypeForNEON(MVT::v4f32);
431 addQRTypeForNEON(MVT::v2f64);
432 addQRTypeForNEON(MVT::v16i8);
433 addQRTypeForNEON(MVT::v8i16);
434 addQRTypeForNEON(MVT::v4i32);
435 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000436
Bob Wilson74dc72e2009-09-15 23:55:57 +0000437 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
438 // neither Neon nor VFP support any arithmetic operations on it.
439 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
440 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
441 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
442 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
443 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
444 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
445 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
446 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
447 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
448 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
449 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
450 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
451 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
452 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
453 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
454 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
455 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
456 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
457 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
458 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
459 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
460 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
461 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
462 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
463
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000464 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
465
Bob Wilson642b3292009-09-16 00:32:15 +0000466 // Neon does not support some operations on v1i64 and v2i64 types.
467 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000468 // Custom handling for some quad-vector types to detect VMULL.
469 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
470 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
471 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000472 // Custom handling for some vector types to avoid expensive expansions
473 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
474 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
475 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
476 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000477 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
478 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000479 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
480 // a destination type that is wider than the source.
481 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
482 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000483
Bob Wilson1c3ef902011-02-07 17:43:21 +0000484 setTargetDAGCombine(ISD::INTRINSIC_VOID);
485 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000486 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
487 setTargetDAGCombine(ISD::SHL);
488 setTargetDAGCombine(ISD::SRL);
489 setTargetDAGCombine(ISD::SRA);
490 setTargetDAGCombine(ISD::SIGN_EXTEND);
491 setTargetDAGCombine(ISD::ZERO_EXTEND);
492 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000493 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000494 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000495 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000496 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
497 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000498 }
499
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000500 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000501
502 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000503 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000504
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000505 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000507
Evan Chenga8e29892007-01-19 07:51:42 +0000508 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000509 if (!Subtarget->isThumb1Only()) {
510 for (unsigned im = (unsigned)ISD::PRE_INC;
511 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setIndexedLoadAction(im, MVT::i1, Legal);
513 setIndexedLoadAction(im, MVT::i8, Legal);
514 setIndexedLoadAction(im, MVT::i16, Legal);
515 setIndexedLoadAction(im, MVT::i32, Legal);
516 setIndexedStoreAction(im, MVT::i1, Legal);
517 setIndexedStoreAction(im, MVT::i8, Legal);
518 setIndexedStoreAction(im, MVT::i16, Legal);
519 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000520 }
Evan Chenga8e29892007-01-19 07:51:42 +0000521 }
522
523 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000524 setOperationAction(ISD::MUL, MVT::i64, Expand);
525 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000526 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
528 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000529 }
Eric Christopher2cc40132011-04-19 18:49:19 +0000530 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops())
531 setOperationAction(ISD::MULHS, MVT::i32, Expand);
532
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000533 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000534 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000535 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::SRL, MVT::i64, Custom);
537 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000538
539 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000541 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000543 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000545
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000546 // Only ARMv6 has BSWAP.
547 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000548 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000549
Evan Chenga8e29892007-01-19 07:51:42 +0000550 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000551 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000552 // v7M has a hardware divider
553 setOperationAction(ISD::SDIV, MVT::i32, Expand);
554 setOperationAction(ISD::UDIV, MVT::i32, Expand);
555 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 setOperationAction(ISD::SREM, MVT::i32, Expand);
557 setOperationAction(ISD::UREM, MVT::i32, Expand);
558 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
559 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000560
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
562 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
563 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
564 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000565 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000566
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000567 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000568
Evan Chenga8e29892007-01-19 07:51:42 +0000569 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setOperationAction(ISD::VASTART, MVT::Other, Custom);
571 setOperationAction(ISD::VAARG, MVT::Other, Expand);
572 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
573 setOperationAction(ISD::VAEND, MVT::Other, Expand);
574 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
575 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000576 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000577 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
578 setExceptionPointerRegister(ARM::R0);
579 setExceptionSelectorRegister(ARM::R1);
580
Evan Cheng3a1588a2010-04-15 22:20:34 +0000581 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000582 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
583 // the default expansion.
584 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000585 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000586 // membarrier needs custom lowering; the rest are legal and handled
587 // normally.
588 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
589 } else {
590 // Set them all for expansion, which will force libcalls.
591 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
592 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
593 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
594 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000595 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
596 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
597 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000598 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
606 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
607 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
608 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
609 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
612 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000616 // Since the libcalls include locking, fold in the fences
617 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000618 }
619 // 64-bit versions are always libcalls (for now)
620 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000621 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000622 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
624 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
625 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
626 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
627 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000628
Evan Cheng416941d2010-11-04 05:19:35 +0000629 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000630
Eli Friedmana2c6f452010-06-26 04:36:50 +0000631 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
632 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
634 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000635 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000637
Nate Begemand1fb5832010-08-03 21:31:55 +0000638 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000639 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
640 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000641 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000642 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
643 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000644
645 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000647 if (Subtarget->isTargetDarwin()) {
648 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
649 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000650 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000651 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::SETCC, MVT::i32, Expand);
654 setOperationAction(ISD::SETCC, MVT::f32, Expand);
655 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000656 setOperationAction(ISD::SELECT, MVT::i32, Custom);
657 setOperationAction(ISD::SELECT, MVT::f32, Custom);
658 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
660 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
661 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000662
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
664 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
665 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
666 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
667 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000668
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000669 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FSIN, MVT::f64, Expand);
671 setOperationAction(ISD::FSIN, MVT::f32, Expand);
672 setOperationAction(ISD::FCOS, MVT::f32, Expand);
673 setOperationAction(ISD::FCOS, MVT::f64, Expand);
674 setOperationAction(ISD::FREM, MVT::f64, Expand);
675 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000676 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
678 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000679 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::FPOW, MVT::f64, Expand);
681 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000682
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000683 // Various VFP goodness
684 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000685 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
686 if (Subtarget->hasVFP2()) {
687 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
688 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
689 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
690 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
691 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000692 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000693 if (!Subtarget->hasFP16()) {
694 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
695 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000696 }
Evan Cheng110cf482008-04-01 01:50:16 +0000697 }
Evan Chenga8e29892007-01-19 07:51:42 +0000698
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000699 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000700 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000701 setTargetDAGCombine(ISD::ADD);
702 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000703 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000704
Owen Anderson080c0922010-11-05 19:27:46 +0000705 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000706 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000707 if (Subtarget->hasNEON())
708 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000709
Evan Chenga8e29892007-01-19 07:51:42 +0000710 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000711
Evan Chengf7d87ee2010-05-21 00:43:17 +0000712 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
713 setSchedulingPreference(Sched::RegPressure);
714 else
715 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000716
Evan Cheng05219282011-01-06 06:52:41 +0000717 //// temporary - rewrite interface to use type
718 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000719
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000720 // On ARM arguments smaller than 4 bytes are extended, so all arguments
721 // are at least 4 bytes aligned.
722 setMinStackArgumentAlignment(4);
723
Evan Chengfff606d2010-09-24 19:07:23 +0000724 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000725}
726
Andrew Trick32cec0a2011-01-19 02:35:27 +0000727// FIXME: It might make sense to define the representative register class as the
728// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
729// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
730// SPR's representative would be DPR_VFP2. This should work well if register
731// pressure tracking were modified such that a register use would increment the
732// pressure of the register class's representative and all of it's super
733// classes' representatives transitively. We have not implemented this because
734// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000735// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000736// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000737std::pair<const TargetRegisterClass*, uint8_t>
738ARMTargetLowering::findRepresentativeClass(EVT VT) const{
739 const TargetRegisterClass *RRC = 0;
740 uint8_t Cost = 1;
741 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000742 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000743 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000744 // Use DPR as representative register class for all floating point
745 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
746 // the cost is 1 for both f32 and f64.
747 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000748 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000749 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000750 // When NEON is used for SP, only half of the register file is available
751 // because operations that define both SP and DP results will be constrained
752 // to the VFP2 class (D0-D15). We currently model this constraint prior to
753 // coalescing by double-counting the SP regs. See the FIXME above.
754 if (Subtarget->useNEONForSinglePrecisionFP())
755 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000756 break;
757 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
758 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000759 RRC = ARM::DPRRegisterClass;
760 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000761 break;
762 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000763 RRC = ARM::DPRRegisterClass;
764 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000765 break;
766 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000767 RRC = ARM::DPRRegisterClass;
768 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000769 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000770 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000771 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000772}
773
Evan Chenga8e29892007-01-19 07:51:42 +0000774const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
775 switch (Opcode) {
776 default: return 0;
777 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000778 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000779 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000780 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
781 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000782 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000783 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
784 case ARMISD::tCALL: return "ARMISD::tCALL";
785 case ARMISD::BRCOND: return "ARMISD::BRCOND";
786 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000787 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000788 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
789 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
790 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000791 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000792 case ARMISD::CMPFP: return "ARMISD::CMPFP";
793 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000794 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000795 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
796 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000797
Jim Grosbach3482c802010-01-18 19:58:49 +0000798 case ARMISD::RBIT: return "ARMISD::RBIT";
799
Bob Wilson76a312b2010-03-19 22:51:32 +0000800 case ARMISD::FTOSI: return "ARMISD::FTOSI";
801 case ARMISD::FTOUI: return "ARMISD::FTOUI";
802 case ARMISD::SITOF: return "ARMISD::SITOF";
803 case ARMISD::UITOF: return "ARMISD::UITOF";
804
Evan Chenga8e29892007-01-19 07:51:42 +0000805 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
806 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
807 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000808
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000809 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
810 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000811
Evan Chengc5942082009-10-28 06:55:03 +0000812 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
813 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000814 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000815
Dale Johannesen51e28e62010-06-03 21:09:53 +0000816 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000817
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000818 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000819
Evan Cheng86198642009-08-07 00:34:42 +0000820 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
821
Jim Grosbach3728e962009-12-10 00:11:09 +0000822 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000823 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000824
Evan Chengdfed19f2010-11-03 06:34:55 +0000825 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
826
Bob Wilson5bafff32009-06-22 23:27:02 +0000827 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000828 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000829 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000830 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
831 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000832 case ARMISD::VCGEU: return "ARMISD::VCGEU";
833 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000834 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
835 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000836 case ARMISD::VCGTU: return "ARMISD::VCGTU";
837 case ARMISD::VTST: return "ARMISD::VTST";
838
839 case ARMISD::VSHL: return "ARMISD::VSHL";
840 case ARMISD::VSHRs: return "ARMISD::VSHRs";
841 case ARMISD::VSHRu: return "ARMISD::VSHRu";
842 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
843 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
844 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
845 case ARMISD::VSHRN: return "ARMISD::VSHRN";
846 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
847 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
848 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
849 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
850 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
851 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
852 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
853 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
854 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
855 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
856 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
857 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
858 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
859 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000860 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000861 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000862 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000863 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000864 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000865 case ARMISD::VREV64: return "ARMISD::VREV64";
866 case ARMISD::VREV32: return "ARMISD::VREV32";
867 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000868 case ARMISD::VZIP: return "ARMISD::VZIP";
869 case ARMISD::VUZP: return "ARMISD::VUZP";
870 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000871 case ARMISD::VTBL1: return "ARMISD::VTBL1";
872 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000873 case ARMISD::VMULLs: return "ARMISD::VMULLs";
874 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000875 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000876 case ARMISD::FMAX: return "ARMISD::FMAX";
877 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000878 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000879 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
880 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000881 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000882 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
883 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
884 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000885 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
886 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
887 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
888 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
889 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
890 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
891 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
892 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
893 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
894 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
895 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
896 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
897 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
898 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
899 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
900 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
901 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000902 }
903}
904
Evan Cheng06b666c2010-05-15 02:18:07 +0000905/// getRegClassFor - Return the register class that should be used for the
906/// specified value type.
907TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
908 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
909 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
910 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000911 if (Subtarget->hasNEON()) {
912 if (VT == MVT::v4i64)
913 return ARM::QQPRRegisterClass;
914 else if (VT == MVT::v8i64)
915 return ARM::QQQQPRRegisterClass;
916 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000917 return TargetLowering::getRegClassFor(VT);
918}
919
Eric Christopherab695882010-07-21 22:26:11 +0000920// Create a fast isel object.
921FastISel *
922ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
923 return ARM::createFastISel(funcInfo);
924}
925
Bill Wendlingb4202b82009-07-01 18:50:55 +0000926/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000927unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000928 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000929}
930
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000931/// getMaximalGlobalOffset - Returns the maximal possible offset which can
932/// be used for loads / stores from the global.
933unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
934 return (Subtarget->isThumb1Only() ? 127 : 4095);
935}
936
Evan Cheng1cc39842010-05-20 23:26:43 +0000937Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000938 unsigned NumVals = N->getNumValues();
939 if (!NumVals)
940 return Sched::RegPressure;
941
942 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000943 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000944 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000945 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000946 if (VT.isFloatingPoint() || VT.isVector())
947 return Sched::Latency;
948 }
Evan Chengc10f5432010-05-28 23:25:23 +0000949
950 if (!N->isMachineOpcode())
951 return Sched::RegPressure;
952
953 // Load are scheduled for latency even if there instruction itinerary
954 // is not available.
955 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
956 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000957
958 if (TID.getNumDefs() == 0)
959 return Sched::RegPressure;
960 if (!Itins->isEmpty() &&
961 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000962 return Sched::Latency;
963
Evan Cheng1cc39842010-05-20 23:26:43 +0000964 return Sched::RegPressure;
965}
966
Evan Chenga8e29892007-01-19 07:51:42 +0000967//===----------------------------------------------------------------------===//
968// Lowering Code
969//===----------------------------------------------------------------------===//
970
Evan Chenga8e29892007-01-19 07:51:42 +0000971/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
972static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
973 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000974 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000975 case ISD::SETNE: return ARMCC::NE;
976 case ISD::SETEQ: return ARMCC::EQ;
977 case ISD::SETGT: return ARMCC::GT;
978 case ISD::SETGE: return ARMCC::GE;
979 case ISD::SETLT: return ARMCC::LT;
980 case ISD::SETLE: return ARMCC::LE;
981 case ISD::SETUGT: return ARMCC::HI;
982 case ISD::SETUGE: return ARMCC::HS;
983 case ISD::SETULT: return ARMCC::LO;
984 case ISD::SETULE: return ARMCC::LS;
985 }
986}
987
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000988/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
989static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000990 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000991 CondCode2 = ARMCC::AL;
992 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000993 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000994 case ISD::SETEQ:
995 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
996 case ISD::SETGT:
997 case ISD::SETOGT: CondCode = ARMCC::GT; break;
998 case ISD::SETGE:
999 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1000 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001001 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001002 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1003 case ISD::SETO: CondCode = ARMCC::VC; break;
1004 case ISD::SETUO: CondCode = ARMCC::VS; break;
1005 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1006 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1007 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1008 case ISD::SETLT:
1009 case ISD::SETULT: CondCode = ARMCC::LT; break;
1010 case ISD::SETLE:
1011 case ISD::SETULE: CondCode = ARMCC::LE; break;
1012 case ISD::SETNE:
1013 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1014 }
Evan Chenga8e29892007-01-19 07:51:42 +00001015}
1016
Bob Wilson1f595bb2009-04-17 19:07:39 +00001017//===----------------------------------------------------------------------===//
1018// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001019//===----------------------------------------------------------------------===//
1020
1021#include "ARMGenCallingConv.inc"
1022
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001023/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1024/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001025CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001026 bool Return,
1027 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001028 switch (CC) {
1029 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001030 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001031 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001032 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001033 if (!Subtarget->isAAPCS_ABI())
1034 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1035 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1036 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1037 }
1038 // Fallthrough
1039 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001040 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001041 if (!Subtarget->isAAPCS_ABI())
1042 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1043 else if (Subtarget->hasVFP2() &&
1044 FloatABIType == FloatABI::Hard && !isVarArg)
1045 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1046 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1047 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001048 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001049 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001050 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001051 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001052 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001053 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001054 }
1055}
1056
Dan Gohman98ca4f22009-08-05 01:29:28 +00001057/// LowerCallResult - Lower the result values of a call into the
1058/// appropriate copies out of appropriate physical registers.
1059SDValue
1060ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001061 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001062 const SmallVectorImpl<ISD::InputArg> &Ins,
1063 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001064 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001065
Bob Wilson1f595bb2009-04-17 19:07:39 +00001066 // Assign locations to each value returned by this call.
1067 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001068 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001069 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001070 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001071 CCAssignFnForNode(CallConv, /* Return*/ true,
1072 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001073
1074 // Copy all of the result registers out of their specified physreg.
1075 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1076 CCValAssign VA = RVLocs[i];
1077
Bob Wilson80915242009-04-25 00:33:20 +00001078 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001079 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001080 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001081 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001082 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001083 Chain = Lo.getValue(1);
1084 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001085 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001087 InFlag);
1088 Chain = Hi.getValue(1);
1089 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001090 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001091
Owen Anderson825b72b2009-08-11 20:47:22 +00001092 if (VA.getLocVT() == MVT::v2f64) {
1093 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1094 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1095 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001096
1097 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001098 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001099 Chain = Lo.getValue(1);
1100 InFlag = Lo.getValue(2);
1101 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001103 Chain = Hi.getValue(1);
1104 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001105 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001106 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1107 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001108 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001110 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1111 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001112 Chain = Val.getValue(1);
1113 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001114 }
Bob Wilson80915242009-04-25 00:33:20 +00001115
1116 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001117 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001118 case CCValAssign::Full: break;
1119 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001120 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001121 break;
1122 }
1123
Dan Gohman98ca4f22009-08-05 01:29:28 +00001124 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001125 }
1126
Dan Gohman98ca4f22009-08-05 01:29:28 +00001127 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001128}
1129
Bob Wilsondee46d72009-04-17 20:35:10 +00001130/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001132ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1133 SDValue StackPtr, SDValue Arg,
1134 DebugLoc dl, SelectionDAG &DAG,
1135 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001136 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137 unsigned LocMemOffset = VA.getLocMemOffset();
1138 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1139 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001140 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001141 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001142 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001143}
1144
Dan Gohman98ca4f22009-08-05 01:29:28 +00001145void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001146 SDValue Chain, SDValue &Arg,
1147 RegsToPassVector &RegsToPass,
1148 CCValAssign &VA, CCValAssign &NextVA,
1149 SDValue &StackPtr,
1150 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001151 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001152
Jim Grosbache5165492009-11-09 00:11:35 +00001153 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001155 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1156
1157 if (NextVA.isRegLoc())
1158 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1159 else {
1160 assert(NextVA.isMemLoc());
1161 if (StackPtr.getNode() == 0)
1162 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1163
Dan Gohman98ca4f22009-08-05 01:29:28 +00001164 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1165 dl, DAG, NextVA,
1166 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001167 }
1168}
1169
Dan Gohman98ca4f22009-08-05 01:29:28 +00001170/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001171/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1172/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001173SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001174ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001175 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001176 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001178 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179 const SmallVectorImpl<ISD::InputArg> &Ins,
1180 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001181 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001182 MachineFunction &MF = DAG.getMachineFunction();
1183 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1184 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001185 // Temporarily disable tail calls so things don't break.
1186 if (!EnableARMTailCalls)
1187 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001188 if (isTailCall) {
1189 // Check if it's really possible to do a tail call.
1190 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1191 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001192 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001193 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1194 // detected sibcalls.
1195 if (isTailCall) {
1196 ++NumTailCalls;
1197 IsSibCall = true;
1198 }
1199 }
Evan Chenga8e29892007-01-19 07:51:42 +00001200
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201 // Analyze operands of the call, assigning locations to each operand.
1202 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001203 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1204 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00001205 CCInfo.setCallOrPrologue(Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001207 CCAssignFnForNode(CallConv, /* Return*/ false,
1208 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001209
Bob Wilson1f595bb2009-04-17 19:07:39 +00001210 // Get a count of how many bytes are to be pushed on the stack.
1211 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001212
Dale Johannesen51e28e62010-06-03 21:09:53 +00001213 // For tail calls, memory operands are available in our caller's stack.
1214 if (IsSibCall)
1215 NumBytes = 0;
1216
Evan Chenga8e29892007-01-19 07:51:42 +00001217 // Adjust the stack pointer for the new arguments...
1218 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001219 if (!IsSibCall)
1220 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001221
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001222 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001223
Bob Wilson5bafff32009-06-22 23:27:02 +00001224 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001225 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001226
Bob Wilson1f595bb2009-04-17 19:07:39 +00001227 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001228 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001229 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1230 i != e;
1231 ++i, ++realArgIdx) {
1232 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001233 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001235 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001236
Bob Wilson1f595bb2009-04-17 19:07:39 +00001237 // Promote the value if needed.
1238 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001239 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001240 case CCValAssign::Full: break;
1241 case CCValAssign::SExt:
1242 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1243 break;
1244 case CCValAssign::ZExt:
1245 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1246 break;
1247 case CCValAssign::AExt:
1248 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1249 break;
1250 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001251 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001253 }
1254
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001255 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 if (VA.getLocVT() == MVT::v2f64) {
1258 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1259 DAG.getConstant(0, MVT::i32));
1260 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1261 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262
Dan Gohman98ca4f22009-08-05 01:29:28 +00001263 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001264 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1265
1266 VA = ArgLocs[++i]; // skip ahead to next loc
1267 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001268 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001269 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1270 } else {
1271 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001272
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1274 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001275 }
1276 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001277 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001278 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279 }
1280 } else if (VA.isRegLoc()) {
1281 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001282 } else if (isByVal) {
1283 assert(VA.isMemLoc());
1284 unsigned offset = 0;
1285
1286 // True if this byval aggregate will be split between registers
1287 // and memory.
1288 if (CCInfo.isFirstByValRegValid()) {
1289 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1290 unsigned int i, j;
1291 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1292 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1293 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1294 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1295 MachinePointerInfo(),
1296 false, false, 0);
1297 MemOpChains.push_back(Load.getValue(1));
1298 RegsToPass.push_back(std::make_pair(j, Load));
1299 }
1300 offset = ARM::R4 - CCInfo.getFirstByValReg();
1301 CCInfo.clearFirstByValReg();
1302 }
1303
1304 unsigned LocMemOffset = VA.getLocMemOffset();
1305 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1306 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1307 StkPtrOff);
1308 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1309 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1310 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1311 MVT::i32);
1312 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1313 Flags.getByValAlign(),
1314 /*isVolatile=*/false,
1315 /*AlwaysInline=*/false,
1316 MachinePointerInfo(0),
1317 MachinePointerInfo(0)));
1318
1319 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001320 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001321
Dan Gohman98ca4f22009-08-05 01:29:28 +00001322 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1323 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001324 }
Evan Chenga8e29892007-01-19 07:51:42 +00001325 }
1326
1327 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001328 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001329 &MemOpChains[0], MemOpChains.size());
1330
1331 // Build a sequence of copy-to-reg nodes chained together with token chain
1332 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001333 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001334 // Tail call byval lowering might overwrite argument registers so in case of
1335 // tail call optimization the copies to registers are lowered later.
1336 if (!isTailCall)
1337 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1338 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1339 RegsToPass[i].second, InFlag);
1340 InFlag = Chain.getValue(1);
1341 }
Evan Chenga8e29892007-01-19 07:51:42 +00001342
Dale Johannesen51e28e62010-06-03 21:09:53 +00001343 // For tail calls lower the arguments to the 'real' stack slot.
1344 if (isTailCall) {
1345 // Force all the incoming stack arguments to be loaded from the stack
1346 // before any new outgoing arguments are stored to the stack, because the
1347 // outgoing stack slots may alias the incoming argument stack slots, and
1348 // the alias isn't otherwise explicit. This is slightly more conservative
1349 // than necessary, because it means that each store effectively depends
1350 // on every argument instead of just those arguments it would clobber.
1351
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001352 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001353 InFlag = SDValue();
1354 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1355 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1356 RegsToPass[i].second, InFlag);
1357 InFlag = Chain.getValue(1);
1358 }
1359 InFlag =SDValue();
1360 }
1361
Bill Wendling056292f2008-09-16 21:48:12 +00001362 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1363 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1364 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001365 bool isDirect = false;
1366 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001367 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001368 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001369
1370 if (EnableARMLongCalls) {
1371 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1372 && "long-calls with non-static relocation model!");
1373 // Handle a global address or an external symbol. If it's not one of
1374 // those, the target's already in a register, so we don't need to do
1375 // anything extra.
1376 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001377 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001378 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001379 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001380 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1381 ARMPCLabelIndex,
1382 ARMCP::CPValue, 0);
1383 // Get the address of the callee into a register
1384 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1385 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1386 Callee = DAG.getLoad(getPointerTy(), dl,
1387 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001388 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001389 false, false, 0);
1390 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1391 const char *Sym = S->getSymbol();
1392
1393 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001394 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001395 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1396 Sym, ARMPCLabelIndex, 0);
1397 // Get the address of the callee into a register
1398 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1399 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1400 Callee = DAG.getLoad(getPointerTy(), dl,
1401 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001402 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001403 false, false, 0);
1404 }
1405 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001406 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001407 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001408 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001409 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001410 getTargetMachine().getRelocationModel() != Reloc::Static;
1411 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001412 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001413 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001414 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001415 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001416 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001417 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001418 ARMPCLabelIndex,
1419 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001420 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001421 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001422 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001423 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001424 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001425 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001426 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001427 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001428 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001429 } else {
1430 // On ELF targets for PIC code, direct calls should go through the PLT
1431 unsigned OpFlags = 0;
1432 if (Subtarget->isTargetELF() &&
1433 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1434 OpFlags = ARMII::MO_PLT;
1435 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1436 }
Bill Wendling056292f2008-09-16 21:48:12 +00001437 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001438 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001439 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001440 getTargetMachine().getRelocationModel() != Reloc::Static;
1441 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001442 // tBX takes a register source operand.
1443 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001444 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001445 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001446 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001447 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001448 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001449 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001450 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001451 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001452 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001453 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001454 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001455 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001456 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001457 } else {
1458 unsigned OpFlags = 0;
1459 // On ELF targets for PIC code, direct calls should go through the PLT
1460 if (Subtarget->isTargetELF() &&
1461 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1462 OpFlags = ARMII::MO_PLT;
1463 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1464 }
Evan Chenga8e29892007-01-19 07:51:42 +00001465 }
1466
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001467 // FIXME: handle tail calls differently.
1468 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001469 if (Subtarget->isThumb()) {
1470 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001471 CallOpc = ARMISD::CALL_NOLINK;
1472 else
1473 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1474 } else {
1475 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001476 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1477 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001478 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001479
Dan Gohman475871a2008-07-27 21:46:04 +00001480 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001481 Ops.push_back(Chain);
1482 Ops.push_back(Callee);
1483
1484 // Add argument registers to the end of the list so that they are known live
1485 // into the call.
1486 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1487 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1488 RegsToPass[i].second.getValueType()));
1489
Gabor Greifba36cb52008-08-28 21:40:38 +00001490 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001491 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001492
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001493 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001494 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001495 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001496
Duncan Sands4bdcb612008-07-02 17:40:58 +00001497 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001498 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001499 InFlag = Chain.getValue(1);
1500
Chris Lattnere563bbc2008-10-11 22:08:30 +00001501 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1502 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001503 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001504 InFlag = Chain.getValue(1);
1505
Bob Wilson1f595bb2009-04-17 19:07:39 +00001506 // Handle result values, copying them out of physregs into vregs that we
1507 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1509 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001510}
1511
Stuart Hastingsf222e592011-02-28 17:17:53 +00001512/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001513/// on the stack. Remember the next parameter register to allocate,
1514/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001515/// this.
1516void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001517llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1518 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1519 assert((State->getCallOrPrologue() == Prologue ||
1520 State->getCallOrPrologue() == Call) &&
1521 "unhandled ParmContext");
1522 if ((!State->isFirstByValRegValid()) &&
1523 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1524 State->setFirstByValReg(reg);
1525 // At a call site, a byval parameter that is split between
1526 // registers and memory needs its size truncated here. In a
1527 // function prologue, such byval parameters are reassembled in
1528 // memory, and are not truncated.
1529 if (State->getCallOrPrologue() == Call) {
1530 unsigned excess = 4 * (ARM::R4 - reg);
1531 assert(size >= excess && "expected larger existing stack allocation");
1532 size -= excess;
1533 }
1534 }
1535 // Confiscate any remaining parameter registers to preclude their
1536 // assignment to subsequent parameters.
1537 while (State->AllocateReg(GPRArgRegs, 4))
1538 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001539}
1540
Dale Johannesen51e28e62010-06-03 21:09:53 +00001541/// MatchingStackOffset - Return true if the given stack call argument is
1542/// already available in the same position (relatively) of the caller's
1543/// incoming argument stack.
1544static
1545bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1546 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1547 const ARMInstrInfo *TII) {
1548 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1549 int FI = INT_MAX;
1550 if (Arg.getOpcode() == ISD::CopyFromReg) {
1551 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001552 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001553 return false;
1554 MachineInstr *Def = MRI->getVRegDef(VR);
1555 if (!Def)
1556 return false;
1557 if (!Flags.isByVal()) {
1558 if (!TII->isLoadFromStackSlot(Def, FI))
1559 return false;
1560 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001561 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001562 }
1563 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1564 if (Flags.isByVal())
1565 // ByVal argument is passed in as a pointer but it's now being
1566 // dereferenced. e.g.
1567 // define @foo(%struct.X* %A) {
1568 // tail call @bar(%struct.X* byval %A)
1569 // }
1570 return false;
1571 SDValue Ptr = Ld->getBasePtr();
1572 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1573 if (!FINode)
1574 return false;
1575 FI = FINode->getIndex();
1576 } else
1577 return false;
1578
1579 assert(FI != INT_MAX);
1580 if (!MFI->isFixedObjectIndex(FI))
1581 return false;
1582 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1583}
1584
1585/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1586/// for tail call optimization. Targets which want to do tail call
1587/// optimization should implement this function.
1588bool
1589ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1590 CallingConv::ID CalleeCC,
1591 bool isVarArg,
1592 bool isCalleeStructRet,
1593 bool isCallerStructRet,
1594 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001595 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001596 const SmallVectorImpl<ISD::InputArg> &Ins,
1597 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001598 const Function *CallerF = DAG.getMachineFunction().getFunction();
1599 CallingConv::ID CallerCC = CallerF->getCallingConv();
1600 bool CCMatch = CallerCC == CalleeCC;
1601
1602 // Look for obvious safe cases to perform tail call optimization that do not
1603 // require ABI changes. This is what gcc calls sibcall.
1604
Jim Grosbach7616b642010-06-16 23:45:49 +00001605 // Do not sibcall optimize vararg calls unless the call site is not passing
1606 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001607 if (isVarArg && !Outs.empty())
1608 return false;
1609
1610 // Also avoid sibcall optimization if either caller or callee uses struct
1611 // return semantics.
1612 if (isCalleeStructRet || isCallerStructRet)
1613 return false;
1614
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001615 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001616 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001617 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1618 // LR. This means if we need to reload LR, it takes an extra instructions,
1619 // which outweighs the value of the tail call; but here we don't know yet
1620 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001621 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001622 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001623
1624 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1625 // but we need to make sure there are enough registers; the only valid
1626 // registers are the 4 used for parameters. We don't currently do this
1627 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001628 if (Subtarget->isThumb1Only())
1629 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001630
Dale Johannesen51e28e62010-06-03 21:09:53 +00001631 // If the calling conventions do not match, then we'd better make sure the
1632 // results are returned in the same way as what the caller expects.
1633 if (!CCMatch) {
1634 SmallVector<CCValAssign, 16> RVLocs1;
1635 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1636 RVLocs1, *DAG.getContext());
1637 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1638
1639 SmallVector<CCValAssign, 16> RVLocs2;
1640 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1641 RVLocs2, *DAG.getContext());
1642 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1643
1644 if (RVLocs1.size() != RVLocs2.size())
1645 return false;
1646 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1647 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1648 return false;
1649 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1650 return false;
1651 if (RVLocs1[i].isRegLoc()) {
1652 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1653 return false;
1654 } else {
1655 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1656 return false;
1657 }
1658 }
1659 }
1660
1661 // If the callee takes no arguments then go on to check the results of the
1662 // call.
1663 if (!Outs.empty()) {
1664 // Check if stack adjustment is needed. For now, do not do this if any
1665 // argument is passed on the stack.
1666 SmallVector<CCValAssign, 16> ArgLocs;
1667 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1668 ArgLocs, *DAG.getContext());
1669 CCInfo.AnalyzeCallOperands(Outs,
1670 CCAssignFnForNode(CalleeCC, false, isVarArg));
1671 if (CCInfo.getNextStackOffset()) {
1672 MachineFunction &MF = DAG.getMachineFunction();
1673
1674 // Check if the arguments are already laid out in the right way as
1675 // the caller's fixed stack objects.
1676 MachineFrameInfo *MFI = MF.getFrameInfo();
1677 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1678 const ARMInstrInfo *TII =
1679 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001680 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1681 i != e;
1682 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001683 CCValAssign &VA = ArgLocs[i];
1684 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001685 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001686 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001687 if (VA.getLocInfo() == CCValAssign::Indirect)
1688 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001689 if (VA.needsCustom()) {
1690 // f64 and vector types are split into multiple registers or
1691 // register/stack-slot combinations. The types will not match
1692 // the registers; give up on memory f64 refs until we figure
1693 // out what to do about this.
1694 if (!VA.isRegLoc())
1695 return false;
1696 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001697 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001698 if (RegVT == MVT::v2f64) {
1699 if (!ArgLocs[++i].isRegLoc())
1700 return false;
1701 if (!ArgLocs[++i].isRegLoc())
1702 return false;
1703 }
1704 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001705 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1706 MFI, MRI, TII))
1707 return false;
1708 }
1709 }
1710 }
1711 }
1712
1713 return true;
1714}
1715
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716SDValue
1717ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001718 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001720 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001721 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001722
Bob Wilsondee46d72009-04-17 20:35:10 +00001723 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001724 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001725
Bob Wilsondee46d72009-04-17 20:35:10 +00001726 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001727 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1728 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001729
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001731 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1732 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001733
1734 // If this is the first return lowered for this function, add
1735 // the regs to the liveout set for the function.
1736 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1737 for (unsigned i = 0; i != RVLocs.size(); ++i)
1738 if (RVLocs[i].isRegLoc())
1739 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001740 }
1741
Bob Wilson1f595bb2009-04-17 19:07:39 +00001742 SDValue Flag;
1743
1744 // Copy the result values into the output registers.
1745 for (unsigned i = 0, realRVLocIdx = 0;
1746 i != RVLocs.size();
1747 ++i, ++realRVLocIdx) {
1748 CCValAssign &VA = RVLocs[i];
1749 assert(VA.isRegLoc() && "Can only return in registers!");
1750
Dan Gohmanc9403652010-07-07 15:54:55 +00001751 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001752
1753 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001754 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001755 case CCValAssign::Full: break;
1756 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001757 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001758 break;
1759 }
1760
Bob Wilson1f595bb2009-04-17 19:07:39 +00001761 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001763 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001764 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1765 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001766 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001768
1769 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1770 Flag = Chain.getValue(1);
1771 VA = RVLocs[++i]; // skip ahead to next loc
1772 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1773 HalfGPRs.getValue(1), Flag);
1774 Flag = Chain.getValue(1);
1775 VA = RVLocs[++i]; // skip ahead to next loc
1776
1777 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1779 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001780 }
1781 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1782 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001783 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001785 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001786 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001787 VA = RVLocs[++i]; // skip ahead to next loc
1788 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1789 Flag);
1790 } else
1791 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1792
Bob Wilsondee46d72009-04-17 20:35:10 +00001793 // Guarantee that all emitted copies are
1794 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001795 Flag = Chain.getValue(1);
1796 }
1797
1798 SDValue result;
1799 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001800 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001801 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001802 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001803
1804 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001805}
1806
Evan Cheng3d2125c2010-11-30 23:55:39 +00001807bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1808 if (N->getNumValues() != 1)
1809 return false;
1810 if (!N->hasNUsesOfValue(1, 0))
1811 return false;
1812
1813 unsigned NumCopies = 0;
1814 SDNode* Copies[2];
1815 SDNode *Use = *N->use_begin();
1816 if (Use->getOpcode() == ISD::CopyToReg) {
1817 Copies[NumCopies++] = Use;
1818 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1819 // f64 returned in a pair of GPRs.
1820 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1821 UI != UE; ++UI) {
1822 if (UI->getOpcode() != ISD::CopyToReg)
1823 return false;
1824 Copies[UI.getUse().getResNo()] = *UI;
1825 ++NumCopies;
1826 }
1827 } else if (Use->getOpcode() == ISD::BITCAST) {
1828 // f32 returned in a single GPR.
1829 if (!Use->hasNUsesOfValue(1, 0))
1830 return false;
1831 Use = *Use->use_begin();
1832 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1833 return false;
1834 Copies[NumCopies++] = Use;
1835 } else {
1836 return false;
1837 }
1838
1839 if (NumCopies != 1 && NumCopies != 2)
1840 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001841
1842 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001843 for (unsigned i = 0; i < NumCopies; ++i) {
1844 SDNode *Copy = Copies[i];
1845 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1846 UI != UE; ++UI) {
1847 if (UI->getOpcode() == ISD::CopyToReg) {
1848 SDNode *Use = *UI;
1849 if (Use == Copies[0] || Use == Copies[1])
1850 continue;
1851 return false;
1852 }
1853 if (UI->getOpcode() != ARMISD::RET_FLAG)
1854 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001855 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001856 }
1857 }
1858
Evan Cheng1bf891a2010-12-01 22:59:46 +00001859 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001860}
1861
Evan Cheng485fafc2011-03-21 01:19:09 +00001862bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1863 if (!EnableARMTailCalls)
1864 return false;
1865
1866 if (!CI->isTailCall())
1867 return false;
1868
1869 return !Subtarget->isThumb1Only();
1870}
1871
Bob Wilsonb62d2572009-11-03 00:02:05 +00001872// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1873// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1874// one of the above mentioned nodes. It has to be wrapped because otherwise
1875// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1876// be used to form addressing mode. These wrapped nodes will be selected
1877// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001878static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001879 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001880 // FIXME there is no actual debug info here
1881 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001882 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001883 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001884 if (CP->isMachineConstantPoolEntry())
1885 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1886 CP->getAlignment());
1887 else
1888 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1889 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001891}
1892
Jim Grosbache1102ca2010-07-19 17:20:38 +00001893unsigned ARMTargetLowering::getJumpTableEncoding() const {
1894 return MachineJumpTableInfo::EK_Inline;
1895}
1896
Dan Gohmand858e902010-04-17 15:26:15 +00001897SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1898 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001899 MachineFunction &MF = DAG.getMachineFunction();
1900 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1901 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001902 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001903 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001904 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001905 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1906 SDValue CPAddr;
1907 if (RelocM == Reloc::Static) {
1908 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1909 } else {
1910 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001911 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001912 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1913 ARMCP::CPBlockAddress,
1914 PCAdj);
1915 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1916 }
1917 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1918 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001919 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001920 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001921 if (RelocM == Reloc::Static)
1922 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001923 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001924 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001925}
1926
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001927// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001928SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001929ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001930 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001931 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001932 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001933 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001934 MachineFunction &MF = DAG.getMachineFunction();
1935 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001936 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001937 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001938 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001939 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001940 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001941 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001942 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001943 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001944 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001946
Evan Chenge7e0d622009-11-06 22:24:13 +00001947 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001948 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001949
1950 // call __tls_get_addr.
1951 ArgListTy Args;
1952 ArgListEntry Entry;
1953 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001954 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001955 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001956 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001957 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001958 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1959 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001960 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001961 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001962 return CallResult.first;
1963}
1964
1965// Lower ISD::GlobalTLSAddress using the "initial exec" or
1966// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001967SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001968ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001969 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001970 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001971 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001972 SDValue Offset;
1973 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001974 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001975 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001976 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001977
Chris Lattner4fb63d02009-07-15 04:12:33 +00001978 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001979 MachineFunction &MF = DAG.getMachineFunction();
1980 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001981 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001982 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001983 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1984 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001985 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001986 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001987 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001989 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001990 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001991 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001992 Chain = Offset.getValue(1);
1993
Evan Chenge7e0d622009-11-06 22:24:13 +00001994 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001995 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001996
Evan Cheng9eda6892009-10-31 03:39:36 +00001997 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001998 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001999 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002000 } else {
2001 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002002 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002003 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002005 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002006 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002007 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002008 }
2009
2010 // The address of the thread local variable is the add of the thread
2011 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002012 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002013}
2014
Dan Gohman475871a2008-07-27 21:46:04 +00002015SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002016ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002017 // TODO: implement the "local dynamic" model
2018 assert(Subtarget->isTargetELF() &&
2019 "TLS not implemented for non-ELF targets");
2020 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2021 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2022 // otherwise use the "Local Exec" TLS Model
2023 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2024 return LowerToTLSGeneralDynamicModel(GA, DAG);
2025 else
2026 return LowerToTLSExecModels(GA, DAG);
2027}
2028
Dan Gohman475871a2008-07-27 21:46:04 +00002029SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002030 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002031 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002032 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002033 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002034 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2035 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002036 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002037 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002038 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002039 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002041 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002042 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002043 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002044 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002045 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002046 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002047 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002048 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002049 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002050 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002051 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002052 }
2053
2054 // If we have T2 ops, we can materialize the address directly via movt/movw
2055 // pair. This is always cheaper.
2056 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002057 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002058 // FIXME: Once remat is capable of dealing with instructions with register
2059 // operands, expand this into two nodes.
2060 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2061 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002062 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002063 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2064 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2065 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2066 MachinePointerInfo::getConstantPool(),
2067 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002068 }
2069}
2070
Dan Gohman475871a2008-07-27 21:46:04 +00002071SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002072 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002073 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002074 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002075 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002076 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002077 MachineFunction &MF = DAG.getMachineFunction();
2078 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2079
2080 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002081 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002082 // FIXME: Once remat is capable of dealing with instructions with register
2083 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002084 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002085 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2086 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2087
Evan Cheng53519f02011-01-21 18:55:51 +00002088 unsigned Wrapper = (RelocM == Reloc::PIC_)
2089 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2090 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002091 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002092 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2093 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2094 MachinePointerInfo::getGOT(), false, false, 0);
2095 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002096 }
2097
2098 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002100 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002101 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002102 } else {
2103 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002104 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2105 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002106 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002107 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002108 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002110
Evan Cheng9eda6892009-10-31 03:39:36 +00002111 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002112 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002113 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002114 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002115
2116 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002117 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002118 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002119 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002120
Evan Cheng63476a82009-09-03 07:04:02 +00002121 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002122 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002123 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002124
2125 return Result;
2126}
2127
Dan Gohman475871a2008-07-27 21:46:04 +00002128SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002129 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002130 assert(Subtarget->isTargetELF() &&
2131 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002132 MachineFunction &MF = DAG.getMachineFunction();
2133 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002134 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002135 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002136 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002137 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002138 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2139 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002140 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002141 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002142 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002143 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002144 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002145 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002146 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002147 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002148}
2149
Jim Grosbach0e0da732009-05-12 23:59:14 +00002150SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002151ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2152 const {
2153 DebugLoc dl = Op.getDebugLoc();
2154 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendlingf05b1dc2011-04-05 01:37:43 +00002155 Op.getOperand(0));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002156}
2157
2158SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002159ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2160 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002161 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002162 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2163 Op.getOperand(1), Val);
2164}
2165
2166SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002167ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2168 DebugLoc dl = Op.getDebugLoc();
2169 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2170 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2171}
2172
2173SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002174ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002175 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002176 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002177 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002178 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002179 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002180 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002181 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002182 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2183 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002184 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002185 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002186 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002187 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002188 EVT PtrVT = getPointerTy();
2189 DebugLoc dl = Op.getDebugLoc();
2190 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2191 SDValue CPAddr;
2192 unsigned PCAdj = (RelocM != Reloc::PIC_)
2193 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002194 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002195 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2196 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002197 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002198 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002199 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002200 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002201 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002202 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002203
2204 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002205 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002206 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2207 }
2208 return Result;
2209 }
Evan Cheng92e39162011-03-29 23:06:19 +00002210 case Intrinsic::arm_neon_vmulls:
2211 case Intrinsic::arm_neon_vmullu: {
2212 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2213 ? ARMISD::VMULLs : ARMISD::VMULLu;
2214 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2215 Op.getOperand(1), Op.getOperand(2));
2216 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002217 }
2218}
2219
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002220static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002221 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002222 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002223 if (!Subtarget->hasDataBarrier()) {
2224 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2225 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2226 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002227 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002228 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002229 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002230 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002231 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002232
2233 SDValue Op5 = Op.getOperand(5);
2234 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2235 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2236 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2237 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2238
2239 ARM_MB::MemBOpt DMBOpt;
2240 if (isDeviceBarrier)
2241 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2242 else
2243 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2244 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2245 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002246}
2247
Evan Chengdfed19f2010-11-03 06:34:55 +00002248static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2249 const ARMSubtarget *Subtarget) {
2250 // ARM pre v5TE and Thumb1 does not have preload instructions.
2251 if (!(Subtarget->isThumb2() ||
2252 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2253 // Just preserve the chain.
2254 return Op.getOperand(0);
2255
2256 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002257 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2258 if (!isRead &&
2259 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2260 // ARMv7 with MP extension has PLDW.
2261 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002262
2263 if (Subtarget->isThumb())
2264 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002265 isRead = ~isRead & 1;
2266 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002267
Evan Cheng416941d2010-11-04 05:19:35 +00002268 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002269 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002270 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2271 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002272}
2273
Dan Gohman1e93df62010-04-17 14:41:14 +00002274static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2275 MachineFunction &MF = DAG.getMachineFunction();
2276 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2277
Evan Chenga8e29892007-01-19 07:51:42 +00002278 // vastart just stores the address of the VarArgsFrameIndex slot into the
2279 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002280 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002281 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002282 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002283 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002284 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2285 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002286}
2287
Dan Gohman475871a2008-07-27 21:46:04 +00002288SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002289ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2290 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002291 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002292 MachineFunction &MF = DAG.getMachineFunction();
2293 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2294
2295 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002296 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002297 RC = ARM::tGPRRegisterClass;
2298 else
2299 RC = ARM::GPRRegisterClass;
2300
2301 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002302 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002303 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002304
2305 SDValue ArgValue2;
2306 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002307 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002308 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002309
2310 // Create load node to retrieve arguments from the stack.
2311 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002312 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002313 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002314 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002315 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002316 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002317 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 }
2319
Jim Grosbache5165492009-11-09 00:11:35 +00002320 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002321}
2322
Stuart Hastingsc7315872011-04-20 16:47:52 +00002323void
2324ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2325 unsigned &VARegSize, unsigned &VARegSaveSize)
2326 const {
2327 unsigned NumGPRs;
2328 if (CCInfo.isFirstByValRegValid())
2329 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2330 else {
2331 unsigned int firstUnalloced;
2332 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2333 sizeof(GPRArgRegs) /
2334 sizeof(GPRArgRegs[0]));
2335 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2336 }
2337
2338 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2339 VARegSize = NumGPRs * 4;
2340 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2341}
2342
2343// The remaining GPRs hold either the beginning of variable-argument
2344// data, or the beginning of an aggregate passed by value (usuall
2345// byval). Either way, we allocate stack slots adjacent to the data
2346// provided by our caller, and store the unallocated registers there.
2347// If this is a variadic function, the va_list pointer will begin with
2348// these values; otherwise, this reassembles a (byval) structure that
2349// was split between registers and memory.
2350void
2351ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2352 DebugLoc dl, SDValue &Chain,
2353 unsigned ArgOffset) const {
2354 MachineFunction &MF = DAG.getMachineFunction();
2355 MachineFrameInfo *MFI = MF.getFrameInfo();
2356 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2357 unsigned firstRegToSaveIndex;
2358 if (CCInfo.isFirstByValRegValid())
2359 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2360 else {
2361 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2362 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2363 }
2364
2365 unsigned VARegSize, VARegSaveSize;
2366 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2367 if (VARegSaveSize) {
2368 // If this function is vararg, store any remaining integer argument regs
2369 // to their spots on the stack so that they may be loaded by deferencing
2370 // the result of va_next.
2371 AFI->setVarArgsRegSaveSize(VARegSaveSize);
2372 AFI->setVarArgsFrameIndex(
2373 MFI->CreateFixedObject(VARegSaveSize,
2374 ArgOffset + VARegSaveSize - VARegSize,
2375 false));
2376 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2377 getPointerTy());
2378
2379 SmallVector<SDValue, 4> MemOps;
2380 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2381 TargetRegisterClass *RC;
2382 if (AFI->isThumb1OnlyFunction())
2383 RC = ARM::tGPRRegisterClass;
2384 else
2385 RC = ARM::GPRRegisterClass;
2386
2387 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2388 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2389 SDValue Store =
2390 DAG.getStore(Val.getValue(1), dl, Val, FIN,
2391 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2392 false, false, 0);
2393 MemOps.push_back(Store);
2394 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2395 DAG.getConstant(4, getPointerTy()));
2396 }
2397 if (!MemOps.empty())
2398 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2399 &MemOps[0], MemOps.size());
2400 } else
2401 // This will point to the next argument passed via stack.
2402 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2403}
2404
Bob Wilson5bafff32009-06-22 23:27:02 +00002405SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002406ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002407 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002408 const SmallVectorImpl<ISD::InputArg>
2409 &Ins,
2410 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002411 SmallVectorImpl<SDValue> &InVals)
2412 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002413 MachineFunction &MF = DAG.getMachineFunction();
2414 MachineFrameInfo *MFI = MF.getFrameInfo();
2415
Bob Wilson1f595bb2009-04-17 19:07:39 +00002416 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2417
2418 // Assign locations to all of the incoming arguments.
2419 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002420 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2421 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00002422 CCInfo.setCallOrPrologue(Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002423 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002424 CCAssignFnForNode(CallConv, /* Return*/ false,
2425 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002426
2427 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002428 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002429
Stuart Hastingsf222e592011-02-28 17:17:53 +00002430 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002431 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2432 CCValAssign &VA = ArgLocs[i];
2433
Bob Wilsondee46d72009-04-17 20:35:10 +00002434 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002435 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002436 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002437
Bob Wilson1f595bb2009-04-17 19:07:39 +00002438 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002439 // f64 and vector types are split up into multiple registers or
2440 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002441 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002442 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002443 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002444 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002445 SDValue ArgValue2;
2446 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002447 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002448 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2449 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002450 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002451 false, false, 0);
2452 } else {
2453 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2454 Chain, DAG, dl);
2455 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002456 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2457 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002458 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002459 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002460 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2461 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002463
Bob Wilson5bafff32009-06-22 23:27:02 +00002464 } else {
2465 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002466
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002468 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002469 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002470 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002471 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002472 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002473 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002474 RC = (AFI->isThumb1OnlyFunction() ?
2475 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002476 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002477 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002478
2479 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002480 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002481 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002482 }
2483
2484 // If this is an 8 or 16-bit value, it is really passed promoted
2485 // to 32 bits. Insert an assert[sz]ext to capture this, then
2486 // truncate to the right size.
2487 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002488 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002489 case CCValAssign::Full: break;
2490 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002491 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002492 break;
2493 case CCValAssign::SExt:
2494 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2495 DAG.getValueType(VA.getValVT()));
2496 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2497 break;
2498 case CCValAssign::ZExt:
2499 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2500 DAG.getValueType(VA.getValVT()));
2501 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2502 break;
2503 }
2504
Dan Gohman98ca4f22009-08-05 01:29:28 +00002505 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002506
2507 } else { // VA.isRegLoc()
2508
2509 // sanity check
2510 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002512
Stuart Hastingsf222e592011-02-28 17:17:53 +00002513 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002514
Stuart Hastingsf222e592011-02-28 17:17:53 +00002515 // Some Ins[] entries become multiple ArgLoc[] entries.
2516 // Process them only once.
2517 if (index != lastInsIndex)
2518 {
2519 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2520 // FIXME: For now, all byval parameter objects are marked mutable. This can be
2521 // changed with more analysis.
2522 // In case of tail call optimization mark all arguments mutable. Since they
2523 // could be overwritten by lowering of arguments in case of a tail call.
2524 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002525 unsigned VARegSize, VARegSaveSize;
2526 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2527 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2528 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002529 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002530 int FI = MFI->CreateFixedObject(Bytes,
2531 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002532 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2533 } else {
2534 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2535 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002536
Stuart Hastingsf222e592011-02-28 17:17:53 +00002537 // Create load nodes to retrieve arguments from the stack.
2538 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2539 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2540 MachinePointerInfo::getFixedStack(FI),
2541 false, false, 0));
2542 }
2543 lastInsIndex = index;
2544 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002545 }
2546 }
2547
2548 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002549 if (isVarArg)
2550 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002551
Dan Gohman98ca4f22009-08-05 01:29:28 +00002552 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002553}
2554
2555/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002556static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002557 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002558 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002559 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002560 // Maybe this has already been legalized into the constant pool?
2561 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002562 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002563 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002564 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002565 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002566 }
2567 }
2568 return false;
2569}
2570
Evan Chenga8e29892007-01-19 07:51:42 +00002571/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2572/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002573SDValue
2574ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002575 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002576 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002577 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002578 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002579 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002580 // Constant does not fit, try adjusting it by one?
2581 switch (CC) {
2582 default: break;
2583 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002584 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002585 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002586 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002587 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002588 }
2589 break;
2590 case ISD::SETULT:
2591 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002592 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002593 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002594 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002595 }
2596 break;
2597 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002598 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002599 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002600 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002601 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002602 }
2603 break;
2604 case ISD::SETULE:
2605 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002606 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002607 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002608 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002609 }
2610 break;
2611 }
2612 }
2613 }
2614
2615 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002616 ARMISD::NodeType CompareType;
2617 switch (CondCode) {
2618 default:
2619 CompareType = ARMISD::CMP;
2620 break;
2621 case ARMCC::EQ:
2622 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002623 // Uses only Z Flag
2624 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002625 break;
2626 }
Evan Cheng218977b2010-07-13 19:27:42 +00002627 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002628 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002629}
2630
2631/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002632SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002633ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002634 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002635 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002636 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002637 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002638 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002639 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2640 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002641}
2642
Bob Wilson79f56c92011-03-08 01:17:20 +00002643/// duplicateCmp - Glue values can have only one use, so this function
2644/// duplicates a comparison node.
2645SDValue
2646ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2647 unsigned Opc = Cmp.getOpcode();
2648 DebugLoc DL = Cmp.getDebugLoc();
2649 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2650 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2651
2652 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2653 Cmp = Cmp.getOperand(0);
2654 Opc = Cmp.getOpcode();
2655 if (Opc == ARMISD::CMPFP)
2656 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2657 else {
2658 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2659 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2660 }
2661 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2662}
2663
Bill Wendlingde2b1512010-08-11 08:43:16 +00002664SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2665 SDValue Cond = Op.getOperand(0);
2666 SDValue SelectTrue = Op.getOperand(1);
2667 SDValue SelectFalse = Op.getOperand(2);
2668 DebugLoc dl = Op.getDebugLoc();
2669
2670 // Convert:
2671 //
2672 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2673 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2674 //
2675 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2676 const ConstantSDNode *CMOVTrue =
2677 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2678 const ConstantSDNode *CMOVFalse =
2679 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2680
2681 if (CMOVTrue && CMOVFalse) {
2682 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2683 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2684
2685 SDValue True;
2686 SDValue False;
2687 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2688 True = SelectTrue;
2689 False = SelectFalse;
2690 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2691 True = SelectFalse;
2692 False = SelectTrue;
2693 }
2694
2695 if (True.getNode() && False.getNode()) {
2696 EVT VT = Cond.getValueType();
2697 SDValue ARMcc = Cond.getOperand(2);
2698 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002699 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002700 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2701 }
2702 }
2703 }
2704
2705 return DAG.getSelectCC(dl, Cond,
2706 DAG.getConstant(0, Cond.getValueType()),
2707 SelectTrue, SelectFalse, ISD::SETNE);
2708}
2709
Dan Gohmand858e902010-04-17 15:26:15 +00002710SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002711 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002712 SDValue LHS = Op.getOperand(0);
2713 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002714 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002715 SDValue TrueVal = Op.getOperand(2);
2716 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002717 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002718
Owen Anderson825b72b2009-08-11 20:47:22 +00002719 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002720 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002721 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002722 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2723 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002724 }
2725
2726 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002727 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002728
Evan Cheng218977b2010-07-13 19:27:42 +00002729 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2730 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002731 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002732 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002733 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002734 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002735 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002736 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002737 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002738 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002739 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002740 }
2741 return Result;
2742}
2743
Evan Cheng218977b2010-07-13 19:27:42 +00002744/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2745/// to morph to an integer compare sequence.
2746static bool canChangeToInt(SDValue Op, bool &SeenZero,
2747 const ARMSubtarget *Subtarget) {
2748 SDNode *N = Op.getNode();
2749 if (!N->hasOneUse())
2750 // Otherwise it requires moving the value from fp to integer registers.
2751 return false;
2752 if (!N->getNumValues())
2753 return false;
2754 EVT VT = Op.getValueType();
2755 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2756 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2757 // vmrs are very slow, e.g. cortex-a8.
2758 return false;
2759
2760 if (isFloatingPointZero(Op)) {
2761 SeenZero = true;
2762 return true;
2763 }
2764 return ISD::isNormalLoad(N);
2765}
2766
2767static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2768 if (isFloatingPointZero(Op))
2769 return DAG.getConstant(0, MVT::i32);
2770
2771 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2772 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002773 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002774 Ld->isVolatile(), Ld->isNonTemporal(),
2775 Ld->getAlignment());
2776
2777 llvm_unreachable("Unknown VFP cmp argument!");
2778}
2779
2780static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2781 SDValue &RetVal1, SDValue &RetVal2) {
2782 if (isFloatingPointZero(Op)) {
2783 RetVal1 = DAG.getConstant(0, MVT::i32);
2784 RetVal2 = DAG.getConstant(0, MVT::i32);
2785 return;
2786 }
2787
2788 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2789 SDValue Ptr = Ld->getBasePtr();
2790 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2791 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002792 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002793 Ld->isVolatile(), Ld->isNonTemporal(),
2794 Ld->getAlignment());
2795
2796 EVT PtrType = Ptr.getValueType();
2797 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2798 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2799 PtrType, Ptr, DAG.getConstant(4, PtrType));
2800 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2801 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002802 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002803 Ld->isVolatile(), Ld->isNonTemporal(),
2804 NewAlign);
2805 return;
2806 }
2807
2808 llvm_unreachable("Unknown VFP cmp argument!");
2809}
2810
2811/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2812/// f32 and even f64 comparisons to integer ones.
2813SDValue
2814ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2815 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002816 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002817 SDValue LHS = Op.getOperand(2);
2818 SDValue RHS = Op.getOperand(3);
2819 SDValue Dest = Op.getOperand(4);
2820 DebugLoc dl = Op.getDebugLoc();
2821
2822 bool SeenZero = false;
2823 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2824 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002825 // If one of the operand is zero, it's safe to ignore the NaN case since
2826 // we only care about equality comparisons.
2827 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002828 // If unsafe fp math optimization is enabled and there are no other uses of
2829 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002830 // to an integer comparison.
2831 if (CC == ISD::SETOEQ)
2832 CC = ISD::SETEQ;
2833 else if (CC == ISD::SETUNE)
2834 CC = ISD::SETNE;
2835
2836 SDValue ARMcc;
2837 if (LHS.getValueType() == MVT::f32) {
2838 LHS = bitcastf32Toi32(LHS, DAG);
2839 RHS = bitcastf32Toi32(RHS, DAG);
2840 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2841 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2842 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2843 Chain, Dest, ARMcc, CCR, Cmp);
2844 }
2845
2846 SDValue LHS1, LHS2;
2847 SDValue RHS1, RHS2;
2848 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2849 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2850 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2851 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002852 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002853 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2854 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2855 }
2856
2857 return SDValue();
2858}
2859
2860SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2861 SDValue Chain = Op.getOperand(0);
2862 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2863 SDValue LHS = Op.getOperand(2);
2864 SDValue RHS = Op.getOperand(3);
2865 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002866 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002867
Owen Anderson825b72b2009-08-11 20:47:22 +00002868 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002869 SDValue ARMcc;
2870 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002871 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002872 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002873 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002874 }
2875
Owen Anderson825b72b2009-08-11 20:47:22 +00002876 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002877
2878 if (UnsafeFPMath &&
2879 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2880 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2881 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2882 if (Result.getNode())
2883 return Result;
2884 }
2885
Evan Chenga8e29892007-01-19 07:51:42 +00002886 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002887 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002888
Evan Cheng218977b2010-07-13 19:27:42 +00002889 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2890 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002891 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002892 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002893 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002894 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002895 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002896 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2897 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002898 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002899 }
2900 return Res;
2901}
2902
Dan Gohmand858e902010-04-17 15:26:15 +00002903SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002904 SDValue Chain = Op.getOperand(0);
2905 SDValue Table = Op.getOperand(1);
2906 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002907 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002908
Owen Andersone50ed302009-08-10 22:56:29 +00002909 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002910 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2911 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002912 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002913 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002914 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002915 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2916 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002917 if (Subtarget->isThumb2()) {
2918 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2919 // which does another jump to the destination. This also makes it easier
2920 // to translate it to TBB / TBH later.
2921 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002922 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002923 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002924 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002925 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002926 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002927 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002928 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002929 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002930 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002932 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002933 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002934 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002935 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002936 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002937 }
Evan Chenga8e29892007-01-19 07:51:42 +00002938}
2939
Bob Wilson76a312b2010-03-19 22:51:32 +00002940static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2941 DebugLoc dl = Op.getDebugLoc();
2942 unsigned Opc;
2943
2944 switch (Op.getOpcode()) {
2945 default:
2946 assert(0 && "Invalid opcode!");
2947 case ISD::FP_TO_SINT:
2948 Opc = ARMISD::FTOSI;
2949 break;
2950 case ISD::FP_TO_UINT:
2951 Opc = ARMISD::FTOUI;
2952 break;
2953 }
2954 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002955 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002956}
2957
Cameron Zwarich3007d332011-03-29 21:41:55 +00002958static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2959 EVT VT = Op.getValueType();
2960 DebugLoc dl = Op.getDebugLoc();
2961
2962 EVT OperandVT = Op.getOperand(0).getValueType();
2963 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2964 if (VT != MVT::v4f32)
2965 return DAG.UnrollVectorOp(Op.getNode());
2966
2967 unsigned CastOpc;
2968 unsigned Opc;
2969 switch (Op.getOpcode()) {
2970 default:
2971 assert(0 && "Invalid opcode!");
2972 case ISD::SINT_TO_FP:
2973 CastOpc = ISD::SIGN_EXTEND;
2974 Opc = ISD::SINT_TO_FP;
2975 break;
2976 case ISD::UINT_TO_FP:
2977 CastOpc = ISD::ZERO_EXTEND;
2978 Opc = ISD::UINT_TO_FP;
2979 break;
2980 }
2981
2982 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2983 return DAG.getNode(Opc, dl, VT, Op);
2984}
2985
Bob Wilson76a312b2010-03-19 22:51:32 +00002986static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2987 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002988 if (VT.isVector())
2989 return LowerVectorINT_TO_FP(Op, DAG);
2990
Bob Wilson76a312b2010-03-19 22:51:32 +00002991 DebugLoc dl = Op.getDebugLoc();
2992 unsigned Opc;
2993
2994 switch (Op.getOpcode()) {
2995 default:
2996 assert(0 && "Invalid opcode!");
2997 case ISD::SINT_TO_FP:
2998 Opc = ARMISD::SITOF;
2999 break;
3000 case ISD::UINT_TO_FP:
3001 Opc = ARMISD::UITOF;
3002 break;
3003 }
3004
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003005 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003006 return DAG.getNode(Opc, dl, VT, Op);
3007}
3008
Evan Cheng515fe3a2010-07-08 02:08:50 +00003009SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003010 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003011 SDValue Tmp0 = Op.getOperand(0);
3012 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003013 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003014 EVT VT = Op.getValueType();
3015 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003016 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3017 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3018 bool UseNEON = !InGPR && Subtarget->hasNEON();
3019
3020 if (UseNEON) {
3021 // Use VBSL to copy the sign bit.
3022 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3023 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3024 DAG.getTargetConstant(EncodedVal, MVT::i32));
3025 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3026 if (VT == MVT::f64)
3027 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3028 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3029 DAG.getConstant(32, MVT::i32));
3030 else /*if (VT == MVT::f32)*/
3031 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3032 if (SrcVT == MVT::f32) {
3033 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3034 if (VT == MVT::f64)
3035 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3036 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3037 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003038 } else if (VT == MVT::f32)
3039 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3040 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3041 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003042 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3043 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3044
3045 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3046 MVT::i32);
3047 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3048 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3049 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003050
Evan Chenge573fb32011-02-23 02:24:55 +00003051 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3052 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3053 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003054 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003055 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3056 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3057 DAG.getConstant(0, MVT::i32));
3058 } else {
3059 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3060 }
3061
3062 return Res;
3063 }
Evan Chengc143dd42011-02-11 02:28:55 +00003064
3065 // Bitcast operand 1 to i32.
3066 if (SrcVT == MVT::f64)
3067 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3068 &Tmp1, 1).getValue(1);
3069 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3070
Evan Chenge573fb32011-02-23 02:24:55 +00003071 // Or in the signbit with integer operations.
3072 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3073 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3074 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3075 if (VT == MVT::f32) {
3076 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3077 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3078 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3079 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003080 }
3081
Evan Chenge573fb32011-02-23 02:24:55 +00003082 // f64: Or the high part with signbit and then combine two parts.
3083 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3084 &Tmp0, 1);
3085 SDValue Lo = Tmp0.getValue(0);
3086 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3087 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3088 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003089}
3090
Evan Cheng2457f2c2010-05-22 01:47:14 +00003091SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3092 MachineFunction &MF = DAG.getMachineFunction();
3093 MachineFrameInfo *MFI = MF.getFrameInfo();
3094 MFI->setReturnAddressIsTaken(true);
3095
3096 EVT VT = Op.getValueType();
3097 DebugLoc dl = Op.getDebugLoc();
3098 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3099 if (Depth) {
3100 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3101 SDValue Offset = DAG.getConstant(4, MVT::i32);
3102 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3103 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003104 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003105 }
3106
3107 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003108 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003109 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3110}
3111
Dan Gohmand858e902010-04-17 15:26:15 +00003112SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003113 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3114 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003115
Owen Andersone50ed302009-08-10 22:56:29 +00003116 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003117 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3118 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003119 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003120 ? ARM::R7 : ARM::R11;
3121 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3122 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003123 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3124 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003125 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003126 return FrameAddr;
3127}
3128
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003129/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003130/// expand a bit convert where either the source or destination type is i64 to
3131/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3132/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3133/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003134static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003135 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3136 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003137 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003138
Bob Wilson9f3f0612010-04-17 05:30:19 +00003139 // This function is only supposed to be called for i64 types, either as the
3140 // source or destination of the bit convert.
3141 EVT SrcVT = Op.getValueType();
3142 EVT DstVT = N->getValueType(0);
3143 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003144 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003145
Bob Wilson9f3f0612010-04-17 05:30:19 +00003146 // Turn i64->f64 into VMOVDRR.
3147 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3149 DAG.getConstant(0, MVT::i32));
3150 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3151 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003152 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003153 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003154 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003155
Jim Grosbache5165492009-11-09 00:11:35 +00003156 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003157 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3158 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3159 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3160 // Merge the pieces into a single i64 value.
3161 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3162 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003163
Bob Wilson9f3f0612010-04-17 05:30:19 +00003164 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003165}
3166
Bob Wilson5bafff32009-06-22 23:27:02 +00003167/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003168/// Zero vectors are used to represent vector negation and in those cases
3169/// will be implemented with the NEON VNEG instruction. However, VNEG does
3170/// not support i64 elements, so sometimes the zero vectors will need to be
3171/// explicitly constructed. Regardless, use a canonical VMOV to create the
3172/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003173static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003174 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003175 // The canonical modified immediate encoding of a zero vector is....0!
3176 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3177 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3178 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003179 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003180}
3181
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003182/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3183/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003184SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3185 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003186 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3187 EVT VT = Op.getValueType();
3188 unsigned VTBits = VT.getSizeInBits();
3189 DebugLoc dl = Op.getDebugLoc();
3190 SDValue ShOpLo = Op.getOperand(0);
3191 SDValue ShOpHi = Op.getOperand(1);
3192 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003193 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003194 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003195
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003196 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3197
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003198 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3199 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3200 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3201 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3202 DAG.getConstant(VTBits, MVT::i32));
3203 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3204 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003205 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003206
3207 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3208 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003209 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003210 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003211 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003212 CCR, Cmp);
3213
3214 SDValue Ops[2] = { Lo, Hi };
3215 return DAG.getMergeValues(Ops, 2, dl);
3216}
3217
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003218/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3219/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003220SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3221 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003222 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3223 EVT VT = Op.getValueType();
3224 unsigned VTBits = VT.getSizeInBits();
3225 DebugLoc dl = Op.getDebugLoc();
3226 SDValue ShOpLo = Op.getOperand(0);
3227 SDValue ShOpHi = Op.getOperand(1);
3228 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003229 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003230
3231 assert(Op.getOpcode() == ISD::SHL_PARTS);
3232 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3233 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3234 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3235 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3236 DAG.getConstant(VTBits, MVT::i32));
3237 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3238 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3239
3240 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3241 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3242 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003243 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003244 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003245 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003246 CCR, Cmp);
3247
3248 SDValue Ops[2] = { Lo, Hi };
3249 return DAG.getMergeValues(Ops, 2, dl);
3250}
3251
Jim Grosbach4725ca72010-09-08 03:54:02 +00003252SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003253 SelectionDAG &DAG) const {
3254 // The rounding mode is in bits 23:22 of the FPSCR.
3255 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3256 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3257 // so that the shift + and get folded into a bitfield extract.
3258 DebugLoc dl = Op.getDebugLoc();
3259 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3260 DAG.getConstant(Intrinsic::arm_get_fpscr,
3261 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003262 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003263 DAG.getConstant(1U << 22, MVT::i32));
3264 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3265 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003266 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003267 DAG.getConstant(3, MVT::i32));
3268}
3269
Jim Grosbach3482c802010-01-18 19:58:49 +00003270static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3271 const ARMSubtarget *ST) {
3272 EVT VT = N->getValueType(0);
3273 DebugLoc dl = N->getDebugLoc();
3274
3275 if (!ST->hasV6T2Ops())
3276 return SDValue();
3277
3278 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3279 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3280}
3281
Bob Wilson5bafff32009-06-22 23:27:02 +00003282static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3283 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003284 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003285 DebugLoc dl = N->getDebugLoc();
3286
Bob Wilsond5448bb2010-11-18 21:16:28 +00003287 if (!VT.isVector())
3288 return SDValue();
3289
Bob Wilson5bafff32009-06-22 23:27:02 +00003290 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003291 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003292
Bob Wilsond5448bb2010-11-18 21:16:28 +00003293 // Left shifts translate directly to the vshiftu intrinsic.
3294 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003295 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003296 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3297 N->getOperand(0), N->getOperand(1));
3298
3299 assert((N->getOpcode() == ISD::SRA ||
3300 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3301
3302 // NEON uses the same intrinsics for both left and right shifts. For
3303 // right shifts, the shift amounts are negative, so negate the vector of
3304 // shift amounts.
3305 EVT ShiftVT = N->getOperand(1).getValueType();
3306 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3307 getZeroVector(ShiftVT, DAG, dl),
3308 N->getOperand(1));
3309 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3310 Intrinsic::arm_neon_vshifts :
3311 Intrinsic::arm_neon_vshiftu);
3312 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3313 DAG.getConstant(vshiftInt, MVT::i32),
3314 N->getOperand(0), NegatedCount);
3315}
3316
3317static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3318 const ARMSubtarget *ST) {
3319 EVT VT = N->getValueType(0);
3320 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003321
Eli Friedmance392eb2009-08-22 03:13:10 +00003322 // We can get here for a node like i32 = ISD::SHL i32, i64
3323 if (VT != MVT::i64)
3324 return SDValue();
3325
3326 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003327 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003328
Chris Lattner27a6c732007-11-24 07:07:01 +00003329 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3330 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003331 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003332 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003333
Chris Lattner27a6c732007-11-24 07:07:01 +00003334 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003335 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003336
Chris Lattner27a6c732007-11-24 07:07:01 +00003337 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003338 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003339 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003341 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003342
Chris Lattner27a6c732007-11-24 07:07:01 +00003343 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3344 // captures the result into a carry flag.
3345 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003346 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003347
Chris Lattner27a6c732007-11-24 07:07:01 +00003348 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003350
Chris Lattner27a6c732007-11-24 07:07:01 +00003351 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003353}
3354
Bob Wilson5bafff32009-06-22 23:27:02 +00003355static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3356 SDValue TmpOp0, TmpOp1;
3357 bool Invert = false;
3358 bool Swap = false;
3359 unsigned Opc = 0;
3360
3361 SDValue Op0 = Op.getOperand(0);
3362 SDValue Op1 = Op.getOperand(1);
3363 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003364 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003365 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3366 DebugLoc dl = Op.getDebugLoc();
3367
3368 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3369 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003370 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003371 case ISD::SETUNE:
3372 case ISD::SETNE: Invert = true; // Fallthrough
3373 case ISD::SETOEQ:
3374 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3375 case ISD::SETOLT:
3376 case ISD::SETLT: Swap = true; // Fallthrough
3377 case ISD::SETOGT:
3378 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3379 case ISD::SETOLE:
3380 case ISD::SETLE: Swap = true; // Fallthrough
3381 case ISD::SETOGE:
3382 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3383 case ISD::SETUGE: Swap = true; // Fallthrough
3384 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3385 case ISD::SETUGT: Swap = true; // Fallthrough
3386 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3387 case ISD::SETUEQ: Invert = true; // Fallthrough
3388 case ISD::SETONE:
3389 // Expand this to (OLT | OGT).
3390 TmpOp0 = Op0;
3391 TmpOp1 = Op1;
3392 Opc = ISD::OR;
3393 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3394 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3395 break;
3396 case ISD::SETUO: Invert = true; // Fallthrough
3397 case ISD::SETO:
3398 // Expand this to (OLT | OGE).
3399 TmpOp0 = Op0;
3400 TmpOp1 = Op1;
3401 Opc = ISD::OR;
3402 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3403 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3404 break;
3405 }
3406 } else {
3407 // Integer comparisons.
3408 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003409 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003410 case ISD::SETNE: Invert = true;
3411 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3412 case ISD::SETLT: Swap = true;
3413 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3414 case ISD::SETLE: Swap = true;
3415 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3416 case ISD::SETULT: Swap = true;
3417 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3418 case ISD::SETULE: Swap = true;
3419 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3420 }
3421
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003422 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003423 if (Opc == ARMISD::VCEQ) {
3424
3425 SDValue AndOp;
3426 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3427 AndOp = Op0;
3428 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3429 AndOp = Op1;
3430
3431 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003432 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003433 AndOp = AndOp.getOperand(0);
3434
3435 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3436 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003437 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3438 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003439 Invert = !Invert;
3440 }
3441 }
3442 }
3443
3444 if (Swap)
3445 std::swap(Op0, Op1);
3446
Owen Andersonc24cb352010-11-08 23:21:22 +00003447 // If one of the operands is a constant vector zero, attempt to fold the
3448 // comparison to a specialized compare-against-zero form.
3449 SDValue SingleOp;
3450 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3451 SingleOp = Op0;
3452 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3453 if (Opc == ARMISD::VCGE)
3454 Opc = ARMISD::VCLEZ;
3455 else if (Opc == ARMISD::VCGT)
3456 Opc = ARMISD::VCLTZ;
3457 SingleOp = Op1;
3458 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003459
Owen Andersonc24cb352010-11-08 23:21:22 +00003460 SDValue Result;
3461 if (SingleOp.getNode()) {
3462 switch (Opc) {
3463 case ARMISD::VCEQ:
3464 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3465 case ARMISD::VCGE:
3466 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3467 case ARMISD::VCLEZ:
3468 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3469 case ARMISD::VCGT:
3470 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3471 case ARMISD::VCLTZ:
3472 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3473 default:
3474 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3475 }
3476 } else {
3477 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3478 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003479
3480 if (Invert)
3481 Result = DAG.getNOT(dl, Result, VT);
3482
3483 return Result;
3484}
3485
Bob Wilsond3c42842010-06-14 22:19:57 +00003486/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3487/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003488/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003489static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3490 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003491 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003492 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003493
Bob Wilson827b2102010-06-15 19:05:35 +00003494 // SplatBitSize is set to the smallest size that splats the vector, so a
3495 // zero vector will always have SplatBitSize == 8. However, NEON modified
3496 // immediate instructions others than VMOV do not support the 8-bit encoding
3497 // of a zero vector, and the default encoding of zero is supposed to be the
3498 // 32-bit version.
3499 if (SplatBits == 0)
3500 SplatBitSize = 32;
3501
Bob Wilson5bafff32009-06-22 23:27:02 +00003502 switch (SplatBitSize) {
3503 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003504 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003505 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003506 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003507 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003508 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003509 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003510 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003511 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003512
3513 case 16:
3514 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003515 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003516 if ((SplatBits & ~0xff) == 0) {
3517 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003518 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003519 Imm = SplatBits;
3520 break;
3521 }
3522 if ((SplatBits & ~0xff00) == 0) {
3523 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003524 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003525 Imm = SplatBits >> 8;
3526 break;
3527 }
3528 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003529
3530 case 32:
3531 // NEON's 32-bit VMOV supports splat values where:
3532 // * only one byte is nonzero, or
3533 // * the least significant byte is 0xff and the second byte is nonzero, or
3534 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003535 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003536 if ((SplatBits & ~0xff) == 0) {
3537 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003538 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003539 Imm = SplatBits;
3540 break;
3541 }
3542 if ((SplatBits & ~0xff00) == 0) {
3543 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003544 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003545 Imm = SplatBits >> 8;
3546 break;
3547 }
3548 if ((SplatBits & ~0xff0000) == 0) {
3549 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003550 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003551 Imm = SplatBits >> 16;
3552 break;
3553 }
3554 if ((SplatBits & ~0xff000000) == 0) {
3555 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003556 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003557 Imm = SplatBits >> 24;
3558 break;
3559 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003560
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003561 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3562 if (type == OtherModImm) return SDValue();
3563
Bob Wilson5bafff32009-06-22 23:27:02 +00003564 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003565 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3566 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003567 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003568 Imm = SplatBits >> 8;
3569 SplatBits |= 0xff;
3570 break;
3571 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003572
3573 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003574 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3575 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003576 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003577 Imm = SplatBits >> 16;
3578 SplatBits |= 0xffff;
3579 break;
3580 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003581
3582 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3583 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3584 // VMOV.I32. A (very) minor optimization would be to replicate the value
3585 // and fall through here to test for a valid 64-bit splat. But, then the
3586 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003587 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003588
3589 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003590 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003591 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003592 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003593 uint64_t BitMask = 0xff;
3594 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003595 unsigned ImmMask = 1;
3596 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003597 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003598 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003599 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003600 Imm |= ImmMask;
3601 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003602 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003603 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003604 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003605 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003606 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003607 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003608 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003609 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003610 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003611 break;
3612 }
3613
Bob Wilson1a913ed2010-06-11 21:34:50 +00003614 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003615 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003616 return SDValue();
3617 }
3618
Bob Wilsoncba270d2010-07-13 21:16:48 +00003619 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3620 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003621}
3622
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003623static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3624 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003625 unsigned NumElts = VT.getVectorNumElements();
3626 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003627
3628 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3629 if (M[0] < 0)
3630 return false;
3631
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003632 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003633
3634 // If this is a VEXT shuffle, the immediate value is the index of the first
3635 // element. The other shuffle indices must be the successive elements after
3636 // the first one.
3637 unsigned ExpectedElt = Imm;
3638 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003639 // Increment the expected index. If it wraps around, it may still be
3640 // a VEXT but the source vectors must be swapped.
3641 ExpectedElt += 1;
3642 if (ExpectedElt == NumElts * 2) {
3643 ExpectedElt = 0;
3644 ReverseVEXT = true;
3645 }
3646
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003647 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003648 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003649 return false;
3650 }
3651
3652 // Adjust the index value if the source operands will be swapped.
3653 if (ReverseVEXT)
3654 Imm -= NumElts;
3655
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003656 return true;
3657}
3658
Bob Wilson8bb9e482009-07-26 00:39:34 +00003659/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3660/// instruction with the specified blocksize. (The order of the elements
3661/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003662static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3663 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003664 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3665 "Only possible block sizes for VREV are: 16, 32, 64");
3666
Bob Wilson8bb9e482009-07-26 00:39:34 +00003667 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003668 if (EltSz == 64)
3669 return false;
3670
3671 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003672 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003673 // If the first shuffle index is UNDEF, be optimistic.
3674 if (M[0] < 0)
3675 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003676
3677 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3678 return false;
3679
3680 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003681 if (M[i] < 0) continue; // ignore UNDEF indices
3682 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003683 return false;
3684 }
3685
3686 return true;
3687}
3688
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003689static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3690 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3691 // range, then 0 is placed into the resulting vector. So pretty much any mask
3692 // of 8 elements can work here.
3693 return VT == MVT::v8i8 && M.size() == 8;
3694}
3695
Bob Wilsonc692cb72009-08-21 20:54:19 +00003696static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3697 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003698 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3699 if (EltSz == 64)
3700 return false;
3701
Bob Wilsonc692cb72009-08-21 20:54:19 +00003702 unsigned NumElts = VT.getVectorNumElements();
3703 WhichResult = (M[0] == 0 ? 0 : 1);
3704 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003705 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3706 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003707 return false;
3708 }
3709 return true;
3710}
3711
Bob Wilson324f4f12009-12-03 06:40:55 +00003712/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3713/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3714/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3715static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3716 unsigned &WhichResult) {
3717 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3718 if (EltSz == 64)
3719 return false;
3720
3721 unsigned NumElts = VT.getVectorNumElements();
3722 WhichResult = (M[0] == 0 ? 0 : 1);
3723 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003724 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3725 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003726 return false;
3727 }
3728 return true;
3729}
3730
Bob Wilsonc692cb72009-08-21 20:54:19 +00003731static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3732 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003733 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3734 if (EltSz == 64)
3735 return false;
3736
Bob Wilsonc692cb72009-08-21 20:54:19 +00003737 unsigned NumElts = VT.getVectorNumElements();
3738 WhichResult = (M[0] == 0 ? 0 : 1);
3739 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003740 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003741 if ((unsigned) M[i] != 2 * i + WhichResult)
3742 return false;
3743 }
3744
3745 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003746 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003747 return false;
3748
3749 return true;
3750}
3751
Bob Wilson324f4f12009-12-03 06:40:55 +00003752/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3753/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3754/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3755static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3756 unsigned &WhichResult) {
3757 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3758 if (EltSz == 64)
3759 return false;
3760
3761 unsigned Half = VT.getVectorNumElements() / 2;
3762 WhichResult = (M[0] == 0 ? 0 : 1);
3763 for (unsigned j = 0; j != 2; ++j) {
3764 unsigned Idx = WhichResult;
3765 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003766 int MIdx = M[i + j * Half];
3767 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003768 return false;
3769 Idx += 2;
3770 }
3771 }
3772
3773 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3774 if (VT.is64BitVector() && EltSz == 32)
3775 return false;
3776
3777 return true;
3778}
3779
Bob Wilsonc692cb72009-08-21 20:54:19 +00003780static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3781 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003782 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3783 if (EltSz == 64)
3784 return false;
3785
Bob Wilsonc692cb72009-08-21 20:54:19 +00003786 unsigned NumElts = VT.getVectorNumElements();
3787 WhichResult = (M[0] == 0 ? 0 : 1);
3788 unsigned Idx = WhichResult * NumElts / 2;
3789 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003790 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3791 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003792 return false;
3793 Idx += 1;
3794 }
3795
3796 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003797 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003798 return false;
3799
3800 return true;
3801}
3802
Bob Wilson324f4f12009-12-03 06:40:55 +00003803/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3804/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3805/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3806static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3807 unsigned &WhichResult) {
3808 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3809 if (EltSz == 64)
3810 return false;
3811
3812 unsigned NumElts = VT.getVectorNumElements();
3813 WhichResult = (M[0] == 0 ? 0 : 1);
3814 unsigned Idx = WhichResult * NumElts / 2;
3815 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003816 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3817 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003818 return false;
3819 Idx += 1;
3820 }
3821
3822 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3823 if (VT.is64BitVector() && EltSz == 32)
3824 return false;
3825
3826 return true;
3827}
3828
Dale Johannesenf630c712010-07-29 20:10:08 +00003829// If N is an integer constant that can be moved into a register in one
3830// instruction, return an SDValue of such a constant (will become a MOV
3831// instruction). Otherwise return null.
3832static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3833 const ARMSubtarget *ST, DebugLoc dl) {
3834 uint64_t Val;
3835 if (!isa<ConstantSDNode>(N))
3836 return SDValue();
3837 Val = cast<ConstantSDNode>(N)->getZExtValue();
3838
3839 if (ST->isThumb1Only()) {
3840 if (Val <= 255 || ~Val <= 255)
3841 return DAG.getConstant(Val, MVT::i32);
3842 } else {
3843 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3844 return DAG.getConstant(Val, MVT::i32);
3845 }
3846 return SDValue();
3847}
3848
Bob Wilson5bafff32009-06-22 23:27:02 +00003849// If this is a case we can't handle, return null and let the default
3850// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003851SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3852 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003853 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003854 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003855 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003856
3857 APInt SplatBits, SplatUndef;
3858 unsigned SplatBitSize;
3859 bool HasAnyUndefs;
3860 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003861 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003862 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003863 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003864 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003865 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003866 DAG, VmovVT, VT.is128BitVector(),
3867 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003868 if (Val.getNode()) {
3869 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003870 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003871 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003872
3873 // Try an immediate VMVN.
3874 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3875 ((1LL << SplatBitSize) - 1));
3876 Val = isNEONModifiedImm(NegatedImm,
3877 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003878 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003879 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003880 if (Val.getNode()) {
3881 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003882 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003883 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003884 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003885 }
3886
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003887 // Scan through the operands to see if only one value is used.
3888 unsigned NumElts = VT.getVectorNumElements();
3889 bool isOnlyLowElement = true;
3890 bool usesOnlyOneValue = true;
3891 bool isConstant = true;
3892 SDValue Value;
3893 for (unsigned i = 0; i < NumElts; ++i) {
3894 SDValue V = Op.getOperand(i);
3895 if (V.getOpcode() == ISD::UNDEF)
3896 continue;
3897 if (i > 0)
3898 isOnlyLowElement = false;
3899 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3900 isConstant = false;
3901
3902 if (!Value.getNode())
3903 Value = V;
3904 else if (V != Value)
3905 usesOnlyOneValue = false;
3906 }
3907
3908 if (!Value.getNode())
3909 return DAG.getUNDEF(VT);
3910
3911 if (isOnlyLowElement)
3912 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3913
Dale Johannesenf630c712010-07-29 20:10:08 +00003914 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3915
Dale Johannesen575cd142010-10-19 20:00:17 +00003916 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3917 // i32 and try again.
3918 if (usesOnlyOneValue && EltSize <= 32) {
3919 if (!isConstant)
3920 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3921 if (VT.getVectorElementType().isFloatingPoint()) {
3922 SmallVector<SDValue, 8> Ops;
3923 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003924 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003925 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003926 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3927 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003928 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3929 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003930 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003931 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003932 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3933 if (Val.getNode())
3934 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003935 }
3936
3937 // If all elements are constants and the case above didn't get hit, fall back
3938 // to the default expansion, which will generate a load from the constant
3939 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003940 if (isConstant)
3941 return SDValue();
3942
Bob Wilson11a1dff2011-01-07 21:37:30 +00003943 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3944 if (NumElts >= 4) {
3945 SDValue shuffle = ReconstructShuffle(Op, DAG);
3946 if (shuffle != SDValue())
3947 return shuffle;
3948 }
3949
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003950 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003951 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3952 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003953 if (EltSize >= 32) {
3954 // Do the expansion with floating-point types, since that is what the VFP
3955 // registers are defined to use, and since i64 is not legal.
3956 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3957 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003958 SmallVector<SDValue, 8> Ops;
3959 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003960 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003961 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003962 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003963 }
3964
3965 return SDValue();
3966}
3967
Bob Wilson11a1dff2011-01-07 21:37:30 +00003968// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003969// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003970SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3971 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003972 DebugLoc dl = Op.getDebugLoc();
3973 EVT VT = Op.getValueType();
3974 unsigned NumElts = VT.getVectorNumElements();
3975
3976 SmallVector<SDValue, 2> SourceVecs;
3977 SmallVector<unsigned, 2> MinElts;
3978 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003979
Bob Wilson11a1dff2011-01-07 21:37:30 +00003980 for (unsigned i = 0; i < NumElts; ++i) {
3981 SDValue V = Op.getOperand(i);
3982 if (V.getOpcode() == ISD::UNDEF)
3983 continue;
3984 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3985 // A shuffle can only come from building a vector from various
3986 // elements of other vectors.
3987 return SDValue();
3988 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003989
Bob Wilson11a1dff2011-01-07 21:37:30 +00003990 // Record this extraction against the appropriate vector if possible...
3991 SDValue SourceVec = V.getOperand(0);
3992 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3993 bool FoundSource = false;
3994 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3995 if (SourceVecs[j] == SourceVec) {
3996 if (MinElts[j] > EltNo)
3997 MinElts[j] = EltNo;
3998 if (MaxElts[j] < EltNo)
3999 MaxElts[j] = EltNo;
4000 FoundSource = true;
4001 break;
4002 }
4003 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004004
Bob Wilson11a1dff2011-01-07 21:37:30 +00004005 // Or record a new source if not...
4006 if (!FoundSource) {
4007 SourceVecs.push_back(SourceVec);
4008 MinElts.push_back(EltNo);
4009 MaxElts.push_back(EltNo);
4010 }
4011 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004012
Bob Wilson11a1dff2011-01-07 21:37:30 +00004013 // Currently only do something sane when at most two source vectors
4014 // involved.
4015 if (SourceVecs.size() > 2)
4016 return SDValue();
4017
4018 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4019 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004020
Bob Wilson11a1dff2011-01-07 21:37:30 +00004021 // This loop extracts the usage patterns of the source vectors
4022 // and prepares appropriate SDValues for a shuffle if possible.
4023 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4024 if (SourceVecs[i].getValueType() == VT) {
4025 // No VEXT necessary
4026 ShuffleSrcs[i] = SourceVecs[i];
4027 VEXTOffsets[i] = 0;
4028 continue;
4029 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4030 // It probably isn't worth padding out a smaller vector just to
4031 // break it down again in a shuffle.
4032 return SDValue();
4033 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004034
Bob Wilson11a1dff2011-01-07 21:37:30 +00004035 // Since only 64-bit and 128-bit vectors are legal on ARM and
4036 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004037 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4038 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004039
Bob Wilson11a1dff2011-01-07 21:37:30 +00004040 if (MaxElts[i] - MinElts[i] >= NumElts) {
4041 // Span too large for a VEXT to cope
4042 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004043 }
4044
Bob Wilson11a1dff2011-01-07 21:37:30 +00004045 if (MinElts[i] >= NumElts) {
4046 // The extraction can just take the second half
4047 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004048 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4049 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004050 DAG.getIntPtrConstant(NumElts));
4051 } else if (MaxElts[i] < NumElts) {
4052 // The extraction can just take the first half
4053 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004054 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4055 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004056 DAG.getIntPtrConstant(0));
4057 } else {
4058 // An actual VEXT is needed
4059 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004060 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4061 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004062 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004063 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4064 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004065 DAG.getIntPtrConstant(NumElts));
4066 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4067 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4068 }
4069 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004070
Bob Wilson11a1dff2011-01-07 21:37:30 +00004071 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004072
Bob Wilson11a1dff2011-01-07 21:37:30 +00004073 for (unsigned i = 0; i < NumElts; ++i) {
4074 SDValue Entry = Op.getOperand(i);
4075 if (Entry.getOpcode() == ISD::UNDEF) {
4076 Mask.push_back(-1);
4077 continue;
4078 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004079
Bob Wilson11a1dff2011-01-07 21:37:30 +00004080 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004081 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4082 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004083 if (ExtractVec == SourceVecs[0]) {
4084 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4085 } else {
4086 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4087 }
4088 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004089
Bob Wilson11a1dff2011-01-07 21:37:30 +00004090 // Final check before we try to produce nonsense...
4091 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004092 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4093 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004094
Bob Wilson11a1dff2011-01-07 21:37:30 +00004095 return SDValue();
4096}
4097
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004098/// isShuffleMaskLegal - Targets can use this to indicate that they only
4099/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4100/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4101/// are assumed to be legal.
4102bool
4103ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4104 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004105 if (VT.getVectorNumElements() == 4 &&
4106 (VT.is128BitVector() || VT.is64BitVector())) {
4107 unsigned PFIndexes[4];
4108 for (unsigned i = 0; i != 4; ++i) {
4109 if (M[i] < 0)
4110 PFIndexes[i] = 8;
4111 else
4112 PFIndexes[i] = M[i];
4113 }
4114
4115 // Compute the index in the perfect shuffle table.
4116 unsigned PFTableIndex =
4117 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4118 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4119 unsigned Cost = (PFEntry >> 30);
4120
4121 if (Cost <= 4)
4122 return true;
4123 }
4124
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004125 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004126 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004127
Bob Wilson53dd2452010-06-07 23:53:38 +00004128 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4129 return (EltSize >= 32 ||
4130 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004131 isVREVMask(M, VT, 64) ||
4132 isVREVMask(M, VT, 32) ||
4133 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004134 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004135 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004136 isVTRNMask(M, VT, WhichResult) ||
4137 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004138 isVZIPMask(M, VT, WhichResult) ||
4139 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4140 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4141 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004142}
4143
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004144/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4145/// the specified operations to build the shuffle.
4146static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4147 SDValue RHS, SelectionDAG &DAG,
4148 DebugLoc dl) {
4149 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4150 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4151 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4152
4153 enum {
4154 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4155 OP_VREV,
4156 OP_VDUP0,
4157 OP_VDUP1,
4158 OP_VDUP2,
4159 OP_VDUP3,
4160 OP_VEXT1,
4161 OP_VEXT2,
4162 OP_VEXT3,
4163 OP_VUZPL, // VUZP, left result
4164 OP_VUZPR, // VUZP, right result
4165 OP_VZIPL, // VZIP, left result
4166 OP_VZIPR, // VZIP, right result
4167 OP_VTRNL, // VTRN, left result
4168 OP_VTRNR // VTRN, right result
4169 };
4170
4171 if (OpNum == OP_COPY) {
4172 if (LHSID == (1*9+2)*9+3) return LHS;
4173 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4174 return RHS;
4175 }
4176
4177 SDValue OpLHS, OpRHS;
4178 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4179 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4180 EVT VT = OpLHS.getValueType();
4181
4182 switch (OpNum) {
4183 default: llvm_unreachable("Unknown shuffle opcode!");
4184 case OP_VREV:
4185 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4186 case OP_VDUP0:
4187 case OP_VDUP1:
4188 case OP_VDUP2:
4189 case OP_VDUP3:
4190 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004191 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004192 case OP_VEXT1:
4193 case OP_VEXT2:
4194 case OP_VEXT3:
4195 return DAG.getNode(ARMISD::VEXT, dl, VT,
4196 OpLHS, OpRHS,
4197 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4198 case OP_VUZPL:
4199 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004200 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004201 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4202 case OP_VZIPL:
4203 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004204 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004205 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4206 case OP_VTRNL:
4207 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004208 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4209 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004210 }
4211}
4212
Bill Wendling69a05a72011-03-14 23:02:38 +00004213static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4214 SmallVectorImpl<int> &ShuffleMask,
4215 SelectionDAG &DAG) {
4216 // Check to see if we can use the VTBL instruction.
4217 SDValue V1 = Op.getOperand(0);
4218 SDValue V2 = Op.getOperand(1);
4219 DebugLoc DL = Op.getDebugLoc();
4220
4221 SmallVector<SDValue, 8> VTBLMask;
4222 for (SmallVectorImpl<int>::iterator
4223 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4224 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4225
4226 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4227 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4228 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4229 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004230
Owen Anderson76706012011-04-05 21:48:57 +00004231 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004232 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4233 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004234}
4235
Bob Wilson5bafff32009-06-22 23:27:02 +00004236static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004237 SDValue V1 = Op.getOperand(0);
4238 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004239 DebugLoc dl = Op.getDebugLoc();
4240 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004241 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004242 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004243
Bob Wilson28865062009-08-13 02:13:04 +00004244 // Convert shuffles that are directly supported on NEON to target-specific
4245 // DAG nodes, instead of keeping them as shuffles and matching them again
4246 // during code selection. This is more efficient and avoids the possibility
4247 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004248 // FIXME: floating-point vectors should be canonicalized to integer vectors
4249 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004250 SVN->getMask(ShuffleMask);
4251
Bob Wilson53dd2452010-06-07 23:53:38 +00004252 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4253 if (EltSize <= 32) {
4254 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4255 int Lane = SVN->getSplatIndex();
4256 // If this is undef splat, generate it via "just" vdup, if possible.
4257 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004258
Bob Wilson53dd2452010-06-07 23:53:38 +00004259 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4260 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4261 }
4262 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4263 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004264 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004265
4266 bool ReverseVEXT;
4267 unsigned Imm;
4268 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4269 if (ReverseVEXT)
4270 std::swap(V1, V2);
4271 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4272 DAG.getConstant(Imm, MVT::i32));
4273 }
4274
4275 if (isVREVMask(ShuffleMask, VT, 64))
4276 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4277 if (isVREVMask(ShuffleMask, VT, 32))
4278 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4279 if (isVREVMask(ShuffleMask, VT, 16))
4280 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4281
4282 // Check for Neon shuffles that modify both input vectors in place.
4283 // If both results are used, i.e., if there are two shuffles with the same
4284 // source operands and with masks corresponding to both results of one of
4285 // these operations, DAG memoization will ensure that a single node is
4286 // used for both shuffles.
4287 unsigned WhichResult;
4288 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4289 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4290 V1, V2).getValue(WhichResult);
4291 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4292 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4293 V1, V2).getValue(WhichResult);
4294 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4295 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4296 V1, V2).getValue(WhichResult);
4297
4298 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4299 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4300 V1, V1).getValue(WhichResult);
4301 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4302 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4303 V1, V1).getValue(WhichResult);
4304 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4305 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4306 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004307 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004308
Bob Wilsonc692cb72009-08-21 20:54:19 +00004309 // If the shuffle is not directly supported and it has 4 elements, use
4310 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004311 unsigned NumElts = VT.getVectorNumElements();
4312 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004313 unsigned PFIndexes[4];
4314 for (unsigned i = 0; i != 4; ++i) {
4315 if (ShuffleMask[i] < 0)
4316 PFIndexes[i] = 8;
4317 else
4318 PFIndexes[i] = ShuffleMask[i];
4319 }
4320
4321 // Compute the index in the perfect shuffle table.
4322 unsigned PFTableIndex =
4323 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004324 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4325 unsigned Cost = (PFEntry >> 30);
4326
4327 if (Cost <= 4)
4328 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4329 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004330
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004331 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004332 if (EltSize >= 32) {
4333 // Do the expansion with floating-point types, since that is what the VFP
4334 // registers are defined to use, and since i64 is not legal.
4335 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4336 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004337 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4338 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004339 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004340 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004341 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004342 Ops.push_back(DAG.getUNDEF(EltVT));
4343 else
4344 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4345 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4346 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4347 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004348 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004349 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004350 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004351 }
4352
Bill Wendling69a05a72011-03-14 23:02:38 +00004353 if (VT == MVT::v8i8) {
4354 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4355 if (NewOp.getNode())
4356 return NewOp;
4357 }
4358
Bob Wilson22cac0d2009-08-14 05:16:33 +00004359 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004360}
4361
Bob Wilson5bafff32009-06-22 23:27:02 +00004362static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004363 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004364 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004365 if (!isa<ConstantSDNode>(Lane))
4366 return SDValue();
4367
4368 SDValue Vec = Op.getOperand(0);
4369 if (Op.getValueType() == MVT::i32 &&
4370 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4371 DebugLoc dl = Op.getDebugLoc();
4372 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4373 }
4374
4375 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004376}
4377
Bob Wilsona6d65862009-08-03 20:36:38 +00004378static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4379 // The only time a CONCAT_VECTORS operation can have legal types is when
4380 // two 64-bit vectors are concatenated to a 128-bit vector.
4381 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4382 "unexpected CONCAT_VECTORS");
4383 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004384 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004385 SDValue Op0 = Op.getOperand(0);
4386 SDValue Op1 = Op.getOperand(1);
4387 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004388 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004389 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004390 DAG.getIntPtrConstant(0));
4391 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004392 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004393 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004394 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004395 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004396}
4397
Bob Wilson626613d2010-11-23 19:38:38 +00004398/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4399/// element has been zero/sign-extended, depending on the isSigned parameter,
4400/// from an integer type half its size.
4401static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4402 bool isSigned) {
4403 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4404 EVT VT = N->getValueType(0);
4405 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4406 SDNode *BVN = N->getOperand(0).getNode();
4407 if (BVN->getValueType(0) != MVT::v4i32 ||
4408 BVN->getOpcode() != ISD::BUILD_VECTOR)
4409 return false;
4410 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4411 unsigned HiElt = 1 - LoElt;
4412 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4413 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4414 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4415 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4416 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4417 return false;
4418 if (isSigned) {
4419 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4420 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4421 return true;
4422 } else {
4423 if (Hi0->isNullValue() && Hi1->isNullValue())
4424 return true;
4425 }
4426 return false;
4427 }
4428
4429 if (N->getOpcode() != ISD::BUILD_VECTOR)
4430 return false;
4431
4432 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4433 SDNode *Elt = N->getOperand(i).getNode();
4434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4435 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4436 unsigned HalfSize = EltSize / 2;
4437 if (isSigned) {
4438 int64_t SExtVal = C->getSExtValue();
4439 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4440 return false;
4441 } else {
4442 if ((C->getZExtValue() >> HalfSize) != 0)
4443 return false;
4444 }
4445 continue;
4446 }
4447 return false;
4448 }
4449
4450 return true;
4451}
4452
4453/// isSignExtended - Check if a node is a vector value that is sign-extended
4454/// or a constant BUILD_VECTOR with sign-extended elements.
4455static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4456 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4457 return true;
4458 if (isExtendedBUILD_VECTOR(N, DAG, true))
4459 return true;
4460 return false;
4461}
4462
4463/// isZeroExtended - Check if a node is a vector value that is zero-extended
4464/// or a constant BUILD_VECTOR with zero-extended elements.
4465static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4466 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4467 return true;
4468 if (isExtendedBUILD_VECTOR(N, DAG, false))
4469 return true;
4470 return false;
4471}
4472
4473/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4474/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004475static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4476 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4477 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004478 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4479 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4480 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4481 LD->isNonTemporal(), LD->getAlignment());
4482 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4483 // have been legalized as a BITCAST from v4i32.
4484 if (N->getOpcode() == ISD::BITCAST) {
4485 SDNode *BVN = N->getOperand(0).getNode();
4486 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4487 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4488 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4489 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4490 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4491 }
4492 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4493 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4494 EVT VT = N->getValueType(0);
4495 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4496 unsigned NumElts = VT.getVectorNumElements();
4497 MVT TruncVT = MVT::getIntegerVT(EltSize);
4498 SmallVector<SDValue, 8> Ops;
4499 for (unsigned i = 0; i != NumElts; ++i) {
4500 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4501 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004502 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004503 }
4504 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4505 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004506}
4507
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004508static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4509 unsigned Opcode = N->getOpcode();
4510 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4511 SDNode *N0 = N->getOperand(0).getNode();
4512 SDNode *N1 = N->getOperand(1).getNode();
4513 return N0->hasOneUse() && N1->hasOneUse() &&
4514 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4515 }
4516 return false;
4517}
4518
4519static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4520 unsigned Opcode = N->getOpcode();
4521 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4522 SDNode *N0 = N->getOperand(0).getNode();
4523 SDNode *N1 = N->getOperand(1).getNode();
4524 return N0->hasOneUse() && N1->hasOneUse() &&
4525 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4526 }
4527 return false;
4528}
4529
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004530static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4531 // Multiplications are only custom-lowered for 128-bit vectors so that
4532 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4533 EVT VT = Op.getValueType();
4534 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4535 SDNode *N0 = Op.getOperand(0).getNode();
4536 SDNode *N1 = Op.getOperand(1).getNode();
4537 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004538 bool isMLA = false;
4539 bool isN0SExt = isSignExtended(N0, DAG);
4540 bool isN1SExt = isSignExtended(N1, DAG);
4541 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004542 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004543 else {
4544 bool isN0ZExt = isZeroExtended(N0, DAG);
4545 bool isN1ZExt = isZeroExtended(N1, DAG);
4546 if (isN0ZExt && isN1ZExt)
4547 NewOpc = ARMISD::VMULLu;
4548 else if (isN1SExt || isN1ZExt) {
4549 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4550 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4551 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4552 NewOpc = ARMISD::VMULLs;
4553 isMLA = true;
4554 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4555 NewOpc = ARMISD::VMULLu;
4556 isMLA = true;
4557 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4558 std::swap(N0, N1);
4559 NewOpc = ARMISD::VMULLu;
4560 isMLA = true;
4561 }
4562 }
4563
4564 if (!NewOpc) {
4565 if (VT == MVT::v2i64)
4566 // Fall through to expand this. It is not legal.
4567 return SDValue();
4568 else
4569 // Other vector multiplications are legal.
4570 return Op;
4571 }
4572 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004573
4574 // Legalize to a VMULL instruction.
4575 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004576 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004577 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004578 if (!isMLA) {
4579 Op0 = SkipExtension(N0, DAG);
4580 assert(Op0.getValueType().is64BitVector() &&
4581 Op1.getValueType().is64BitVector() &&
4582 "unexpected types for extended operands to VMULL");
4583 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4584 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004585
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004586 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4587 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4588 // vmull q0, d4, d6
4589 // vmlal q0, d5, d6
4590 // is faster than
4591 // vaddl q0, d4, d5
4592 // vmovl q1, d6
4593 // vmul q0, q0, q1
4594 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4595 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4596 EVT Op1VT = Op1.getValueType();
4597 return DAG.getNode(N0->getOpcode(), DL, VT,
4598 DAG.getNode(NewOpc, DL, VT,
4599 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4600 DAG.getNode(NewOpc, DL, VT,
4601 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004602}
4603
Owen Anderson76706012011-04-05 21:48:57 +00004604static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004605LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4606 // Convert to float
4607 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4608 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4609 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4610 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4611 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4612 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4613 // Get reciprocal estimate.
4614 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004615 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004616 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4617 // Because char has a smaller range than uchar, we can actually get away
4618 // without any newton steps. This requires that we use a weird bias
4619 // of 0xb000, however (again, this has been exhaustively tested).
4620 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4621 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4622 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4623 Y = DAG.getConstant(0xb000, MVT::i32);
4624 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4625 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4626 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4627 // Convert back to short.
4628 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4629 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4630 return X;
4631}
4632
Owen Anderson76706012011-04-05 21:48:57 +00004633static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004634LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4635 SDValue N2;
4636 // Convert to float.
4637 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4638 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4639 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4640 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4641 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4642 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004643
Nate Begeman7973f352011-02-11 20:53:29 +00004644 // Use reciprocal estimate and one refinement step.
4645 // float4 recip = vrecpeq_f32(yf);
4646 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004647 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004648 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004649 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004650 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4651 N1, N2);
4652 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4653 // Because short has a smaller range than ushort, we can actually get away
4654 // with only a single newton step. This requires that we use a weird bias
4655 // of 89, however (again, this has been exhaustively tested).
4656 // float4 result = as_float4(as_int4(xf*recip) + 89);
4657 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4658 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4659 N1 = DAG.getConstant(89, MVT::i32);
4660 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4661 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4662 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4663 // Convert back to integer and return.
4664 // return vmovn_s32(vcvt_s32_f32(result));
4665 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4666 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4667 return N0;
4668}
4669
4670static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4671 EVT VT = Op.getValueType();
4672 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4673 "unexpected type for custom-lowering ISD::SDIV");
4674
4675 DebugLoc dl = Op.getDebugLoc();
4676 SDValue N0 = Op.getOperand(0);
4677 SDValue N1 = Op.getOperand(1);
4678 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004679
Nate Begeman7973f352011-02-11 20:53:29 +00004680 if (VT == MVT::v8i8) {
4681 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4682 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004683
Nate Begeman7973f352011-02-11 20:53:29 +00004684 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4685 DAG.getIntPtrConstant(4));
4686 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004687 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004688 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4689 DAG.getIntPtrConstant(0));
4690 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4691 DAG.getIntPtrConstant(0));
4692
4693 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4694 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4695
4696 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4697 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004698
Nate Begeman7973f352011-02-11 20:53:29 +00004699 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4700 return N0;
4701 }
4702 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4703}
4704
4705static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4706 EVT VT = Op.getValueType();
4707 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4708 "unexpected type for custom-lowering ISD::UDIV");
4709
4710 DebugLoc dl = Op.getDebugLoc();
4711 SDValue N0 = Op.getOperand(0);
4712 SDValue N1 = Op.getOperand(1);
4713 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004714
Nate Begeman7973f352011-02-11 20:53:29 +00004715 if (VT == MVT::v8i8) {
4716 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4717 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004718
Nate Begeman7973f352011-02-11 20:53:29 +00004719 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4720 DAG.getIntPtrConstant(4));
4721 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004722 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004723 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4724 DAG.getIntPtrConstant(0));
4725 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4726 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004727
Nate Begeman7973f352011-02-11 20:53:29 +00004728 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4729 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004730
Nate Begeman7973f352011-02-11 20:53:29 +00004731 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4732 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004733
4734 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004735 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4736 N0);
4737 return N0;
4738 }
Owen Anderson76706012011-04-05 21:48:57 +00004739
Nate Begeman7973f352011-02-11 20:53:29 +00004740 // v4i16 sdiv ... Convert to float.
4741 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4742 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4743 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4744 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4745 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4746 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4747
4748 // Use reciprocal estimate and two refinement steps.
4749 // float4 recip = vrecpeq_f32(yf);
4750 // recip *= vrecpsq_f32(yf, recip);
4751 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004752 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004753 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004754 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004755 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4756 N1, N2);
4757 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004758 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004759 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4760 N1, N2);
4761 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4762 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4763 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4764 // and that it will never cause us to return an answer too large).
4765 // float4 result = as_float4(as_int4(xf*recip) + 89);
4766 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4767 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4768 N1 = DAG.getConstant(2, MVT::i32);
4769 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4770 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4771 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4772 // Convert back to integer and return.
4773 // return vmovn_u32(vcvt_s32_f32(result));
4774 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4775 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4776 return N0;
4777}
4778
Dan Gohmand858e902010-04-17 15:26:15 +00004779SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004780 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004781 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004782 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004783 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004784 case ISD::GlobalAddress:
4785 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4786 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004787 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004788 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004789 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4790 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004791 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004792 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004793 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004794 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004795 case ISD::SINT_TO_FP:
4796 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4797 case ISD::FP_TO_SINT:
4798 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004799 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004800 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004801 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004802 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004803 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004804 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004805 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004806 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4807 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004808 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004809 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004810 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004811 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004812 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004813 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004814 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004815 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004816 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004817 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004818 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004819 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004820 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004821 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004822 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004823 case ISD::SDIV: return LowerSDIV(Op, DAG);
4824 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004825 }
Dan Gohman475871a2008-07-27 21:46:04 +00004826 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004827}
4828
Duncan Sands1607f052008-12-01 11:39:25 +00004829/// ReplaceNodeResults - Replace the results of node with an illegal result
4830/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004831void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4832 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004833 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004834 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004835 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004836 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004837 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004838 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004839 case ISD::BITCAST:
4840 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004841 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004842 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004843 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004844 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004845 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004846 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004847 if (Res.getNode())
4848 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004849}
Chris Lattner27a6c732007-11-24 07:07:01 +00004850
Evan Chenga8e29892007-01-19 07:51:42 +00004851//===----------------------------------------------------------------------===//
4852// ARM Scheduler Hooks
4853//===----------------------------------------------------------------------===//
4854
4855MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004856ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4857 MachineBasicBlock *BB,
4858 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004859 unsigned dest = MI->getOperand(0).getReg();
4860 unsigned ptr = MI->getOperand(1).getReg();
4861 unsigned oldval = MI->getOperand(2).getReg();
4862 unsigned newval = MI->getOperand(3).getReg();
4863 unsigned scratch = BB->getParent()->getRegInfo()
4864 .createVirtualRegister(ARM::GPRRegisterClass);
4865 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4866 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004867 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004868
4869 unsigned ldrOpc, strOpc;
4870 switch (Size) {
4871 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004872 case 1:
4873 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004874 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004875 break;
4876 case 2:
4877 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4878 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4879 break;
4880 case 4:
4881 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4882 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4883 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004884 }
4885
4886 MachineFunction *MF = BB->getParent();
4887 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4888 MachineFunction::iterator It = BB;
4889 ++It; // insert the new blocks after the current block
4890
4891 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4892 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4893 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4894 MF->insert(It, loop1MBB);
4895 MF->insert(It, loop2MBB);
4896 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004897
4898 // Transfer the remainder of BB and its successor edges to exitMBB.
4899 exitMBB->splice(exitMBB->begin(), BB,
4900 llvm::next(MachineBasicBlock::iterator(MI)),
4901 BB->end());
4902 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004903
4904 // thisMBB:
4905 // ...
4906 // fallthrough --> loop1MBB
4907 BB->addSuccessor(loop1MBB);
4908
4909 // loop1MBB:
4910 // ldrex dest, [ptr]
4911 // cmp dest, oldval
4912 // bne exitMBB
4913 BB = loop1MBB;
4914 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004915 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004916 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004917 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4918 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004919 BB->addSuccessor(loop2MBB);
4920 BB->addSuccessor(exitMBB);
4921
4922 // loop2MBB:
4923 // strex scratch, newval, [ptr]
4924 // cmp scratch, #0
4925 // bne loop1MBB
4926 BB = loop2MBB;
4927 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4928 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004929 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004930 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004931 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4932 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004933 BB->addSuccessor(loop1MBB);
4934 BB->addSuccessor(exitMBB);
4935
4936 // exitMBB:
4937 // ...
4938 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004939
Dan Gohman14152b42010-07-06 20:24:04 +00004940 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004941
Jim Grosbach5278eb82009-12-11 01:42:04 +00004942 return BB;
4943}
4944
4945MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004946ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4947 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004948 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4949 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4950
4951 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004952 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004953 MachineFunction::iterator It = BB;
4954 ++It;
4955
4956 unsigned dest = MI->getOperand(0).getReg();
4957 unsigned ptr = MI->getOperand(1).getReg();
4958 unsigned incr = MI->getOperand(2).getReg();
4959 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004960
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004961 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004962 unsigned ldrOpc, strOpc;
4963 switch (Size) {
4964 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004965 case 1:
4966 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004967 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004968 break;
4969 case 2:
4970 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4971 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4972 break;
4973 case 4:
4974 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4975 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4976 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004977 }
4978
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004979 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4980 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4981 MF->insert(It, loopMBB);
4982 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004983
4984 // Transfer the remainder of BB and its successor edges to exitMBB.
4985 exitMBB->splice(exitMBB->begin(), BB,
4986 llvm::next(MachineBasicBlock::iterator(MI)),
4987 BB->end());
4988 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004989
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004990 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004991 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4992 unsigned scratch2 = (!BinOpcode) ? incr :
4993 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4994
4995 // thisMBB:
4996 // ...
4997 // fallthrough --> loopMBB
4998 BB->addSuccessor(loopMBB);
4999
5000 // loopMBB:
5001 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005002 // <binop> scratch2, dest, incr
5003 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005004 // cmp scratch, #0
5005 // bne- loopMBB
5006 // fallthrough --> exitMBB
5007 BB = loopMBB;
5008 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005009 if (BinOpcode) {
5010 // operand order needs to go the other way for NAND
5011 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5012 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5013 addReg(incr).addReg(dest)).addReg(0);
5014 else
5015 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5016 addReg(dest).addReg(incr)).addReg(0);
5017 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005018
5019 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5020 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005021 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005022 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005023 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5024 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005025
5026 BB->addSuccessor(loopMBB);
5027 BB->addSuccessor(exitMBB);
5028
5029 // exitMBB:
5030 // ...
5031 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005032
Dan Gohman14152b42010-07-06 20:24:04 +00005033 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005034
Jim Grosbachc3c23542009-12-14 04:22:04 +00005035 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005036}
5037
Evan Cheng218977b2010-07-13 19:27:42 +00005038static
5039MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5040 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5041 E = MBB->succ_end(); I != E; ++I)
5042 if (*I != Succ)
5043 return *I;
5044 llvm_unreachable("Expecting a BB with two successors!");
5045}
5046
Jim Grosbache801dc42009-12-12 01:40:06 +00005047MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005048ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005049 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005050 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005051 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005052 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005053 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00005054 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00005055 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005056 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00005057
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005058 case ARM::ATOMIC_LOAD_ADD_I8:
5059 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5060 case ARM::ATOMIC_LOAD_ADD_I16:
5061 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5062 case ARM::ATOMIC_LOAD_ADD_I32:
5063 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005064
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005065 case ARM::ATOMIC_LOAD_AND_I8:
5066 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5067 case ARM::ATOMIC_LOAD_AND_I16:
5068 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5069 case ARM::ATOMIC_LOAD_AND_I32:
5070 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005071
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005072 case ARM::ATOMIC_LOAD_OR_I8:
5073 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5074 case ARM::ATOMIC_LOAD_OR_I16:
5075 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5076 case ARM::ATOMIC_LOAD_OR_I32:
5077 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005078
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005079 case ARM::ATOMIC_LOAD_XOR_I8:
5080 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5081 case ARM::ATOMIC_LOAD_XOR_I16:
5082 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5083 case ARM::ATOMIC_LOAD_XOR_I32:
5084 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005085
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005086 case ARM::ATOMIC_LOAD_NAND_I8:
5087 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5088 case ARM::ATOMIC_LOAD_NAND_I16:
5089 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5090 case ARM::ATOMIC_LOAD_NAND_I32:
5091 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005092
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005093 case ARM::ATOMIC_LOAD_SUB_I8:
5094 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5095 case ARM::ATOMIC_LOAD_SUB_I16:
5096 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5097 case ARM::ATOMIC_LOAD_SUB_I32:
5098 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005099
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005100 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5101 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5102 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005103
5104 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5105 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5106 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005107
Owen Anderson76706012011-04-05 21:48:57 +00005108 case ARM::ADCSSri:
5109 case ARM::ADCSSrr:
5110 case ARM::ADCSSrs:
5111 case ARM::SBCSSri:
5112 case ARM::SBCSSrr:
Owen Andersonb48c7912011-04-05 23:55:28 +00005113 case ARM::SBCSSrs:
5114 case ARM::RSBSri:
5115 case ARM::RSBSrr:
5116 case ARM::RSBSrs:
5117 case ARM::RSCSri:
5118 case ARM::RSCSrs: {
Owen Anderson76706012011-04-05 21:48:57 +00005119 unsigned OldOpc = MI->getOpcode();
5120 unsigned Opc = 0;
5121 switch (OldOpc) {
5122 case ARM::ADCSSrr:
5123 Opc = ARM::ADCrr;
5124 break;
5125 case ARM::ADCSSri:
5126 Opc = ARM::ADCri;
5127 break;
5128 case ARM::ADCSSrs:
5129 Opc = ARM::ADCrs;
5130 break;
5131 case ARM::SBCSSrr:
5132 Opc = ARM::SBCrr;
5133 break;
5134 case ARM::SBCSSri:
5135 Opc = ARM::SBCri;
5136 break;
5137 case ARM::SBCSSrs:
5138 Opc = ARM::SBCrs;
5139 break;
Owen Andersonb48c7912011-04-05 23:55:28 +00005140 case ARM::RSBSri:
5141 Opc = ARM::RSBri;
5142 break;
5143 case ARM::RSBSrr:
5144 Opc = ARM::RSBrr;
5145 break;
5146 case ARM::RSBSrs:
5147 Opc = ARM::RSBrs;
5148 break;
5149 case ARM::RSCSri:
5150 Opc = ARM::RSCri;
5151 break;
5152 case ARM::RSCSrs:
5153 Opc = ARM::RSCrs;
5154 break;
Owen Anderson76706012011-04-05 21:48:57 +00005155 default:
5156 llvm_unreachable("Unknown opcode?");
5157 }
5158
5159 MachineInstrBuilder MIB =
5160 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(Opc));
5161 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5162 MIB.addOperand(MI->getOperand(i));
5163 AddDefaultPred(MIB);
5164 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5165 MI->eraseFromParent();
5166 return BB;
5167 }
5168
5169
Evan Cheng007ea272009-08-12 05:17:19 +00005170 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005171 // To "insert" a SELECT_CC instruction, we actually have to insert the
5172 // diamond control-flow pattern. The incoming instruction knows the
5173 // destination vreg to set, the condition code register to branch on, the
5174 // true/false values to select between, and a branch opcode to use.
5175 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005176 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005177 ++It;
5178
5179 // thisMBB:
5180 // ...
5181 // TrueVal = ...
5182 // cmpTY ccX, r1, r2
5183 // bCC copy1MBB
5184 // fallthrough --> copy0MBB
5185 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005186 MachineFunction *F = BB->getParent();
5187 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5188 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005189 F->insert(It, copy0MBB);
5190 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005191
5192 // Transfer the remainder of BB and its successor edges to sinkMBB.
5193 sinkMBB->splice(sinkMBB->begin(), BB,
5194 llvm::next(MachineBasicBlock::iterator(MI)),
5195 BB->end());
5196 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5197
Dan Gohman258c58c2010-07-06 15:49:48 +00005198 BB->addSuccessor(copy0MBB);
5199 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005200
Dan Gohman14152b42010-07-06 20:24:04 +00005201 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5202 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5203
Evan Chenga8e29892007-01-19 07:51:42 +00005204 // copy0MBB:
5205 // %FalseValue = ...
5206 // # fallthrough to sinkMBB
5207 BB = copy0MBB;
5208
5209 // Update machine-CFG edges
5210 BB->addSuccessor(sinkMBB);
5211
5212 // sinkMBB:
5213 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5214 // ...
5215 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005216 BuildMI(*BB, BB->begin(), dl,
5217 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005218 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5219 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5220
Dan Gohman14152b42010-07-06 20:24:04 +00005221 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005222 return BB;
5223 }
Evan Cheng86198642009-08-07 00:34:42 +00005224
Evan Cheng218977b2010-07-13 19:27:42 +00005225 case ARM::BCCi64:
5226 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005227 // If there is an unconditional branch to the other successor, remove it.
5228 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005229
Evan Cheng218977b2010-07-13 19:27:42 +00005230 // Compare both parts that make up the double comparison separately for
5231 // equality.
5232 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5233
5234 unsigned LHS1 = MI->getOperand(1).getReg();
5235 unsigned LHS2 = MI->getOperand(2).getReg();
5236 if (RHSisZero) {
5237 AddDefaultPred(BuildMI(BB, dl,
5238 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5239 .addReg(LHS1).addImm(0));
5240 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5241 .addReg(LHS2).addImm(0)
5242 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5243 } else {
5244 unsigned RHS1 = MI->getOperand(3).getReg();
5245 unsigned RHS2 = MI->getOperand(4).getReg();
5246 AddDefaultPred(BuildMI(BB, dl,
5247 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5248 .addReg(LHS1).addReg(RHS1));
5249 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5250 .addReg(LHS2).addReg(RHS2)
5251 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5252 }
5253
5254 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5255 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5256 if (MI->getOperand(0).getImm() == ARMCC::NE)
5257 std::swap(destMBB, exitMBB);
5258
5259 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5260 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5261 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5262 .addMBB(exitMBB);
5263
5264 MI->eraseFromParent(); // The pseudo instruction is gone now.
5265 return BB;
5266 }
Evan Chenga8e29892007-01-19 07:51:42 +00005267 }
5268}
5269
5270//===----------------------------------------------------------------------===//
5271// ARM Optimization Hooks
5272//===----------------------------------------------------------------------===//
5273
Chris Lattnerd1980a52009-03-12 06:52:53 +00005274static
5275SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5276 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005277 SelectionDAG &DAG = DCI.DAG;
5278 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005279 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005280 unsigned Opc = N->getOpcode();
5281 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5282 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5283 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5284 ISD::CondCode CC = ISD::SETCC_INVALID;
5285
5286 if (isSlctCC) {
5287 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5288 } else {
5289 SDValue CCOp = Slct.getOperand(0);
5290 if (CCOp.getOpcode() == ISD::SETCC)
5291 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5292 }
5293
5294 bool DoXform = false;
5295 bool InvCC = false;
5296 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5297 "Bad input!");
5298
5299 if (LHS.getOpcode() == ISD::Constant &&
5300 cast<ConstantSDNode>(LHS)->isNullValue()) {
5301 DoXform = true;
5302 } else if (CC != ISD::SETCC_INVALID &&
5303 RHS.getOpcode() == ISD::Constant &&
5304 cast<ConstantSDNode>(RHS)->isNullValue()) {
5305 std::swap(LHS, RHS);
5306 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005307 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005308 Op0.getOperand(0).getValueType();
5309 bool isInt = OpVT.isInteger();
5310 CC = ISD::getSetCCInverse(CC, isInt);
5311
5312 if (!TLI.isCondCodeLegal(CC, OpVT))
5313 return SDValue(); // Inverse operator isn't legal.
5314
5315 DoXform = true;
5316 InvCC = true;
5317 }
5318
5319 if (DoXform) {
5320 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5321 if (isSlctCC)
5322 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5323 Slct.getOperand(0), Slct.getOperand(1), CC);
5324 SDValue CCOp = Slct.getOperand(0);
5325 if (InvCC)
5326 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5327 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5328 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5329 CCOp, OtherOp, Result);
5330 }
5331 return SDValue();
5332}
5333
Bob Wilson3d5792a2010-07-29 20:34:14 +00005334/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5335/// operands N0 and N1. This is a helper for PerformADDCombine that is
5336/// called with the default operands, and if that fails, with commuted
5337/// operands.
5338static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5339 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005340 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5341 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5342 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5343 if (Result.getNode()) return Result;
5344 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005345 return SDValue();
5346}
5347
Bob Wilson3d5792a2010-07-29 20:34:14 +00005348/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5349///
5350static SDValue PerformADDCombine(SDNode *N,
5351 TargetLowering::DAGCombinerInfo &DCI) {
5352 SDValue N0 = N->getOperand(0);
5353 SDValue N1 = N->getOperand(1);
5354
5355 // First try with the default operand order.
5356 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5357 if (Result.getNode())
5358 return Result;
5359
5360 // If that didn't work, try again with the operands commuted.
5361 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5362}
5363
Chris Lattnerd1980a52009-03-12 06:52:53 +00005364/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005365///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005366static SDValue PerformSUBCombine(SDNode *N,
5367 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005368 SDValue N0 = N->getOperand(0);
5369 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005370
Chris Lattnerd1980a52009-03-12 06:52:53 +00005371 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5372 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5373 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5374 if (Result.getNode()) return Result;
5375 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005376
Chris Lattnerd1980a52009-03-12 06:52:53 +00005377 return SDValue();
5378}
5379
Evan Cheng463d3582011-03-31 19:38:48 +00005380/// PerformVMULCombine
5381/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5382/// special multiplier accumulator forwarding.
5383/// vmul d3, d0, d2
5384/// vmla d3, d1, d2
5385/// is faster than
5386/// vadd d3, d0, d1
5387/// vmul d3, d3, d2
5388static SDValue PerformVMULCombine(SDNode *N,
5389 TargetLowering::DAGCombinerInfo &DCI,
5390 const ARMSubtarget *Subtarget) {
5391 if (!Subtarget->hasVMLxForwarding())
5392 return SDValue();
5393
5394 SelectionDAG &DAG = DCI.DAG;
5395 SDValue N0 = N->getOperand(0);
5396 SDValue N1 = N->getOperand(1);
5397 unsigned Opcode = N0.getOpcode();
5398 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5399 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5400 Opcode = N0.getOpcode();
5401 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5402 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5403 return SDValue();
5404 std::swap(N0, N1);
5405 }
5406
5407 EVT VT = N->getValueType(0);
5408 DebugLoc DL = N->getDebugLoc();
5409 SDValue N00 = N0->getOperand(0);
5410 SDValue N01 = N0->getOperand(1);
5411 return DAG.getNode(Opcode, DL, VT,
5412 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5413 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5414}
5415
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005416static SDValue PerformMULCombine(SDNode *N,
5417 TargetLowering::DAGCombinerInfo &DCI,
5418 const ARMSubtarget *Subtarget) {
5419 SelectionDAG &DAG = DCI.DAG;
5420
5421 if (Subtarget->isThumb1Only())
5422 return SDValue();
5423
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005424 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5425 return SDValue();
5426
5427 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005428 if (VT.is64BitVector() || VT.is128BitVector())
5429 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005430 if (VT != MVT::i32)
5431 return SDValue();
5432
5433 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5434 if (!C)
5435 return SDValue();
5436
5437 uint64_t MulAmt = C->getZExtValue();
5438 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5439 ShiftAmt = ShiftAmt & (32 - 1);
5440 SDValue V = N->getOperand(0);
5441 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005442
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005443 SDValue Res;
5444 MulAmt >>= ShiftAmt;
5445 if (isPowerOf2_32(MulAmt - 1)) {
5446 // (mul x, 2^N + 1) => (add (shl x, N), x)
5447 Res = DAG.getNode(ISD::ADD, DL, VT,
5448 V, DAG.getNode(ISD::SHL, DL, VT,
5449 V, DAG.getConstant(Log2_32(MulAmt-1),
5450 MVT::i32)));
5451 } else if (isPowerOf2_32(MulAmt + 1)) {
5452 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5453 Res = DAG.getNode(ISD::SUB, DL, VT,
5454 DAG.getNode(ISD::SHL, DL, VT,
5455 V, DAG.getConstant(Log2_32(MulAmt+1),
5456 MVT::i32)),
5457 V);
5458 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005459 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005460
5461 if (ShiftAmt != 0)
5462 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5463 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005464
5465 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005466 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005467 return SDValue();
5468}
5469
Owen Anderson080c0922010-11-05 19:27:46 +00005470static SDValue PerformANDCombine(SDNode *N,
5471 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005472
Owen Anderson080c0922010-11-05 19:27:46 +00005473 // Attempt to use immediate-form VBIC
5474 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5475 DebugLoc dl = N->getDebugLoc();
5476 EVT VT = N->getValueType(0);
5477 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005478
Tanya Lattner0433b212011-04-07 15:24:20 +00005479 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5480 return SDValue();
5481
Owen Anderson080c0922010-11-05 19:27:46 +00005482 APInt SplatBits, SplatUndef;
5483 unsigned SplatBitSize;
5484 bool HasAnyUndefs;
5485 if (BVN &&
5486 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5487 if (SplatBitSize <= 64) {
5488 EVT VbicVT;
5489 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5490 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005491 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005492 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005493 if (Val.getNode()) {
5494 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005495 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005496 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005497 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005498 }
5499 }
5500 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005501
Owen Anderson080c0922010-11-05 19:27:46 +00005502 return SDValue();
5503}
5504
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005505/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5506static SDValue PerformORCombine(SDNode *N,
5507 TargetLowering::DAGCombinerInfo &DCI,
5508 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005509 // Attempt to use immediate-form VORR
5510 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5511 DebugLoc dl = N->getDebugLoc();
5512 EVT VT = N->getValueType(0);
5513 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005514
Tanya Lattner0433b212011-04-07 15:24:20 +00005515 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5516 return SDValue();
5517
Owen Anderson60f48702010-11-03 23:15:26 +00005518 APInt SplatBits, SplatUndef;
5519 unsigned SplatBitSize;
5520 bool HasAnyUndefs;
5521 if (BVN && Subtarget->hasNEON() &&
5522 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5523 if (SplatBitSize <= 64) {
5524 EVT VorrVT;
5525 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5526 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005527 DAG, VorrVT, VT.is128BitVector(),
5528 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005529 if (Val.getNode()) {
5530 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005531 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005532 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005533 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005534 }
5535 }
5536 }
5537
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005538 SDValue N0 = N->getOperand(0);
5539 if (N0.getOpcode() != ISD::AND)
5540 return SDValue();
5541 SDValue N1 = N->getOperand(1);
5542
5543 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5544 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5545 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5546 APInt SplatUndef;
5547 unsigned SplatBitSize;
5548 bool HasAnyUndefs;
5549
5550 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5551 APInt SplatBits0;
5552 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5553 HasAnyUndefs) && !HasAnyUndefs) {
5554 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5555 APInt SplatBits1;
5556 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5557 HasAnyUndefs) && !HasAnyUndefs &&
5558 SplatBits0 == ~SplatBits1) {
5559 // Canonicalize the vector type to make instruction selection simpler.
5560 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5561 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5562 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005563 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005564 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5565 }
5566 }
5567 }
5568
Jim Grosbach54238562010-07-17 03:30:54 +00005569 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5570 // reasonable.
5571
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005572 // BFI is only available on V6T2+
5573 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5574 return SDValue();
5575
Jim Grosbach54238562010-07-17 03:30:54 +00005576 DebugLoc DL = N->getDebugLoc();
5577 // 1) or (and A, mask), val => ARMbfi A, val, mask
5578 // iff (val & mask) == val
5579 //
5580 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5581 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005582 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005583 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005584 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005585 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005586
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005587 if (VT != MVT::i32)
5588 return SDValue();
5589
Evan Cheng30fb13f2010-12-13 20:32:54 +00005590 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005591
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005592 // The value and the mask need to be constants so we can verify this is
5593 // actually a bitfield set. If the mask is 0xffff, we can do better
5594 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005595 SDValue MaskOp = N0.getOperand(1);
5596 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5597 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005598 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005599 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005600 if (Mask == 0xffff)
5601 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005602 SDValue Res;
5603 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005604 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5605 if (N1C) {
5606 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005607 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005608 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005609
Evan Chenga9688c42010-12-11 04:11:38 +00005610 if (ARM::isBitFieldInvertedMask(Mask)) {
5611 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005612
Evan Cheng30fb13f2010-12-13 20:32:54 +00005613 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005614 DAG.getConstant(Val, MVT::i32),
5615 DAG.getConstant(Mask, MVT::i32));
5616
5617 // Do not add new nodes to DAG combiner worklist.
5618 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005619 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005620 }
Jim Grosbach54238562010-07-17 03:30:54 +00005621 } else if (N1.getOpcode() == ISD::AND) {
5622 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005623 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5624 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005625 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005626 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005627
Eric Christopher29aeed12011-03-26 01:21:03 +00005628 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5629 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005630 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005631 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005632 // The pack halfword instruction works better for masks that fit it,
5633 // so use that when it's available.
5634 if (Subtarget->hasT2ExtractPack() &&
5635 (Mask == 0xffff || Mask == 0xffff0000))
5636 return SDValue();
5637 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005638 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005639 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005640 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005641 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005642 DAG.getConstant(Mask, MVT::i32));
5643 // Do not add new nodes to DAG combiner worklist.
5644 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005645 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005646 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005647 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005648 // The pack halfword instruction works better for masks that fit it,
5649 // so use that when it's available.
5650 if (Subtarget->hasT2ExtractPack() &&
5651 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5652 return SDValue();
5653 // 2b
5654 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005655 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005656 DAG.getConstant(lsb, MVT::i32));
5657 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005658 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005659 // Do not add new nodes to DAG combiner worklist.
5660 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005661 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005662 }
5663 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005664
Evan Cheng30fb13f2010-12-13 20:32:54 +00005665 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5666 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5667 ARM::isBitFieldInvertedMask(~Mask)) {
5668 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5669 // where lsb(mask) == #shamt and masked bits of B are known zero.
5670 SDValue ShAmt = N00.getOperand(1);
5671 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5672 unsigned LSB = CountTrailingZeros_32(Mask);
5673 if (ShAmtC != LSB)
5674 return SDValue();
5675
5676 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5677 DAG.getConstant(~Mask, MVT::i32));
5678
5679 // Do not add new nodes to DAG combiner worklist.
5680 DCI.CombineTo(N, Res, false);
5681 }
5682
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005683 return SDValue();
5684}
5685
Evan Cheng0c1aec12010-12-14 03:22:07 +00005686/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5687/// C1 & C2 == C1.
5688static SDValue PerformBFICombine(SDNode *N,
5689 TargetLowering::DAGCombinerInfo &DCI) {
5690 SDValue N1 = N->getOperand(1);
5691 if (N1.getOpcode() == ISD::AND) {
5692 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5693 if (!N11C)
5694 return SDValue();
5695 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5696 unsigned Mask2 = N11C->getZExtValue();
5697 if ((Mask & Mask2) == Mask2)
5698 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5699 N->getOperand(0), N1.getOperand(0),
5700 N->getOperand(2));
5701 }
5702 return SDValue();
5703}
5704
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005705/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5706/// ARMISD::VMOVRRD.
5707static SDValue PerformVMOVRRDCombine(SDNode *N,
5708 TargetLowering::DAGCombinerInfo &DCI) {
5709 // vmovrrd(vmovdrr x, y) -> x,y
5710 SDValue InDouble = N->getOperand(0);
5711 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5712 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00005713
5714 // vmovrrd(load f64) -> (load i32), (load i32)
5715 SDNode *InNode = InDouble.getNode();
5716 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
5717 InNode->getValueType(0) == MVT::f64 &&
5718 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
5719 !cast<LoadSDNode>(InNode)->isVolatile()) {
5720 // TODO: Should this be done for non-FrameIndex operands?
5721 LoadSDNode *LD = cast<LoadSDNode>(InNode);
5722
5723 SelectionDAG &DAG = DCI.DAG;
5724 DebugLoc DL = LD->getDebugLoc();
5725 SDValue BasePtr = LD->getBasePtr();
5726 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
5727 LD->getPointerInfo(), LD->isVolatile(),
5728 LD->isNonTemporal(), LD->getAlignment());
5729
5730 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5731 DAG.getConstant(4, MVT::i32));
5732 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
5733 LD->getPointerInfo(), LD->isVolatile(),
5734 LD->isNonTemporal(),
5735 std::min(4U, LD->getAlignment() / 2));
5736
5737 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
5738 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
5739 DCI.RemoveFromWorklist(LD);
5740 DAG.DeleteNode(LD);
5741 return Result;
5742 }
5743
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005744 return SDValue();
5745}
5746
5747/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5748/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5749static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5750 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5751 SDValue Op0 = N->getOperand(0);
5752 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005753 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005754 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005755 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005756 Op1 = Op1.getOperand(0);
5757 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5758 Op0.getNode() == Op1.getNode() &&
5759 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005760 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005761 N->getValueType(0), Op0.getOperand(0));
5762 return SDValue();
5763}
5764
Bob Wilson31600902010-12-21 06:43:19 +00005765/// PerformSTORECombine - Target-specific dag combine xforms for
5766/// ISD::STORE.
5767static SDValue PerformSTORECombine(SDNode *N,
5768 TargetLowering::DAGCombinerInfo &DCI) {
5769 // Bitcast an i64 store extracted from a vector to f64.
5770 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5771 StoreSDNode *St = cast<StoreSDNode>(N);
5772 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00005773 if (!ISD::isNormalStore(St) || St->isVolatile())
5774 return SDValue();
5775
5776 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
5777 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
5778 SelectionDAG &DAG = DCI.DAG;
5779 DebugLoc DL = St->getDebugLoc();
5780 SDValue BasePtr = St->getBasePtr();
5781 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
5782 StVal.getNode()->getOperand(0), BasePtr,
5783 St->getPointerInfo(), St->isVolatile(),
5784 St->isNonTemporal(), St->getAlignment());
5785
5786 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5787 DAG.getConstant(4, MVT::i32));
5788 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
5789 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
5790 St->isNonTemporal(),
5791 std::min(4U, St->getAlignment() / 2));
5792 }
5793
5794 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00005795 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5796 return SDValue();
5797
5798 SelectionDAG &DAG = DCI.DAG;
5799 DebugLoc dl = StVal.getDebugLoc();
5800 SDValue IntVec = StVal.getOperand(0);
5801 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5802 IntVec.getValueType().getVectorNumElements());
5803 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5804 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5805 Vec, StVal.getOperand(1));
5806 dl = N->getDebugLoc();
5807 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5808 // Make the DAGCombiner fold the bitcasts.
5809 DCI.AddToWorklist(Vec.getNode());
5810 DCI.AddToWorklist(ExtElt.getNode());
5811 DCI.AddToWorklist(V.getNode());
5812 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5813 St->getPointerInfo(), St->isVolatile(),
5814 St->isNonTemporal(), St->getAlignment(),
5815 St->getTBAAInfo());
5816}
5817
5818/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5819/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5820/// i64 vector to have f64 elements, since the value can then be loaded
5821/// directly into a VFP register.
5822static bool hasNormalLoadOperand(SDNode *N) {
5823 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5824 for (unsigned i = 0; i < NumElts; ++i) {
5825 SDNode *Elt = N->getOperand(i).getNode();
5826 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5827 return true;
5828 }
5829 return false;
5830}
5831
Bob Wilson75f02882010-09-17 22:59:05 +00005832/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5833/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005834static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5835 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005836 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5837 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5838 // into a pair of GPRs, which is fine when the value is used as a scalar,
5839 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005840 SelectionDAG &DAG = DCI.DAG;
5841 if (N->getNumOperands() == 2) {
5842 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5843 if (RV.getNode())
5844 return RV;
5845 }
Bob Wilson75f02882010-09-17 22:59:05 +00005846
Bob Wilson31600902010-12-21 06:43:19 +00005847 // Load i64 elements as f64 values so that type legalization does not split
5848 // them up into i32 values.
5849 EVT VT = N->getValueType(0);
5850 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5851 return SDValue();
5852 DebugLoc dl = N->getDebugLoc();
5853 SmallVector<SDValue, 8> Ops;
5854 unsigned NumElts = VT.getVectorNumElements();
5855 for (unsigned i = 0; i < NumElts; ++i) {
5856 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5857 Ops.push_back(V);
5858 // Make the DAGCombiner fold the bitcast.
5859 DCI.AddToWorklist(V.getNode());
5860 }
5861 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5862 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5863 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5864}
5865
5866/// PerformInsertEltCombine - Target-specific dag combine xforms for
5867/// ISD::INSERT_VECTOR_ELT.
5868static SDValue PerformInsertEltCombine(SDNode *N,
5869 TargetLowering::DAGCombinerInfo &DCI) {
5870 // Bitcast an i64 load inserted into a vector to f64.
5871 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5872 EVT VT = N->getValueType(0);
5873 SDNode *Elt = N->getOperand(1).getNode();
5874 if (VT.getVectorElementType() != MVT::i64 ||
5875 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5876 return SDValue();
5877
5878 SelectionDAG &DAG = DCI.DAG;
5879 DebugLoc dl = N->getDebugLoc();
5880 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5881 VT.getVectorNumElements());
5882 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5883 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5884 // Make the DAGCombiner fold the bitcasts.
5885 DCI.AddToWorklist(Vec.getNode());
5886 DCI.AddToWorklist(V.getNode());
5887 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5888 Vec, V, N->getOperand(2));
5889 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005890}
5891
Bob Wilsonf20700c2010-10-27 20:38:28 +00005892/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5893/// ISD::VECTOR_SHUFFLE.
5894static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5895 // The LLVM shufflevector instruction does not require the shuffle mask
5896 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5897 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5898 // operands do not match the mask length, they are extended by concatenating
5899 // them with undef vectors. That is probably the right thing for other
5900 // targets, but for NEON it is better to concatenate two double-register
5901 // size vector operands into a single quad-register size vector. Do that
5902 // transformation here:
5903 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5904 // shuffle(concat(v1, v2), undef)
5905 SDValue Op0 = N->getOperand(0);
5906 SDValue Op1 = N->getOperand(1);
5907 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5908 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5909 Op0.getNumOperands() != 2 ||
5910 Op1.getNumOperands() != 2)
5911 return SDValue();
5912 SDValue Concat0Op1 = Op0.getOperand(1);
5913 SDValue Concat1Op1 = Op1.getOperand(1);
5914 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5915 Concat1Op1.getOpcode() != ISD::UNDEF)
5916 return SDValue();
5917 // Skip the transformation if any of the types are illegal.
5918 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5919 EVT VT = N->getValueType(0);
5920 if (!TLI.isTypeLegal(VT) ||
5921 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5922 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5923 return SDValue();
5924
5925 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5926 Op0.getOperand(0), Op1.getOperand(0));
5927 // Translate the shuffle mask.
5928 SmallVector<int, 16> NewMask;
5929 unsigned NumElts = VT.getVectorNumElements();
5930 unsigned HalfElts = NumElts/2;
5931 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5932 for (unsigned n = 0; n < NumElts; ++n) {
5933 int MaskElt = SVN->getMaskElt(n);
5934 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005935 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005936 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005937 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005938 NewElt = HalfElts + MaskElt - NumElts;
5939 NewMask.push_back(NewElt);
5940 }
5941 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5942 DAG.getUNDEF(VT), NewMask.data());
5943}
5944
Bob Wilson1c3ef902011-02-07 17:43:21 +00005945/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5946/// NEON load/store intrinsics to merge base address updates.
5947static SDValue CombineBaseUpdate(SDNode *N,
5948 TargetLowering::DAGCombinerInfo &DCI) {
5949 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5950 return SDValue();
5951
5952 SelectionDAG &DAG = DCI.DAG;
5953 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5954 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5955 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5956 SDValue Addr = N->getOperand(AddrOpIdx);
5957
5958 // Search for a use of the address operand that is an increment.
5959 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5960 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5961 SDNode *User = *UI;
5962 if (User->getOpcode() != ISD::ADD ||
5963 UI.getUse().getResNo() != Addr.getResNo())
5964 continue;
5965
5966 // Check that the add is independent of the load/store. Otherwise, folding
5967 // it would create a cycle.
5968 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5969 continue;
5970
5971 // Find the new opcode for the updating load/store.
5972 bool isLoad = true;
5973 bool isLaneOp = false;
5974 unsigned NewOpc = 0;
5975 unsigned NumVecs = 0;
5976 if (isIntrinsic) {
5977 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5978 switch (IntNo) {
5979 default: assert(0 && "unexpected intrinsic for Neon base update");
5980 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5981 NumVecs = 1; break;
5982 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5983 NumVecs = 2; break;
5984 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5985 NumVecs = 3; break;
5986 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5987 NumVecs = 4; break;
5988 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5989 NumVecs = 2; isLaneOp = true; break;
5990 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5991 NumVecs = 3; isLaneOp = true; break;
5992 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5993 NumVecs = 4; isLaneOp = true; break;
5994 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5995 NumVecs = 1; isLoad = false; break;
5996 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
5997 NumVecs = 2; isLoad = false; break;
5998 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
5999 NumVecs = 3; isLoad = false; break;
6000 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6001 NumVecs = 4; isLoad = false; break;
6002 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6003 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6004 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6005 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6006 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6007 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6008 }
6009 } else {
6010 isLaneOp = true;
6011 switch (N->getOpcode()) {
6012 default: assert(0 && "unexpected opcode for Neon base update");
6013 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6014 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6015 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6016 }
6017 }
6018
6019 // Find the size of memory referenced by the load/store.
6020 EVT VecTy;
6021 if (isLoad)
6022 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006023 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006024 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6025 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6026 if (isLaneOp)
6027 NumBytes /= VecTy.getVectorNumElements();
6028
6029 // If the increment is a constant, it must match the memory ref size.
6030 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6031 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6032 uint64_t IncVal = CInc->getZExtValue();
6033 if (IncVal != NumBytes)
6034 continue;
6035 } else if (NumBytes >= 3 * 16) {
6036 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6037 // separate instructions that make it harder to use a non-constant update.
6038 continue;
6039 }
6040
6041 // Create the new updating load/store node.
6042 EVT Tys[6];
6043 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6044 unsigned n;
6045 for (n = 0; n < NumResultVecs; ++n)
6046 Tys[n] = VecTy;
6047 Tys[n++] = MVT::i32;
6048 Tys[n] = MVT::Other;
6049 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6050 SmallVector<SDValue, 8> Ops;
6051 Ops.push_back(N->getOperand(0)); // incoming chain
6052 Ops.push_back(N->getOperand(AddrOpIdx));
6053 Ops.push_back(Inc);
6054 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6055 Ops.push_back(N->getOperand(i));
6056 }
6057 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6058 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6059 Ops.data(), Ops.size(),
6060 MemInt->getMemoryVT(),
6061 MemInt->getMemOperand());
6062
6063 // Update the uses.
6064 std::vector<SDValue> NewResults;
6065 for (unsigned i = 0; i < NumResultVecs; ++i) {
6066 NewResults.push_back(SDValue(UpdN.getNode(), i));
6067 }
6068 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6069 DCI.CombineTo(N, NewResults);
6070 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6071
6072 break;
Owen Anderson76706012011-04-05 21:48:57 +00006073 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006074 return SDValue();
6075}
6076
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006077/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6078/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6079/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6080/// return true.
6081static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6082 SelectionDAG &DAG = DCI.DAG;
6083 EVT VT = N->getValueType(0);
6084 // vldN-dup instructions only support 64-bit vectors for N > 1.
6085 if (!VT.is64BitVector())
6086 return false;
6087
6088 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6089 SDNode *VLD = N->getOperand(0).getNode();
6090 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6091 return false;
6092 unsigned NumVecs = 0;
6093 unsigned NewOpc = 0;
6094 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6095 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6096 NumVecs = 2;
6097 NewOpc = ARMISD::VLD2DUP;
6098 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6099 NumVecs = 3;
6100 NewOpc = ARMISD::VLD3DUP;
6101 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6102 NumVecs = 4;
6103 NewOpc = ARMISD::VLD4DUP;
6104 } else {
6105 return false;
6106 }
6107
6108 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6109 // numbers match the load.
6110 unsigned VLDLaneNo =
6111 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6112 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6113 UI != UE; ++UI) {
6114 // Ignore uses of the chain result.
6115 if (UI.getUse().getResNo() == NumVecs)
6116 continue;
6117 SDNode *User = *UI;
6118 if (User->getOpcode() != ARMISD::VDUPLANE ||
6119 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6120 return false;
6121 }
6122
6123 // Create the vldN-dup node.
6124 EVT Tys[5];
6125 unsigned n;
6126 for (n = 0; n < NumVecs; ++n)
6127 Tys[n] = VT;
6128 Tys[n] = MVT::Other;
6129 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6130 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6131 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6132 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6133 Ops, 2, VLDMemInt->getMemoryVT(),
6134 VLDMemInt->getMemOperand());
6135
6136 // Update the uses.
6137 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6138 UI != UE; ++UI) {
6139 unsigned ResNo = UI.getUse().getResNo();
6140 // Ignore uses of the chain result.
6141 if (ResNo == NumVecs)
6142 continue;
6143 SDNode *User = *UI;
6144 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6145 }
6146
6147 // Now the vldN-lane intrinsic is dead except for its chain result.
6148 // Update uses of the chain.
6149 std::vector<SDValue> VLDDupResults;
6150 for (unsigned n = 0; n < NumVecs; ++n)
6151 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6152 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6153 DCI.CombineTo(VLD, VLDDupResults);
6154
6155 return true;
6156}
6157
Bob Wilson9e82bf12010-07-14 01:22:12 +00006158/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6159/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006160static SDValue PerformVDUPLANECombine(SDNode *N,
6161 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006162 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006163
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006164 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6165 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6166 if (CombineVLDDUP(N, DCI))
6167 return SDValue(N, 0);
6168
6169 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6170 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006171 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006172 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006173 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006174 return SDValue();
6175
6176 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6177 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6178 // The canonical VMOV for a zero vector uses a 32-bit element size.
6179 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6180 unsigned EltBits;
6181 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6182 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006183 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006184 if (EltSize > VT.getVectorElementType().getSizeInBits())
6185 return SDValue();
6186
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006187 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006188}
6189
Bob Wilson5bafff32009-06-22 23:27:02 +00006190/// getVShiftImm - Check if this is a valid build_vector for the immediate
6191/// operand of a vector shift operation, where all the elements of the
6192/// build_vector must have the same constant integer value.
6193static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6194 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006195 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006196 Op = Op.getOperand(0);
6197 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6198 APInt SplatBits, SplatUndef;
6199 unsigned SplatBitSize;
6200 bool HasAnyUndefs;
6201 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6202 HasAnyUndefs, ElementBits) ||
6203 SplatBitSize > ElementBits)
6204 return false;
6205 Cnt = SplatBits.getSExtValue();
6206 return true;
6207}
6208
6209/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6210/// operand of a vector shift left operation. That value must be in the range:
6211/// 0 <= Value < ElementBits for a left shift; or
6212/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006213static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006214 assert(VT.isVector() && "vector shift count is not a vector type");
6215 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6216 if (! getVShiftImm(Op, ElementBits, Cnt))
6217 return false;
6218 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6219}
6220
6221/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6222/// operand of a vector shift right operation. For a shift opcode, the value
6223/// is positive, but for an intrinsic the value count must be negative. The
6224/// absolute value must be in the range:
6225/// 1 <= |Value| <= ElementBits for a right shift; or
6226/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006227static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006228 int64_t &Cnt) {
6229 assert(VT.isVector() && "vector shift count is not a vector type");
6230 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6231 if (! getVShiftImm(Op, ElementBits, Cnt))
6232 return false;
6233 if (isIntrinsic)
6234 Cnt = -Cnt;
6235 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6236}
6237
6238/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6239static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6240 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6241 switch (IntNo) {
6242 default:
6243 // Don't do anything for most intrinsics.
6244 break;
6245
6246 // Vector shifts: check for immediate versions and lower them.
6247 // Note: This is done during DAG combining instead of DAG legalizing because
6248 // the build_vectors for 64-bit vector element shift counts are generally
6249 // not legal, and it is hard to see their values after they get legalized to
6250 // loads from a constant pool.
6251 case Intrinsic::arm_neon_vshifts:
6252 case Intrinsic::arm_neon_vshiftu:
6253 case Intrinsic::arm_neon_vshiftls:
6254 case Intrinsic::arm_neon_vshiftlu:
6255 case Intrinsic::arm_neon_vshiftn:
6256 case Intrinsic::arm_neon_vrshifts:
6257 case Intrinsic::arm_neon_vrshiftu:
6258 case Intrinsic::arm_neon_vrshiftn:
6259 case Intrinsic::arm_neon_vqshifts:
6260 case Intrinsic::arm_neon_vqshiftu:
6261 case Intrinsic::arm_neon_vqshiftsu:
6262 case Intrinsic::arm_neon_vqshiftns:
6263 case Intrinsic::arm_neon_vqshiftnu:
6264 case Intrinsic::arm_neon_vqshiftnsu:
6265 case Intrinsic::arm_neon_vqrshiftns:
6266 case Intrinsic::arm_neon_vqrshiftnu:
6267 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006268 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006269 int64_t Cnt;
6270 unsigned VShiftOpc = 0;
6271
6272 switch (IntNo) {
6273 case Intrinsic::arm_neon_vshifts:
6274 case Intrinsic::arm_neon_vshiftu:
6275 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6276 VShiftOpc = ARMISD::VSHL;
6277 break;
6278 }
6279 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6280 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6281 ARMISD::VSHRs : ARMISD::VSHRu);
6282 break;
6283 }
6284 return SDValue();
6285
6286 case Intrinsic::arm_neon_vshiftls:
6287 case Intrinsic::arm_neon_vshiftlu:
6288 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6289 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006290 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006291
6292 case Intrinsic::arm_neon_vrshifts:
6293 case Intrinsic::arm_neon_vrshiftu:
6294 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6295 break;
6296 return SDValue();
6297
6298 case Intrinsic::arm_neon_vqshifts:
6299 case Intrinsic::arm_neon_vqshiftu:
6300 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6301 break;
6302 return SDValue();
6303
6304 case Intrinsic::arm_neon_vqshiftsu:
6305 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6306 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006307 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006308
6309 case Intrinsic::arm_neon_vshiftn:
6310 case Intrinsic::arm_neon_vrshiftn:
6311 case Intrinsic::arm_neon_vqshiftns:
6312 case Intrinsic::arm_neon_vqshiftnu:
6313 case Intrinsic::arm_neon_vqshiftnsu:
6314 case Intrinsic::arm_neon_vqrshiftns:
6315 case Intrinsic::arm_neon_vqrshiftnu:
6316 case Intrinsic::arm_neon_vqrshiftnsu:
6317 // Narrowing shifts require an immediate right shift.
6318 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6319 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006320 llvm_unreachable("invalid shift count for narrowing vector shift "
6321 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006322
6323 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006324 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006325 }
6326
6327 switch (IntNo) {
6328 case Intrinsic::arm_neon_vshifts:
6329 case Intrinsic::arm_neon_vshiftu:
6330 // Opcode already set above.
6331 break;
6332 case Intrinsic::arm_neon_vshiftls:
6333 case Intrinsic::arm_neon_vshiftlu:
6334 if (Cnt == VT.getVectorElementType().getSizeInBits())
6335 VShiftOpc = ARMISD::VSHLLi;
6336 else
6337 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6338 ARMISD::VSHLLs : ARMISD::VSHLLu);
6339 break;
6340 case Intrinsic::arm_neon_vshiftn:
6341 VShiftOpc = ARMISD::VSHRN; break;
6342 case Intrinsic::arm_neon_vrshifts:
6343 VShiftOpc = ARMISD::VRSHRs; break;
6344 case Intrinsic::arm_neon_vrshiftu:
6345 VShiftOpc = ARMISD::VRSHRu; break;
6346 case Intrinsic::arm_neon_vrshiftn:
6347 VShiftOpc = ARMISD::VRSHRN; break;
6348 case Intrinsic::arm_neon_vqshifts:
6349 VShiftOpc = ARMISD::VQSHLs; break;
6350 case Intrinsic::arm_neon_vqshiftu:
6351 VShiftOpc = ARMISD::VQSHLu; break;
6352 case Intrinsic::arm_neon_vqshiftsu:
6353 VShiftOpc = ARMISD::VQSHLsu; break;
6354 case Intrinsic::arm_neon_vqshiftns:
6355 VShiftOpc = ARMISD::VQSHRNs; break;
6356 case Intrinsic::arm_neon_vqshiftnu:
6357 VShiftOpc = ARMISD::VQSHRNu; break;
6358 case Intrinsic::arm_neon_vqshiftnsu:
6359 VShiftOpc = ARMISD::VQSHRNsu; break;
6360 case Intrinsic::arm_neon_vqrshiftns:
6361 VShiftOpc = ARMISD::VQRSHRNs; break;
6362 case Intrinsic::arm_neon_vqrshiftnu:
6363 VShiftOpc = ARMISD::VQRSHRNu; break;
6364 case Intrinsic::arm_neon_vqrshiftnsu:
6365 VShiftOpc = ARMISD::VQRSHRNsu; break;
6366 }
6367
6368 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006369 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006370 }
6371
6372 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006373 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006374 int64_t Cnt;
6375 unsigned VShiftOpc = 0;
6376
6377 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6378 VShiftOpc = ARMISD::VSLI;
6379 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6380 VShiftOpc = ARMISD::VSRI;
6381 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006382 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006383 }
6384
6385 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6386 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006387 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006388 }
6389
6390 case Intrinsic::arm_neon_vqrshifts:
6391 case Intrinsic::arm_neon_vqrshiftu:
6392 // No immediate versions of these to check for.
6393 break;
6394 }
6395
6396 return SDValue();
6397}
6398
6399/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6400/// lowers them. As with the vector shift intrinsics, this is done during DAG
6401/// combining instead of DAG legalizing because the build_vectors for 64-bit
6402/// vector element shift counts are generally not legal, and it is hard to see
6403/// their values after they get legalized to loads from a constant pool.
6404static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6405 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006406 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006407
6408 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006409 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6410 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006411 return SDValue();
6412
6413 assert(ST->hasNEON() && "unexpected vector shift");
6414 int64_t Cnt;
6415
6416 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006417 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006418
6419 case ISD::SHL:
6420 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6421 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006422 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006423 break;
6424
6425 case ISD::SRA:
6426 case ISD::SRL:
6427 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6428 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6429 ARMISD::VSHRs : ARMISD::VSHRu);
6430 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006431 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006432 }
6433 }
6434 return SDValue();
6435}
6436
6437/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6438/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6439static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6440 const ARMSubtarget *ST) {
6441 SDValue N0 = N->getOperand(0);
6442
6443 // Check for sign- and zero-extensions of vector extract operations of 8-
6444 // and 16-bit vector elements. NEON supports these directly. They are
6445 // handled during DAG combining because type legalization will promote them
6446 // to 32-bit types and it is messy to recognize the operations after that.
6447 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6448 SDValue Vec = N0.getOperand(0);
6449 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006450 EVT VT = N->getValueType(0);
6451 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006452 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6453
Owen Anderson825b72b2009-08-11 20:47:22 +00006454 if (VT == MVT::i32 &&
6455 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006456 TLI.isTypeLegal(Vec.getValueType()) &&
6457 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006458
6459 unsigned Opc = 0;
6460 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006461 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006462 case ISD::SIGN_EXTEND:
6463 Opc = ARMISD::VGETLANEs;
6464 break;
6465 case ISD::ZERO_EXTEND:
6466 case ISD::ANY_EXTEND:
6467 Opc = ARMISD::VGETLANEu;
6468 break;
6469 }
6470 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6471 }
6472 }
6473
6474 return SDValue();
6475}
6476
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006477/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6478/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6479static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6480 const ARMSubtarget *ST) {
6481 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006482 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006483 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6484 // a NaN; only do the transformation when it matches that behavior.
6485
6486 // For now only do this when using NEON for FP operations; if using VFP, it
6487 // is not obvious that the benefit outweighs the cost of switching to the
6488 // NEON pipeline.
6489 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6490 N->getValueType(0) != MVT::f32)
6491 return SDValue();
6492
6493 SDValue CondLHS = N->getOperand(0);
6494 SDValue CondRHS = N->getOperand(1);
6495 SDValue LHS = N->getOperand(2);
6496 SDValue RHS = N->getOperand(3);
6497 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6498
6499 unsigned Opcode = 0;
6500 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006501 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006502 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006503 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006504 IsReversed = true ; // x CC y ? y : x
6505 } else {
6506 return SDValue();
6507 }
6508
Bob Wilsone742bb52010-02-24 22:15:53 +00006509 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006510 switch (CC) {
6511 default: break;
6512 case ISD::SETOLT:
6513 case ISD::SETOLE:
6514 case ISD::SETLT:
6515 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006516 case ISD::SETULT:
6517 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006518 // If LHS is NaN, an ordered comparison will be false and the result will
6519 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6520 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6521 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6522 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6523 break;
6524 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6525 // will return -0, so vmin can only be used for unsafe math or if one of
6526 // the operands is known to be nonzero.
6527 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6528 !UnsafeFPMath &&
6529 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6530 break;
6531 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006532 break;
6533
6534 case ISD::SETOGT:
6535 case ISD::SETOGE:
6536 case ISD::SETGT:
6537 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006538 case ISD::SETUGT:
6539 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006540 // If LHS is NaN, an ordered comparison will be false and the result will
6541 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6542 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6543 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6544 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6545 break;
6546 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6547 // will return +0, so vmax can only be used for unsafe math or if one of
6548 // the operands is known to be nonzero.
6549 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6550 !UnsafeFPMath &&
6551 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6552 break;
6553 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006554 break;
6555 }
6556
6557 if (!Opcode)
6558 return SDValue();
6559 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6560}
6561
Dan Gohman475871a2008-07-27 21:46:04 +00006562SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006563 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006564 switch (N->getOpcode()) {
6565 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006566 case ISD::ADD: return PerformADDCombine(N, DCI);
6567 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006568 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006569 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006570 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006571 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006572 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006573 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006574 case ISD::STORE: return PerformSTORECombine(N, DCI);
6575 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6576 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006577 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006578 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006579 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006580 case ISD::SHL:
6581 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006582 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006583 case ISD::SIGN_EXTEND:
6584 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006585 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6586 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006587 case ARMISD::VLD2DUP:
6588 case ARMISD::VLD3DUP:
6589 case ARMISD::VLD4DUP:
6590 return CombineBaseUpdate(N, DCI);
6591 case ISD::INTRINSIC_VOID:
6592 case ISD::INTRINSIC_W_CHAIN:
6593 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6594 case Intrinsic::arm_neon_vld1:
6595 case Intrinsic::arm_neon_vld2:
6596 case Intrinsic::arm_neon_vld3:
6597 case Intrinsic::arm_neon_vld4:
6598 case Intrinsic::arm_neon_vld2lane:
6599 case Intrinsic::arm_neon_vld3lane:
6600 case Intrinsic::arm_neon_vld4lane:
6601 case Intrinsic::arm_neon_vst1:
6602 case Intrinsic::arm_neon_vst2:
6603 case Intrinsic::arm_neon_vst3:
6604 case Intrinsic::arm_neon_vst4:
6605 case Intrinsic::arm_neon_vst2lane:
6606 case Intrinsic::arm_neon_vst3lane:
6607 case Intrinsic::arm_neon_vst4lane:
6608 return CombineBaseUpdate(N, DCI);
6609 default: break;
6610 }
6611 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006612 }
Dan Gohman475871a2008-07-27 21:46:04 +00006613 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006614}
6615
Evan Cheng31959b12011-02-02 01:06:55 +00006616bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6617 EVT VT) const {
6618 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6619}
6620
Bill Wendlingaf566342009-08-15 21:21:19 +00006621bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006622 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006623 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006624
6625 switch (VT.getSimpleVT().SimpleTy) {
6626 default:
6627 return false;
6628 case MVT::i8:
6629 case MVT::i16:
6630 case MVT::i32:
6631 return true;
6632 // FIXME: VLD1 etc with standard alignment is legal.
6633 }
6634}
6635
Evan Chenge6c835f2009-08-14 20:09:37 +00006636static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6637 if (V < 0)
6638 return false;
6639
6640 unsigned Scale = 1;
6641 switch (VT.getSimpleVT().SimpleTy) {
6642 default: return false;
6643 case MVT::i1:
6644 case MVT::i8:
6645 // Scale == 1;
6646 break;
6647 case MVT::i16:
6648 // Scale == 2;
6649 Scale = 2;
6650 break;
6651 case MVT::i32:
6652 // Scale == 4;
6653 Scale = 4;
6654 break;
6655 }
6656
6657 if ((V & (Scale - 1)) != 0)
6658 return false;
6659 V /= Scale;
6660 return V == (V & ((1LL << 5) - 1));
6661}
6662
6663static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6664 const ARMSubtarget *Subtarget) {
6665 bool isNeg = false;
6666 if (V < 0) {
6667 isNeg = true;
6668 V = - V;
6669 }
6670
6671 switch (VT.getSimpleVT().SimpleTy) {
6672 default: return false;
6673 case MVT::i1:
6674 case MVT::i8:
6675 case MVT::i16:
6676 case MVT::i32:
6677 // + imm12 or - imm8
6678 if (isNeg)
6679 return V == (V & ((1LL << 8) - 1));
6680 return V == (V & ((1LL << 12) - 1));
6681 case MVT::f32:
6682 case MVT::f64:
6683 // Same as ARM mode. FIXME: NEON?
6684 if (!Subtarget->hasVFP2())
6685 return false;
6686 if ((V & 3) != 0)
6687 return false;
6688 V >>= 2;
6689 return V == (V & ((1LL << 8) - 1));
6690 }
6691}
6692
Evan Chengb01fad62007-03-12 23:30:29 +00006693/// isLegalAddressImmediate - Return true if the integer value can be used
6694/// as the offset of the target addressing mode for load / store of the
6695/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006696static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006697 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006698 if (V == 0)
6699 return true;
6700
Evan Cheng65011532009-03-09 19:15:00 +00006701 if (!VT.isSimple())
6702 return false;
6703
Evan Chenge6c835f2009-08-14 20:09:37 +00006704 if (Subtarget->isThumb1Only())
6705 return isLegalT1AddressImmediate(V, VT);
6706 else if (Subtarget->isThumb2())
6707 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006708
Evan Chenge6c835f2009-08-14 20:09:37 +00006709 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006710 if (V < 0)
6711 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006712 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006713 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006714 case MVT::i1:
6715 case MVT::i8:
6716 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006717 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006718 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006719 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006720 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006721 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006722 case MVT::f32:
6723 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006724 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006725 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006726 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006727 return false;
6728 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006729 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006730 }
Evan Chenga8e29892007-01-19 07:51:42 +00006731}
6732
Evan Chenge6c835f2009-08-14 20:09:37 +00006733bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6734 EVT VT) const {
6735 int Scale = AM.Scale;
6736 if (Scale < 0)
6737 return false;
6738
6739 switch (VT.getSimpleVT().SimpleTy) {
6740 default: return false;
6741 case MVT::i1:
6742 case MVT::i8:
6743 case MVT::i16:
6744 case MVT::i32:
6745 if (Scale == 1)
6746 return true;
6747 // r + r << imm
6748 Scale = Scale & ~1;
6749 return Scale == 2 || Scale == 4 || Scale == 8;
6750 case MVT::i64:
6751 // r + r
6752 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6753 return true;
6754 return false;
6755 case MVT::isVoid:
6756 // Note, we allow "void" uses (basically, uses that aren't loads or
6757 // stores), because arm allows folding a scale into many arithmetic
6758 // operations. This should be made more precise and revisited later.
6759
6760 // Allow r << imm, but the imm has to be a multiple of two.
6761 if (Scale & 1) return false;
6762 return isPowerOf2_32(Scale);
6763 }
6764}
6765
Chris Lattner37caf8c2007-04-09 23:33:39 +00006766/// isLegalAddressingMode - Return true if the addressing mode represented
6767/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006768bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006769 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006770 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006771 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006772 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006773
Chris Lattner37caf8c2007-04-09 23:33:39 +00006774 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006775 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006776 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006777
Chris Lattner37caf8c2007-04-09 23:33:39 +00006778 switch (AM.Scale) {
6779 case 0: // no scale reg, must be "r+i" or "r", or "i".
6780 break;
6781 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006782 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006783 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006784 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006785 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006786 // ARM doesn't support any R+R*scale+imm addr modes.
6787 if (AM.BaseOffs)
6788 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006789
Bob Wilson2c7dab12009-04-08 17:55:28 +00006790 if (!VT.isSimple())
6791 return false;
6792
Evan Chenge6c835f2009-08-14 20:09:37 +00006793 if (Subtarget->isThumb2())
6794 return isLegalT2ScaledAddressingMode(AM, VT);
6795
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006796 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006797 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006798 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006799 case MVT::i1:
6800 case MVT::i8:
6801 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006802 if (Scale < 0) Scale = -Scale;
6803 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006804 return true;
6805 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006806 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006807 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006808 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006809 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006810 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006811 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006812 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006813
Owen Anderson825b72b2009-08-11 20:47:22 +00006814 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006815 // Note, we allow "void" uses (basically, uses that aren't loads or
6816 // stores), because arm allows folding a scale into many arithmetic
6817 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006818
Chris Lattner37caf8c2007-04-09 23:33:39 +00006819 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006820 if (Scale & 1) return false;
6821 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006822 }
6823 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006824 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006825 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006826}
6827
Evan Cheng77e47512009-11-11 19:05:52 +00006828/// isLegalICmpImmediate - Return true if the specified immediate is legal
6829/// icmp immediate, that is the target has icmp instructions which can compare
6830/// a register against the immediate without having to materialize the
6831/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006832bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006833 if (!Subtarget->isThumb())
6834 return ARM_AM::getSOImmVal(Imm) != -1;
6835 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006836 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006837 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006838}
6839
Owen Andersone50ed302009-08-10 22:56:29 +00006840static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006841 bool isSEXTLoad, SDValue &Base,
6842 SDValue &Offset, bool &isInc,
6843 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006844 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6845 return false;
6846
Owen Anderson825b72b2009-08-11 20:47:22 +00006847 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006848 // AddressingMode 3
6849 Base = Ptr->getOperand(0);
6850 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006851 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006852 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006853 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006854 isInc = false;
6855 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6856 return true;
6857 }
6858 }
6859 isInc = (Ptr->getOpcode() == ISD::ADD);
6860 Offset = Ptr->getOperand(1);
6861 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006863 // AddressingMode 2
6864 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006865 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006866 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006867 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006868 isInc = false;
6869 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6870 Base = Ptr->getOperand(0);
6871 return true;
6872 }
6873 }
6874
6875 if (Ptr->getOpcode() == ISD::ADD) {
6876 isInc = true;
6877 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6878 if (ShOpcVal != ARM_AM::no_shift) {
6879 Base = Ptr->getOperand(1);
6880 Offset = Ptr->getOperand(0);
6881 } else {
6882 Base = Ptr->getOperand(0);
6883 Offset = Ptr->getOperand(1);
6884 }
6885 return true;
6886 }
6887
6888 isInc = (Ptr->getOpcode() == ISD::ADD);
6889 Base = Ptr->getOperand(0);
6890 Offset = Ptr->getOperand(1);
6891 return true;
6892 }
6893
Jim Grosbache5165492009-11-09 00:11:35 +00006894 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006895 return false;
6896}
6897
Owen Andersone50ed302009-08-10 22:56:29 +00006898static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006899 bool isSEXTLoad, SDValue &Base,
6900 SDValue &Offset, bool &isInc,
6901 SelectionDAG &DAG) {
6902 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6903 return false;
6904
6905 Base = Ptr->getOperand(0);
6906 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6907 int RHSC = (int)RHS->getZExtValue();
6908 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6909 assert(Ptr->getOpcode() == ISD::ADD);
6910 isInc = false;
6911 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6912 return true;
6913 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6914 isInc = Ptr->getOpcode() == ISD::ADD;
6915 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6916 return true;
6917 }
6918 }
6919
6920 return false;
6921}
6922
Evan Chenga8e29892007-01-19 07:51:42 +00006923/// getPreIndexedAddressParts - returns true by value, base pointer and
6924/// offset pointer and addressing mode by reference if the node's address
6925/// can be legally represented as pre-indexed load / store address.
6926bool
Dan Gohman475871a2008-07-27 21:46:04 +00006927ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6928 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006929 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006930 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006931 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006932 return false;
6933
Owen Andersone50ed302009-08-10 22:56:29 +00006934 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006935 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006936 bool isSEXTLoad = false;
6937 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6938 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006939 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006940 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6941 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6942 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006943 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006944 } else
6945 return false;
6946
6947 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006948 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006949 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006950 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6951 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006952 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006953 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006954 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006955 if (!isLegal)
6956 return false;
6957
6958 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6959 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006960}
6961
6962/// getPostIndexedAddressParts - returns true by value, base pointer and
6963/// offset pointer and addressing mode by reference if this node can be
6964/// combined with a load / store to form a post-indexed load / store.
6965bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006966 SDValue &Base,
6967 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006968 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006969 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006970 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006971 return false;
6972
Owen Andersone50ed302009-08-10 22:56:29 +00006973 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006974 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006975 bool isSEXTLoad = false;
6976 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006977 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006978 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006979 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6980 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006981 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006982 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006983 } else
6984 return false;
6985
6986 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006987 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006988 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006989 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006990 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006991 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006992 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6993 isInc, DAG);
6994 if (!isLegal)
6995 return false;
6996
Evan Cheng28dad2a2010-05-18 21:31:17 +00006997 if (Ptr != Base) {
6998 // Swap base ptr and offset to catch more post-index load / store when
6999 // it's legal. In Thumb2 mode, offset must be an immediate.
7000 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7001 !Subtarget->isThumb2())
7002 std::swap(Base, Offset);
7003
7004 // Post-indexed load / store update the base pointer.
7005 if (Ptr != Base)
7006 return false;
7007 }
7008
Evan Chenge88d5ce2009-07-02 07:28:31 +00007009 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7010 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007011}
7012
Dan Gohman475871a2008-07-27 21:46:04 +00007013void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007014 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007015 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007016 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007017 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007018 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007019 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007020 switch (Op.getOpcode()) {
7021 default: break;
7022 case ARMISD::CMOV: {
7023 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007024 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007025 if (KnownZero == 0 && KnownOne == 0) return;
7026
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007027 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007028 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7029 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007030 KnownZero &= KnownZeroRHS;
7031 KnownOne &= KnownOneRHS;
7032 return;
7033 }
7034 }
7035}
7036
7037//===----------------------------------------------------------------------===//
7038// ARM Inline Assembly Support
7039//===----------------------------------------------------------------------===//
7040
Evan Cheng55d42002011-01-08 01:24:27 +00007041bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7042 // Looking for "rev" which is V6+.
7043 if (!Subtarget->hasV6Ops())
7044 return false;
7045
7046 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7047 std::string AsmStr = IA->getAsmString();
7048 SmallVector<StringRef, 4> AsmPieces;
7049 SplitString(AsmStr, AsmPieces, ";\n");
7050
7051 switch (AsmPieces.size()) {
7052 default: return false;
7053 case 1:
7054 AsmStr = AsmPieces[0];
7055 AsmPieces.clear();
7056 SplitString(AsmStr, AsmPieces, " \t,");
7057
7058 // rev $0, $1
7059 if (AsmPieces.size() == 3 &&
7060 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7061 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7062 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7063 if (Ty && Ty->getBitWidth() == 32)
7064 return IntrinsicLowering::LowerToByteSwap(CI);
7065 }
7066 break;
7067 }
7068
7069 return false;
7070}
7071
Evan Chenga8e29892007-01-19 07:51:42 +00007072/// getConstraintType - Given a constraint letter, return the type of
7073/// constraint it is for this target.
7074ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007075ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7076 if (Constraint.size() == 1) {
7077 switch (Constraint[0]) {
7078 default: break;
7079 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007080 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00007081 }
Evan Chenga8e29892007-01-19 07:51:42 +00007082 }
Chris Lattner4234f572007-03-25 02:14:49 +00007083 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007084}
7085
John Thompson44ab89e2010-10-29 17:29:13 +00007086/// Examine constraint type and operand type and determine a weight value.
7087/// This object must already have been set up with the operand type
7088/// and the current alternative constraint selected.
7089TargetLowering::ConstraintWeight
7090ARMTargetLowering::getSingleConstraintMatchWeight(
7091 AsmOperandInfo &info, const char *constraint) const {
7092 ConstraintWeight weight = CW_Invalid;
7093 Value *CallOperandVal = info.CallOperandVal;
7094 // If we don't have a value, we can't do a match,
7095 // but allow it at the lowest weight.
7096 if (CallOperandVal == NULL)
7097 return CW_Default;
7098 const Type *type = CallOperandVal->getType();
7099 // Look at the constraint type.
7100 switch (*constraint) {
7101 default:
7102 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7103 break;
7104 case 'l':
7105 if (type->isIntegerTy()) {
7106 if (Subtarget->isThumb())
7107 weight = CW_SpecificReg;
7108 else
7109 weight = CW_Register;
7110 }
7111 break;
7112 case 'w':
7113 if (type->isFloatingPointTy())
7114 weight = CW_Register;
7115 break;
7116 }
7117 return weight;
7118}
7119
Bob Wilson2dc4f542009-03-20 22:42:55 +00007120std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007121ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007122 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007123 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007124 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007125 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007126 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007127 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007128 return std::make_pair(0U, ARM::tGPRRegisterClass);
7129 else
7130 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007131 case 'r':
7132 return std::make_pair(0U, ARM::GPRRegisterClass);
7133 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007135 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007136 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007137 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007138 if (VT.getSizeInBits() == 128)
7139 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007140 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007141 }
7142 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007143 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007144 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007145
Evan Chenga8e29892007-01-19 07:51:42 +00007146 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7147}
7148
7149std::vector<unsigned> ARMTargetLowering::
7150getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007151 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007152 if (Constraint.size() != 1)
7153 return std::vector<unsigned>();
7154
7155 switch (Constraint[0]) { // GCC ARM Constraint Letters
7156 default: break;
7157 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007158 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7159 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7160 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007161 case 'r':
7162 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7163 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7164 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7165 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007166 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007167 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007168 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7169 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7170 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7171 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7172 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7173 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7174 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7175 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007176 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007177 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7178 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7179 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7180 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007181 if (VT.getSizeInBits() == 128)
7182 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7183 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007184 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007185 }
7186
7187 return std::vector<unsigned>();
7188}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007189
7190/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7191/// vector. If it is invalid, don't add anything to Ops.
7192void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
7193 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007194 std::vector<SDValue>&Ops,
7195 SelectionDAG &DAG) const {
7196 SDValue Result(0, 0);
7197
7198 switch (Constraint) {
7199 default: break;
7200 case 'I': case 'J': case 'K': case 'L':
7201 case 'M': case 'N': case 'O':
7202 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7203 if (!C)
7204 return;
7205
7206 int64_t CVal64 = C->getSExtValue();
7207 int CVal = (int) CVal64;
7208 // None of these constraints allow values larger than 32 bits. Check
7209 // that the value fits in an int.
7210 if (CVal != CVal64)
7211 return;
7212
7213 switch (Constraint) {
7214 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007215 if (Subtarget->isThumb1Only()) {
7216 // This must be a constant between 0 and 255, for ADD
7217 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007218 if (CVal >= 0 && CVal <= 255)
7219 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007220 } else if (Subtarget->isThumb2()) {
7221 // A constant that can be used as an immediate value in a
7222 // data-processing instruction.
7223 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7224 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007225 } else {
7226 // A constant that can be used as an immediate value in a
7227 // data-processing instruction.
7228 if (ARM_AM::getSOImmVal(CVal) != -1)
7229 break;
7230 }
7231 return;
7232
7233 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007234 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007235 // This must be a constant between -255 and -1, for negated ADD
7236 // immediates. This can be used in GCC with an "n" modifier that
7237 // prints the negated value, for use with SUB instructions. It is
7238 // not useful otherwise but is implemented for compatibility.
7239 if (CVal >= -255 && CVal <= -1)
7240 break;
7241 } else {
7242 // This must be a constant between -4095 and 4095. It is not clear
7243 // what this constraint is intended for. Implemented for
7244 // compatibility with GCC.
7245 if (CVal >= -4095 && CVal <= 4095)
7246 break;
7247 }
7248 return;
7249
7250 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007251 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007252 // A 32-bit value where only one byte has a nonzero value. Exclude
7253 // zero to match GCC. This constraint is used by GCC internally for
7254 // constants that can be loaded with a move/shift combination.
7255 // It is not useful otherwise but is implemented for compatibility.
7256 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7257 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007258 } else if (Subtarget->isThumb2()) {
7259 // A constant whose bitwise inverse can be used as an immediate
7260 // value in a data-processing instruction. This can be used in GCC
7261 // with a "B" modifier that prints the inverted value, for use with
7262 // BIC and MVN instructions. It is not useful otherwise but is
7263 // implemented for compatibility.
7264 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7265 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007266 } else {
7267 // A constant whose bitwise inverse can be used as an immediate
7268 // value in a data-processing instruction. This can be used in GCC
7269 // with a "B" modifier that prints the inverted value, for use with
7270 // BIC and MVN instructions. It is not useful otherwise but is
7271 // implemented for compatibility.
7272 if (ARM_AM::getSOImmVal(~CVal) != -1)
7273 break;
7274 }
7275 return;
7276
7277 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007278 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007279 // This must be a constant between -7 and 7,
7280 // for 3-operand ADD/SUB immediate instructions.
7281 if (CVal >= -7 && CVal < 7)
7282 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007283 } else if (Subtarget->isThumb2()) {
7284 // A constant whose negation can be used as an immediate value in a
7285 // data-processing instruction. This can be used in GCC with an "n"
7286 // modifier that prints the negated value, for use with SUB
7287 // instructions. It is not useful otherwise but is implemented for
7288 // compatibility.
7289 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7290 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007291 } else {
7292 // A constant whose negation can be used as an immediate value in a
7293 // data-processing instruction. This can be used in GCC with an "n"
7294 // modifier that prints the negated value, for use with SUB
7295 // instructions. It is not useful otherwise but is implemented for
7296 // compatibility.
7297 if (ARM_AM::getSOImmVal(-CVal) != -1)
7298 break;
7299 }
7300 return;
7301
7302 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007303 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007304 // This must be a multiple of 4 between 0 and 1020, for
7305 // ADD sp + immediate.
7306 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7307 break;
7308 } else {
7309 // A power of two or a constant between 0 and 32. This is used in
7310 // GCC for the shift amount on shifted register operands, but it is
7311 // useful in general for any shift amounts.
7312 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7313 break;
7314 }
7315 return;
7316
7317 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007318 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007319 // This must be a constant between 0 and 31, for shift amounts.
7320 if (CVal >= 0 && CVal <= 31)
7321 break;
7322 }
7323 return;
7324
7325 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007326 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007327 // This must be a multiple of 4 between -508 and 508, for
7328 // ADD/SUB sp = sp + immediate.
7329 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7330 break;
7331 }
7332 return;
7333 }
7334 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7335 break;
7336 }
7337
7338 if (Result.getNode()) {
7339 Ops.push_back(Result);
7340 return;
7341 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007342 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007343}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007344
7345bool
7346ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7347 // The ARM target isn't yet aware of offsets.
7348 return false;
7349}
Evan Cheng39382422009-10-28 01:44:26 +00007350
7351int ARM::getVFPf32Imm(const APFloat &FPImm) {
7352 APInt Imm = FPImm.bitcastToAPInt();
7353 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7354 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7355 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7356
7357 // We can handle 4 bits of mantissa.
7358 // mantissa = (16+UInt(e:f:g:h))/16.
7359 if (Mantissa & 0x7ffff)
7360 return -1;
7361 Mantissa >>= 19;
7362 if ((Mantissa & 0xf) != Mantissa)
7363 return -1;
7364
7365 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7366 if (Exp < -3 || Exp > 4)
7367 return -1;
7368 Exp = ((Exp+3) & 0x7) ^ 4;
7369
7370 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7371}
7372
7373int ARM::getVFPf64Imm(const APFloat &FPImm) {
7374 APInt Imm = FPImm.bitcastToAPInt();
7375 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7376 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7377 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7378
7379 // We can handle 4 bits of mantissa.
7380 // mantissa = (16+UInt(e:f:g:h))/16.
7381 if (Mantissa & 0xffffffffffffLL)
7382 return -1;
7383 Mantissa >>= 48;
7384 if ((Mantissa & 0xf) != Mantissa)
7385 return -1;
7386
7387 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7388 if (Exp < -3 || Exp > 4)
7389 return -1;
7390 Exp = ((Exp+3) & 0x7) ^ 4;
7391
7392 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7393}
7394
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007395bool ARM::isBitFieldInvertedMask(unsigned v) {
7396 if (v == 0xffffffff)
7397 return 0;
7398 // there can be 1's on either or both "outsides", all the "inside"
7399 // bits must be 0's
7400 unsigned int lsb = 0, msb = 31;
7401 while (v & (1 << msb)) --msb;
7402 while (v & (1 << lsb)) ++lsb;
7403 for (unsigned int i = lsb; i <= msb; ++i) {
7404 if (v & (1 << i))
7405 return 0;
7406 }
7407 return 1;
7408}
7409
Evan Cheng39382422009-10-28 01:44:26 +00007410/// isFPImmLegal - Returns true if the target can instruction select the
7411/// specified FP immediate natively. If false, the legalizer will
7412/// materialize the FP immediate as a load from a constant pool.
7413bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7414 if (!Subtarget->hasVFP3())
7415 return false;
7416 if (VT == MVT::f32)
7417 return ARM::getVFPf32Imm(Imm) != -1;
7418 if (VT == MVT::f64)
7419 return ARM::getVFPf64Imm(Imm) != -1;
7420 return false;
7421}
Bob Wilson65ffec42010-09-21 17:56:22 +00007422
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007423/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007424/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7425/// specified in the intrinsic calls.
7426bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7427 const CallInst &I,
7428 unsigned Intrinsic) const {
7429 switch (Intrinsic) {
7430 case Intrinsic::arm_neon_vld1:
7431 case Intrinsic::arm_neon_vld2:
7432 case Intrinsic::arm_neon_vld3:
7433 case Intrinsic::arm_neon_vld4:
7434 case Intrinsic::arm_neon_vld2lane:
7435 case Intrinsic::arm_neon_vld3lane:
7436 case Intrinsic::arm_neon_vld4lane: {
7437 Info.opc = ISD::INTRINSIC_W_CHAIN;
7438 // Conservatively set memVT to the entire set of vectors loaded.
7439 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7440 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7441 Info.ptrVal = I.getArgOperand(0);
7442 Info.offset = 0;
7443 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7444 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7445 Info.vol = false; // volatile loads with NEON intrinsics not supported
7446 Info.readMem = true;
7447 Info.writeMem = false;
7448 return true;
7449 }
7450 case Intrinsic::arm_neon_vst1:
7451 case Intrinsic::arm_neon_vst2:
7452 case Intrinsic::arm_neon_vst3:
7453 case Intrinsic::arm_neon_vst4:
7454 case Intrinsic::arm_neon_vst2lane:
7455 case Intrinsic::arm_neon_vst3lane:
7456 case Intrinsic::arm_neon_vst4lane: {
7457 Info.opc = ISD::INTRINSIC_VOID;
7458 // Conservatively set memVT to the entire set of vectors stored.
7459 unsigned NumElts = 0;
7460 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7461 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7462 if (!ArgTy->isVectorTy())
7463 break;
7464 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7465 }
7466 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7467 Info.ptrVal = I.getArgOperand(0);
7468 Info.offset = 0;
7469 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7470 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7471 Info.vol = false; // volatile stores with NEON intrinsics not supported
7472 Info.readMem = false;
7473 Info.writeMem = true;
7474 return true;
7475 }
7476 default:
7477 break;
7478 }
7479
7480 return false;
7481}