blob: 94dc6c2b859e9094764e43702f8f932d4466cfd4 [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
Owen Andersone50ed302009-08-10 22:56:29 +000080void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
81 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000082 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000083 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000084 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
85 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000086
Owen Anderson70671842009-08-10 20:18:46 +000087 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000088 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000089 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000090 }
91
Owen Andersone50ed302009-08-10 22:56:29 +000092 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000093 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000094 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +000095 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000096 if (ElemTy != MVT::i32) {
97 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
98 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
99 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
100 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
101 }
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
103 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000104 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000105 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000106 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
107 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000108 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000109 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
110 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
111 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000112 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
113 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000114 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
115 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
116 setTruncStoreAction(VT.getSimpleVT(),
117 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000118 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000119 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000120
121 // Promote all bit-wise operations.
122 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000123 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000124 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
125 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000126 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000127 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000128 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000129 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000130 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000131 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000132 }
Bob Wilson16330762009-09-16 00:17:28 +0000133
134 // Neon does not support vector divide/remainder operations.
135 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
136 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
137 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
138 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
139 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
140 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000141}
142
Owen Andersone50ed302009-08-10 22:56:29 +0000143void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000144 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000146}
147
Owen Andersone50ed302009-08-10 22:56:29 +0000148void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000149 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000151}
152
Chris Lattnerf0144122009-07-28 03:13:23 +0000153static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
154 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000155 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000156
Chris Lattner80ec2792009-08-02 00:34:36 +0000157 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000158}
159
Evan Chenga8e29892007-01-19 07:51:42 +0000160ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000161 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000162 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000163 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000164 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000165
Evan Chengb1df8f22007-04-27 08:15:43 +0000166 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000167 // Uses VFP for Thumb libfuncs if available.
168 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
169 // Single-precision floating-point arithmetic.
170 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
171 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
172 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
173 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000174
Evan Chengb1df8f22007-04-27 08:15:43 +0000175 // Double-precision floating-point arithmetic.
176 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
177 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
178 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
179 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 // Single-precision comparisons.
182 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
183 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
184 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
185 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
186 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
187 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
188 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
189 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000199
Evan Chengb1df8f22007-04-27 08:15:43 +0000200 // Double-precision comparisons.
201 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
202 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
203 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
204 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
205 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
206 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
207 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
208 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000209
Evan Chengb1df8f22007-04-27 08:15:43 +0000210 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000218
Evan Chengb1df8f22007-04-27 08:15:43 +0000219 // Floating-point to integer conversions.
220 // i64 conversions are done via library routines even when generating VFP
221 // instructions, so use the same ones.
222 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
223 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
224 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
225 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000226
Evan Chengb1df8f22007-04-27 08:15:43 +0000227 // Conversions between floating types.
228 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
229 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
230
231 // Integer to floating-point conversions.
232 // i64 conversions are done via library routines even when generating VFP
233 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000234 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
235 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000236 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
237 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
238 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
239 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
240 }
Evan Chenga8e29892007-01-19 07:51:42 +0000241 }
242
Bob Wilson2f954612009-05-22 17:38:41 +0000243 // These libcalls are not available in 32-bit.
244 setLibcallName(RTLIB::SHL_I128, 0);
245 setLibcallName(RTLIB::SRL_I128, 0);
246 setLibcallName(RTLIB::SRA_I128, 0);
247
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000248 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000249 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000250 // RTABI chapter 4.1.2, Table 2
251 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
252 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
253 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
254 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
255 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
256 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
257 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
258 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
259
260 // Double-precision floating-point comparison helper functions
261 // RTABI chapter 4.1.2, Table 3
262 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
263 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
264 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
265 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
266 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
267 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
268 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
269 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
270 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
271 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
272 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
273 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
274 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
275 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
276 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
277 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
278 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
279 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
280 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
281 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
282 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
283 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
284 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
285 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
286
287 // Single-precision floating-point arithmetic helper functions
288 // RTABI chapter 4.1.2, Table 4
289 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
290 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
291 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
292 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
293 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
297
298 // Single-precision floating-point comparison helper functions
299 // RTABI chapter 4.1.2, Table 5
300 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
301 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
302 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
303 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
304 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
305 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
306 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
307 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
308 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
309 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
310 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
311 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
312 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
313 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
314 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
315 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
316 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
317 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
318 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
319 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
320 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
321 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
322 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
323 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
324
325 // Floating-point to integer conversions.
326 // RTABI chapter 4.1.2, Table 6
327 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
328 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
329 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
330 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
331 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
332 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
333 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
334 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
335 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
340 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
341 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
342 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
343
344 // Conversions between floating types.
345 // RTABI chapter 4.1.2, Table 7
346 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
347 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
348 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000349 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000350
351 // Integer to floating-point conversions.
352 // RTABI chapter 4.1.2, Table 8
353 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
354 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
355 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
356 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
357 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
358 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
359 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
360 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
361 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
362 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
363 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
364 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
365 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
366 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
367 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
368 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
369
370 // Long long helper functions
371 // RTABI chapter 4.2, Table 9
372 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
373 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
374 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
375 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
376 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
377 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
378 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
384
385 // Integer division functions
386 // RTABI chapter 4.3.1
387 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
388 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
389 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
390 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
391 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
392 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
393 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000398 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000399 }
400
Evan Chengc8578942011-04-20 22:20:12 +0000401 // Use divmod iOS compiler-rt calls.
402 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
Evan Cheng8e23e812011-04-01 00:42:02 +0000403 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
404 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
405 }
406
David Goodwinf1daf7d2009-07-08 23:10:31 +0000407 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000409 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000411 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000413 if (!Subtarget->isFPOnlySP())
414 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000417 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000418
419 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 addDRTypeForNEON(MVT::v2f32);
421 addDRTypeForNEON(MVT::v8i8);
422 addDRTypeForNEON(MVT::v4i16);
423 addDRTypeForNEON(MVT::v2i32);
424 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 addQRTypeForNEON(MVT::v4f32);
427 addQRTypeForNEON(MVT::v2f64);
428 addQRTypeForNEON(MVT::v16i8);
429 addQRTypeForNEON(MVT::v8i16);
430 addQRTypeForNEON(MVT::v4i32);
431 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000432
Bob Wilson74dc72e2009-09-15 23:55:57 +0000433 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
434 // neither Neon nor VFP support any arithmetic operations on it.
435 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
436 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
437 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
438 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
439 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
440 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
441 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
442 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
443 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
444 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
445 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
446 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
447 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
448 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
449 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
450 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
451 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
452 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
453 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
454 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
455 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
456 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
457 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
458 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
459
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000460 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
461
Bob Wilson642b3292009-09-16 00:32:15 +0000462 // Neon does not support some operations on v1i64 and v2i64 types.
463 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000464 // Custom handling for some quad-vector types to detect VMULL.
465 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
466 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
467 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000468 // Custom handling for some vector types to avoid expensive expansions
469 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
470 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
471 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
472 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000473 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
474 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000475 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
476 // a destination type that is wider than the source.
477 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
478 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000479
Bob Wilson1c3ef902011-02-07 17:43:21 +0000480 setTargetDAGCombine(ISD::INTRINSIC_VOID);
481 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000482 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
483 setTargetDAGCombine(ISD::SHL);
484 setTargetDAGCombine(ISD::SRL);
485 setTargetDAGCombine(ISD::SRA);
486 setTargetDAGCombine(ISD::SIGN_EXTEND);
487 setTargetDAGCombine(ISD::ZERO_EXTEND);
488 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000489 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000490 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000491 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000492 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
493 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000494 }
495
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000496 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000497
498 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000500
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000501 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000503
Evan Chenga8e29892007-01-19 07:51:42 +0000504 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000505 if (!Subtarget->isThumb1Only()) {
506 for (unsigned im = (unsigned)ISD::PRE_INC;
507 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setIndexedLoadAction(im, MVT::i1, Legal);
509 setIndexedLoadAction(im, MVT::i8, Legal);
510 setIndexedLoadAction(im, MVT::i16, Legal);
511 setIndexedLoadAction(im, MVT::i32, Legal);
512 setIndexedStoreAction(im, MVT::i1, Legal);
513 setIndexedStoreAction(im, MVT::i8, Legal);
514 setIndexedStoreAction(im, MVT::i16, Legal);
515 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000516 }
Evan Chenga8e29892007-01-19 07:51:42 +0000517 }
518
519 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000520 setOperationAction(ISD::MUL, MVT::i64, Expand);
521 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000522 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
524 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000525 }
Eric Christopher2cc40132011-04-19 18:49:19 +0000526 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops())
527 setOperationAction(ISD::MULHS, MVT::i32, Expand);
528
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000529 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000530 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000531 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::SRL, MVT::i64, Custom);
533 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000534
535 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000537 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000539 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000541
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000542 // Only ARMv6 has BSWAP.
543 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000545
Evan Chenga8e29892007-01-19 07:51:42 +0000546 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000547 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000548 // v7M has a hardware divider
549 setOperationAction(ISD::SDIV, MVT::i32, Expand);
550 setOperationAction(ISD::UDIV, MVT::i32, Expand);
551 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::SREM, MVT::i32, Expand);
553 setOperationAction(ISD::UREM, MVT::i32, Expand);
554 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
555 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000556
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
558 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
559 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
560 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000561 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000562
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000563 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000564
Evan Chenga8e29892007-01-19 07:51:42 +0000565 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::VASTART, MVT::Other, Custom);
567 setOperationAction(ISD::VAARG, MVT::Other, Expand);
568 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
569 setOperationAction(ISD::VAEND, MVT::Other, Expand);
570 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
571 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000572 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000573 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
574 setExceptionPointerRegister(ARM::R0);
575 setExceptionSelectorRegister(ARM::R1);
576
Evan Cheng3a1588a2010-04-15 22:20:34 +0000577 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000578 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
579 // the default expansion.
580 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000581 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000582 // membarrier needs custom lowering; the rest are legal and handled
583 // normally.
584 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
585 } else {
586 // Set them all for expansion, which will force libcalls.
587 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
588 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
589 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
590 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000591 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
592 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
593 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000594 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
595 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
596 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
606 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
607 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
608 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
609 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000612 // Since the libcalls include locking, fold in the fences
613 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000614 }
615 // 64-bit versions are always libcalls (for now)
616 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000617 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000618 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000624
Evan Cheng416941d2010-11-04 05:19:35 +0000625 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000626
Eli Friedmana2c6f452010-06-26 04:36:50 +0000627 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
628 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
630 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000631 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000633
Nate Begemand1fb5832010-08-03 21:31:55 +0000634 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000635 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
636 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000637 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000638 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
639 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000640
641 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000643 if (Subtarget->isTargetDarwin()) {
644 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
645 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000646 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000647 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000648
Owen Anderson825b72b2009-08-11 20:47:22 +0000649 setOperationAction(ISD::SETCC, MVT::i32, Expand);
650 setOperationAction(ISD::SETCC, MVT::f32, Expand);
651 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000652 setOperationAction(ISD::SELECT, MVT::i32, Custom);
653 setOperationAction(ISD::SELECT, MVT::f32, Custom);
654 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
656 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
657 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000658
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
660 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
661 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
662 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
663 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000664
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000665 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::FSIN, MVT::f64, Expand);
667 setOperationAction(ISD::FSIN, MVT::f32, Expand);
668 setOperationAction(ISD::FCOS, MVT::f32, Expand);
669 setOperationAction(ISD::FCOS, MVT::f64, Expand);
670 setOperationAction(ISD::FREM, MVT::f64, Expand);
671 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000672 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
674 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000675 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::FPOW, MVT::f64, Expand);
677 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000678
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000679 // Various VFP goodness
680 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000681 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
682 if (Subtarget->hasVFP2()) {
683 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
684 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
685 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
686 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
687 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000688 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000689 if (!Subtarget->hasFP16()) {
690 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
691 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000692 }
Evan Cheng110cf482008-04-01 01:50:16 +0000693 }
Evan Chenga8e29892007-01-19 07:51:42 +0000694
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000695 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000696 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000697 setTargetDAGCombine(ISD::ADD);
698 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000699 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000700
Owen Anderson080c0922010-11-05 19:27:46 +0000701 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000702 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000703 if (Subtarget->hasNEON())
704 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000705
Evan Chenga8e29892007-01-19 07:51:42 +0000706 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000707
Evan Chengf7d87ee2010-05-21 00:43:17 +0000708 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
709 setSchedulingPreference(Sched::RegPressure);
710 else
711 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000712
Evan Cheng05219282011-01-06 06:52:41 +0000713 //// temporary - rewrite interface to use type
714 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000715
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000716 // On ARM arguments smaller than 4 bytes are extended, so all arguments
717 // are at least 4 bytes aligned.
718 setMinStackArgumentAlignment(4);
719
Evan Chengfff606d2010-09-24 19:07:23 +0000720 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000721}
722
Andrew Trick32cec0a2011-01-19 02:35:27 +0000723// FIXME: It might make sense to define the representative register class as the
724// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
725// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
726// SPR's representative would be DPR_VFP2. This should work well if register
727// pressure tracking were modified such that a register use would increment the
728// pressure of the register class's representative and all of it's super
729// classes' representatives transitively. We have not implemented this because
730// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000731// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000732// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000733std::pair<const TargetRegisterClass*, uint8_t>
734ARMTargetLowering::findRepresentativeClass(EVT VT) const{
735 const TargetRegisterClass *RRC = 0;
736 uint8_t Cost = 1;
737 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000738 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000739 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000740 // Use DPR as representative register class for all floating point
741 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
742 // the cost is 1 for both f32 and f64.
743 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000744 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000745 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000746 // When NEON is used for SP, only half of the register file is available
747 // because operations that define both SP and DP results will be constrained
748 // to the VFP2 class (D0-D15). We currently model this constraint prior to
749 // coalescing by double-counting the SP regs. See the FIXME above.
750 if (Subtarget->useNEONForSinglePrecisionFP())
751 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000752 break;
753 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
754 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000755 RRC = ARM::DPRRegisterClass;
756 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000757 break;
758 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000759 RRC = ARM::DPRRegisterClass;
760 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000761 break;
762 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000763 RRC = ARM::DPRRegisterClass;
764 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000765 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000766 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000767 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000768}
769
Evan Chenga8e29892007-01-19 07:51:42 +0000770const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
771 switch (Opcode) {
772 default: return 0;
773 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000774 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000775 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000776 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
777 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000778 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000779 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
780 case ARMISD::tCALL: return "ARMISD::tCALL";
781 case ARMISD::BRCOND: return "ARMISD::BRCOND";
782 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000783 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000784 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
785 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
786 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000787 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000788 case ARMISD::CMPFP: return "ARMISD::CMPFP";
789 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000790 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000791 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
792 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000793
Jim Grosbach3482c802010-01-18 19:58:49 +0000794 case ARMISD::RBIT: return "ARMISD::RBIT";
795
Bob Wilson76a312b2010-03-19 22:51:32 +0000796 case ARMISD::FTOSI: return "ARMISD::FTOSI";
797 case ARMISD::FTOUI: return "ARMISD::FTOUI";
798 case ARMISD::SITOF: return "ARMISD::SITOF";
799 case ARMISD::UITOF: return "ARMISD::UITOF";
800
Evan Chenga8e29892007-01-19 07:51:42 +0000801 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
802 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
803 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000804
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000805 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
806 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000807
Evan Chengc5942082009-10-28 06:55:03 +0000808 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
809 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000810 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000811
Dale Johannesen51e28e62010-06-03 21:09:53 +0000812 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000813
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000814 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000815
Evan Cheng86198642009-08-07 00:34:42 +0000816 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
817
Jim Grosbach3728e962009-12-10 00:11:09 +0000818 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000819 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000820
Evan Chengdfed19f2010-11-03 06:34:55 +0000821 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
822
Bob Wilson5bafff32009-06-22 23:27:02 +0000823 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000824 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000825 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000826 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
827 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000828 case ARMISD::VCGEU: return "ARMISD::VCGEU";
829 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000830 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
831 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000832 case ARMISD::VCGTU: return "ARMISD::VCGTU";
833 case ARMISD::VTST: return "ARMISD::VTST";
834
835 case ARMISD::VSHL: return "ARMISD::VSHL";
836 case ARMISD::VSHRs: return "ARMISD::VSHRs";
837 case ARMISD::VSHRu: return "ARMISD::VSHRu";
838 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
839 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
840 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
841 case ARMISD::VSHRN: return "ARMISD::VSHRN";
842 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
843 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
844 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
845 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
846 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
847 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
848 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
849 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
850 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
851 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
852 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
853 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
854 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
855 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000856 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000857 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000858 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000859 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000860 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000861 case ARMISD::VREV64: return "ARMISD::VREV64";
862 case ARMISD::VREV32: return "ARMISD::VREV32";
863 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000864 case ARMISD::VZIP: return "ARMISD::VZIP";
865 case ARMISD::VUZP: return "ARMISD::VUZP";
866 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000867 case ARMISD::VTBL1: return "ARMISD::VTBL1";
868 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000869 case ARMISD::VMULLs: return "ARMISD::VMULLs";
870 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000871 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000872 case ARMISD::FMAX: return "ARMISD::FMAX";
873 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000874 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000875 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
876 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000877 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000878 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
879 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
880 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000881 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
882 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
883 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
884 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
885 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
886 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
887 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
888 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
889 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
890 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
891 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
892 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
893 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
894 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
895 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
896 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
897 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000898 }
899}
900
Evan Cheng06b666c2010-05-15 02:18:07 +0000901/// getRegClassFor - Return the register class that should be used for the
902/// specified value type.
903TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
904 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
905 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
906 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000907 if (Subtarget->hasNEON()) {
908 if (VT == MVT::v4i64)
909 return ARM::QQPRRegisterClass;
910 else if (VT == MVT::v8i64)
911 return ARM::QQQQPRRegisterClass;
912 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000913 return TargetLowering::getRegClassFor(VT);
914}
915
Eric Christopherab695882010-07-21 22:26:11 +0000916// Create a fast isel object.
917FastISel *
918ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
919 return ARM::createFastISel(funcInfo);
920}
921
Bill Wendlingb4202b82009-07-01 18:50:55 +0000922/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000923unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000924 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000925}
926
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000927/// getMaximalGlobalOffset - Returns the maximal possible offset which can
928/// be used for loads / stores from the global.
929unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
930 return (Subtarget->isThumb1Only() ? 127 : 4095);
931}
932
Evan Cheng1cc39842010-05-20 23:26:43 +0000933Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000934 unsigned NumVals = N->getNumValues();
935 if (!NumVals)
936 return Sched::RegPressure;
937
938 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000939 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000940 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000941 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000942 if (VT.isFloatingPoint() || VT.isVector())
943 return Sched::Latency;
944 }
Evan Chengc10f5432010-05-28 23:25:23 +0000945
946 if (!N->isMachineOpcode())
947 return Sched::RegPressure;
948
949 // Load are scheduled for latency even if there instruction itinerary
950 // is not available.
951 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
952 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000953
954 if (TID.getNumDefs() == 0)
955 return Sched::RegPressure;
956 if (!Itins->isEmpty() &&
957 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000958 return Sched::Latency;
959
Evan Cheng1cc39842010-05-20 23:26:43 +0000960 return Sched::RegPressure;
961}
962
Evan Chenga8e29892007-01-19 07:51:42 +0000963//===----------------------------------------------------------------------===//
964// Lowering Code
965//===----------------------------------------------------------------------===//
966
Evan Chenga8e29892007-01-19 07:51:42 +0000967/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
968static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
969 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000970 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000971 case ISD::SETNE: return ARMCC::NE;
972 case ISD::SETEQ: return ARMCC::EQ;
973 case ISD::SETGT: return ARMCC::GT;
974 case ISD::SETGE: return ARMCC::GE;
975 case ISD::SETLT: return ARMCC::LT;
976 case ISD::SETLE: return ARMCC::LE;
977 case ISD::SETUGT: return ARMCC::HI;
978 case ISD::SETUGE: return ARMCC::HS;
979 case ISD::SETULT: return ARMCC::LO;
980 case ISD::SETULE: return ARMCC::LS;
981 }
982}
983
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000984/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
985static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000986 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000987 CondCode2 = ARMCC::AL;
988 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000989 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000990 case ISD::SETEQ:
991 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
992 case ISD::SETGT:
993 case ISD::SETOGT: CondCode = ARMCC::GT; break;
994 case ISD::SETGE:
995 case ISD::SETOGE: CondCode = ARMCC::GE; break;
996 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000997 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000998 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
999 case ISD::SETO: CondCode = ARMCC::VC; break;
1000 case ISD::SETUO: CondCode = ARMCC::VS; break;
1001 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1002 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1003 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1004 case ISD::SETLT:
1005 case ISD::SETULT: CondCode = ARMCC::LT; break;
1006 case ISD::SETLE:
1007 case ISD::SETULE: CondCode = ARMCC::LE; break;
1008 case ISD::SETNE:
1009 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1010 }
Evan Chenga8e29892007-01-19 07:51:42 +00001011}
1012
Bob Wilson1f595bb2009-04-17 19:07:39 +00001013//===----------------------------------------------------------------------===//
1014// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001015//===----------------------------------------------------------------------===//
1016
1017#include "ARMGenCallingConv.inc"
1018
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001019/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1020/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001021CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001022 bool Return,
1023 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001024 switch (CC) {
1025 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001026 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001027 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001028 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001029 if (!Subtarget->isAAPCS_ABI())
1030 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1031 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1032 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1033 }
1034 // Fallthrough
1035 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001036 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001037 if (!Subtarget->isAAPCS_ABI())
1038 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1039 else if (Subtarget->hasVFP2() &&
1040 FloatABIType == FloatABI::Hard && !isVarArg)
1041 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1042 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1043 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001044 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001045 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001046 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001047 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001048 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001049 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001050 }
1051}
1052
Dan Gohman98ca4f22009-08-05 01:29:28 +00001053/// LowerCallResult - Lower the result values of a call into the
1054/// appropriate copies out of appropriate physical registers.
1055SDValue
1056ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001057 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001058 const SmallVectorImpl<ISD::InputArg> &Ins,
1059 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001060 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001061
Bob Wilson1f595bb2009-04-17 19:07:39 +00001062 // Assign locations to each value returned by this call.
1063 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001064 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001065 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001066 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001067 CCAssignFnForNode(CallConv, /* Return*/ true,
1068 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069
1070 // Copy all of the result registers out of their specified physreg.
1071 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1072 CCValAssign VA = RVLocs[i];
1073
Bob Wilson80915242009-04-25 00:33:20 +00001074 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001075 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001076 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001077 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001078 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001079 Chain = Lo.getValue(1);
1080 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001081 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001083 InFlag);
1084 Chain = Hi.getValue(1);
1085 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001086 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001087
Owen Anderson825b72b2009-08-11 20:47:22 +00001088 if (VA.getLocVT() == MVT::v2f64) {
1089 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1090 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1091 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001092
1093 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001094 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001095 Chain = Lo.getValue(1);
1096 InFlag = Lo.getValue(2);
1097 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001098 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001099 Chain = Hi.getValue(1);
1100 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001101 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1103 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001104 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001105 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001106 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1107 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001108 Chain = Val.getValue(1);
1109 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001110 }
Bob Wilson80915242009-04-25 00:33:20 +00001111
1112 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001113 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001114 case CCValAssign::Full: break;
1115 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001116 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001117 break;
1118 }
1119
Dan Gohman98ca4f22009-08-05 01:29:28 +00001120 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001121 }
1122
Dan Gohman98ca4f22009-08-05 01:29:28 +00001123 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001124}
1125
Bob Wilsondee46d72009-04-17 20:35:10 +00001126/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001127SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001128ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1129 SDValue StackPtr, SDValue Arg,
1130 DebugLoc dl, SelectionDAG &DAG,
1131 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001132 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001133 unsigned LocMemOffset = VA.getLocMemOffset();
1134 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1135 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001137 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001138 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001139}
1140
Dan Gohman98ca4f22009-08-05 01:29:28 +00001141void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001142 SDValue Chain, SDValue &Arg,
1143 RegsToPassVector &RegsToPass,
1144 CCValAssign &VA, CCValAssign &NextVA,
1145 SDValue &StackPtr,
1146 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001147 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001148
Jim Grosbache5165492009-11-09 00:11:35 +00001149 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001150 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001151 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1152
1153 if (NextVA.isRegLoc())
1154 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1155 else {
1156 assert(NextVA.isMemLoc());
1157 if (StackPtr.getNode() == 0)
1158 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1159
Dan Gohman98ca4f22009-08-05 01:29:28 +00001160 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1161 dl, DAG, NextVA,
1162 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001163 }
1164}
1165
Dan Gohman98ca4f22009-08-05 01:29:28 +00001166/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001167/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1168/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001169SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001170ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001171 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001172 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001173 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001174 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175 const SmallVectorImpl<ISD::InputArg> &Ins,
1176 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001177 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001178 MachineFunction &MF = DAG.getMachineFunction();
1179 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1180 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001181 // Temporarily disable tail calls so things don't break.
1182 if (!EnableARMTailCalls)
1183 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001184 if (isTailCall) {
1185 // Check if it's really possible to do a tail call.
1186 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1187 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001188 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001189 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1190 // detected sibcalls.
1191 if (isTailCall) {
1192 ++NumTailCalls;
1193 IsSibCall = true;
1194 }
1195 }
Evan Chenga8e29892007-01-19 07:51:42 +00001196
Bob Wilson1f595bb2009-04-17 19:07:39 +00001197 // Analyze operands of the call, assigning locations to each operand.
1198 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001199 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1200 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00001201 CCInfo.setCallOrPrologue(Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001203 CCAssignFnForNode(CallConv, /* Return*/ false,
1204 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001205
Bob Wilson1f595bb2009-04-17 19:07:39 +00001206 // Get a count of how many bytes are to be pushed on the stack.
1207 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001208
Dale Johannesen51e28e62010-06-03 21:09:53 +00001209 // For tail calls, memory operands are available in our caller's stack.
1210 if (IsSibCall)
1211 NumBytes = 0;
1212
Evan Chenga8e29892007-01-19 07:51:42 +00001213 // Adjust the stack pointer for the new arguments...
1214 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001215 if (!IsSibCall)
1216 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001217
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001218 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001219
Bob Wilson5bafff32009-06-22 23:27:02 +00001220 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001221 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001222
Bob Wilson1f595bb2009-04-17 19:07:39 +00001223 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001224 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001225 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1226 i != e;
1227 ++i, ++realArgIdx) {
1228 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001229 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001230 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001231 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001232
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 // Promote the value if needed.
1234 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001235 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236 case CCValAssign::Full: break;
1237 case CCValAssign::SExt:
1238 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1239 break;
1240 case CCValAssign::ZExt:
1241 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1242 break;
1243 case CCValAssign::AExt:
1244 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1245 break;
1246 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001247 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001248 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001249 }
1250
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001251 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 if (VA.getLocVT() == MVT::v2f64) {
1254 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1255 DAG.getConstant(0, MVT::i32));
1256 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1257 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258
Dan Gohman98ca4f22009-08-05 01:29:28 +00001259 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001260 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1261
1262 VA = ArgLocs[++i]; // skip ahead to next loc
1263 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001264 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001265 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1266 } else {
1267 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001268
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1270 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001271 }
1272 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001275 }
1276 } else if (VA.isRegLoc()) {
1277 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001278 } else if (isByVal) {
1279 assert(VA.isMemLoc());
1280 unsigned offset = 0;
1281
1282 // True if this byval aggregate will be split between registers
1283 // and memory.
1284 if (CCInfo.isFirstByValRegValid()) {
1285 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1286 unsigned int i, j;
1287 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1288 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1289 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1290 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1291 MachinePointerInfo(),
1292 false, false, 0);
1293 MemOpChains.push_back(Load.getValue(1));
1294 RegsToPass.push_back(std::make_pair(j, Load));
1295 }
1296 offset = ARM::R4 - CCInfo.getFirstByValReg();
1297 CCInfo.clearFirstByValReg();
1298 }
1299
1300 unsigned LocMemOffset = VA.getLocMemOffset();
1301 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1302 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1303 StkPtrOff);
1304 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1305 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1306 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1307 MVT::i32);
1308 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1309 Flags.getByValAlign(),
1310 /*isVolatile=*/false,
1311 /*AlwaysInline=*/false,
1312 MachinePointerInfo(0),
1313 MachinePointerInfo(0)));
1314
1315 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001316 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001317
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1319 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001320 }
Evan Chenga8e29892007-01-19 07:51:42 +00001321 }
1322
1323 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001324 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001325 &MemOpChains[0], MemOpChains.size());
1326
1327 // Build a sequence of copy-to-reg nodes chained together with token chain
1328 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001329 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001330 // Tail call byval lowering might overwrite argument registers so in case of
1331 // tail call optimization the copies to registers are lowered later.
1332 if (!isTailCall)
1333 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1334 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1335 RegsToPass[i].second, InFlag);
1336 InFlag = Chain.getValue(1);
1337 }
Evan Chenga8e29892007-01-19 07:51:42 +00001338
Dale Johannesen51e28e62010-06-03 21:09:53 +00001339 // For tail calls lower the arguments to the 'real' stack slot.
1340 if (isTailCall) {
1341 // Force all the incoming stack arguments to be loaded from the stack
1342 // before any new outgoing arguments are stored to the stack, because the
1343 // outgoing stack slots may alias the incoming argument stack slots, and
1344 // the alias isn't otherwise explicit. This is slightly more conservative
1345 // than necessary, because it means that each store effectively depends
1346 // on every argument instead of just those arguments it would clobber.
1347
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001348 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001349 InFlag = SDValue();
1350 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1351 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1352 RegsToPass[i].second, InFlag);
1353 InFlag = Chain.getValue(1);
1354 }
1355 InFlag =SDValue();
1356 }
1357
Bill Wendling056292f2008-09-16 21:48:12 +00001358 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1359 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1360 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001361 bool isDirect = false;
1362 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001363 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001364 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001365
1366 if (EnableARMLongCalls) {
1367 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1368 && "long-calls with non-static relocation model!");
1369 // Handle a global address or an external symbol. If it's not one of
1370 // those, the target's already in a register, so we don't need to do
1371 // anything extra.
1372 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001373 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001374 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001375 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001376 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1377 ARMPCLabelIndex,
1378 ARMCP::CPValue, 0);
1379 // Get the address of the callee into a register
1380 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1381 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1382 Callee = DAG.getLoad(getPointerTy(), dl,
1383 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001384 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001385 false, false, 0);
1386 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1387 const char *Sym = S->getSymbol();
1388
1389 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001390 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001391 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1392 Sym, ARMPCLabelIndex, 0);
1393 // Get the address of the callee into a register
1394 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1395 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1396 Callee = DAG.getLoad(getPointerTy(), dl,
1397 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001398 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001399 false, false, 0);
1400 }
1401 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001402 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001403 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001404 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001405 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001406 getTargetMachine().getRelocationModel() != Reloc::Static;
1407 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001408 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001409 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001410 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001411 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001412 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001413 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001414 ARMPCLabelIndex,
1415 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001416 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001417 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001418 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001419 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001420 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001421 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001422 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001423 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001424 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001425 } else {
1426 // On ELF targets for PIC code, direct calls should go through the PLT
1427 unsigned OpFlags = 0;
1428 if (Subtarget->isTargetELF() &&
1429 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1430 OpFlags = ARMII::MO_PLT;
1431 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1432 }
Bill Wendling056292f2008-09-16 21:48:12 +00001433 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001434 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001435 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001436 getTargetMachine().getRelocationModel() != Reloc::Static;
1437 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001438 // tBX takes a register source operand.
1439 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001440 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001441 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001442 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001443 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001444 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001445 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001446 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001447 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001448 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001449 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001450 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001451 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001452 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001453 } else {
1454 unsigned OpFlags = 0;
1455 // On ELF targets for PIC code, direct calls should go through the PLT
1456 if (Subtarget->isTargetELF() &&
1457 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1458 OpFlags = ARMII::MO_PLT;
1459 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1460 }
Evan Chenga8e29892007-01-19 07:51:42 +00001461 }
1462
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001463 // FIXME: handle tail calls differently.
1464 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001465 if (Subtarget->isThumb()) {
1466 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001467 CallOpc = ARMISD::CALL_NOLINK;
1468 else
1469 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1470 } else {
1471 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001472 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1473 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001474 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001475
Dan Gohman475871a2008-07-27 21:46:04 +00001476 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001477 Ops.push_back(Chain);
1478 Ops.push_back(Callee);
1479
1480 // Add argument registers to the end of the list so that they are known live
1481 // into the call.
1482 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1483 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1484 RegsToPass[i].second.getValueType()));
1485
Gabor Greifba36cb52008-08-28 21:40:38 +00001486 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001487 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001488
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001489 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001490 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001491 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001492
Duncan Sands4bdcb612008-07-02 17:40:58 +00001493 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001494 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001495 InFlag = Chain.getValue(1);
1496
Chris Lattnere563bbc2008-10-11 22:08:30 +00001497 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1498 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001500 InFlag = Chain.getValue(1);
1501
Bob Wilson1f595bb2009-04-17 19:07:39 +00001502 // Handle result values, copying them out of physregs into vregs that we
1503 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1505 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001506}
1507
Stuart Hastingsf222e592011-02-28 17:17:53 +00001508/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001509/// on the stack. Remember the next parameter register to allocate,
1510/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001511/// this.
1512void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001513llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1514 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1515 assert((State->getCallOrPrologue() == Prologue ||
1516 State->getCallOrPrologue() == Call) &&
1517 "unhandled ParmContext");
1518 if ((!State->isFirstByValRegValid()) &&
1519 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1520 State->setFirstByValReg(reg);
1521 // At a call site, a byval parameter that is split between
1522 // registers and memory needs its size truncated here. In a
1523 // function prologue, such byval parameters are reassembled in
1524 // memory, and are not truncated.
1525 if (State->getCallOrPrologue() == Call) {
1526 unsigned excess = 4 * (ARM::R4 - reg);
1527 assert(size >= excess && "expected larger existing stack allocation");
1528 size -= excess;
1529 }
1530 }
1531 // Confiscate any remaining parameter registers to preclude their
1532 // assignment to subsequent parameters.
1533 while (State->AllocateReg(GPRArgRegs, 4))
1534 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001535}
1536
Dale Johannesen51e28e62010-06-03 21:09:53 +00001537/// MatchingStackOffset - Return true if the given stack call argument is
1538/// already available in the same position (relatively) of the caller's
1539/// incoming argument stack.
1540static
1541bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1542 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1543 const ARMInstrInfo *TII) {
1544 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1545 int FI = INT_MAX;
1546 if (Arg.getOpcode() == ISD::CopyFromReg) {
1547 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001548 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001549 return false;
1550 MachineInstr *Def = MRI->getVRegDef(VR);
1551 if (!Def)
1552 return false;
1553 if (!Flags.isByVal()) {
1554 if (!TII->isLoadFromStackSlot(Def, FI))
1555 return false;
1556 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001557 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001558 }
1559 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1560 if (Flags.isByVal())
1561 // ByVal argument is passed in as a pointer but it's now being
1562 // dereferenced. e.g.
1563 // define @foo(%struct.X* %A) {
1564 // tail call @bar(%struct.X* byval %A)
1565 // }
1566 return false;
1567 SDValue Ptr = Ld->getBasePtr();
1568 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1569 if (!FINode)
1570 return false;
1571 FI = FINode->getIndex();
1572 } else
1573 return false;
1574
1575 assert(FI != INT_MAX);
1576 if (!MFI->isFixedObjectIndex(FI))
1577 return false;
1578 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1579}
1580
1581/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1582/// for tail call optimization. Targets which want to do tail call
1583/// optimization should implement this function.
1584bool
1585ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1586 CallingConv::ID CalleeCC,
1587 bool isVarArg,
1588 bool isCalleeStructRet,
1589 bool isCallerStructRet,
1590 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001591 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001592 const SmallVectorImpl<ISD::InputArg> &Ins,
1593 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001594 const Function *CallerF = DAG.getMachineFunction().getFunction();
1595 CallingConv::ID CallerCC = CallerF->getCallingConv();
1596 bool CCMatch = CallerCC == CalleeCC;
1597
1598 // Look for obvious safe cases to perform tail call optimization that do not
1599 // require ABI changes. This is what gcc calls sibcall.
1600
Jim Grosbach7616b642010-06-16 23:45:49 +00001601 // Do not sibcall optimize vararg calls unless the call site is not passing
1602 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001603 if (isVarArg && !Outs.empty())
1604 return false;
1605
1606 // Also avoid sibcall optimization if either caller or callee uses struct
1607 // return semantics.
1608 if (isCalleeStructRet || isCallerStructRet)
1609 return false;
1610
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001611 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001612 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001613 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1614 // LR. This means if we need to reload LR, it takes an extra instructions,
1615 // which outweighs the value of the tail call; but here we don't know yet
1616 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001617 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001618 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001619
1620 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1621 // but we need to make sure there are enough registers; the only valid
1622 // registers are the 4 used for parameters. We don't currently do this
1623 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001624 if (Subtarget->isThumb1Only())
1625 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001626
Dale Johannesen51e28e62010-06-03 21:09:53 +00001627 // If the calling conventions do not match, then we'd better make sure the
1628 // results are returned in the same way as what the caller expects.
1629 if (!CCMatch) {
1630 SmallVector<CCValAssign, 16> RVLocs1;
1631 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1632 RVLocs1, *DAG.getContext());
1633 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1634
1635 SmallVector<CCValAssign, 16> RVLocs2;
1636 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1637 RVLocs2, *DAG.getContext());
1638 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1639
1640 if (RVLocs1.size() != RVLocs2.size())
1641 return false;
1642 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1643 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1644 return false;
1645 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1646 return false;
1647 if (RVLocs1[i].isRegLoc()) {
1648 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1649 return false;
1650 } else {
1651 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1652 return false;
1653 }
1654 }
1655 }
1656
1657 // If the callee takes no arguments then go on to check the results of the
1658 // call.
1659 if (!Outs.empty()) {
1660 // Check if stack adjustment is needed. For now, do not do this if any
1661 // argument is passed on the stack.
1662 SmallVector<CCValAssign, 16> ArgLocs;
1663 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1664 ArgLocs, *DAG.getContext());
1665 CCInfo.AnalyzeCallOperands(Outs,
1666 CCAssignFnForNode(CalleeCC, false, isVarArg));
1667 if (CCInfo.getNextStackOffset()) {
1668 MachineFunction &MF = DAG.getMachineFunction();
1669
1670 // Check if the arguments are already laid out in the right way as
1671 // the caller's fixed stack objects.
1672 MachineFrameInfo *MFI = MF.getFrameInfo();
1673 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1674 const ARMInstrInfo *TII =
1675 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001676 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1677 i != e;
1678 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001679 CCValAssign &VA = ArgLocs[i];
1680 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001681 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001682 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001683 if (VA.getLocInfo() == CCValAssign::Indirect)
1684 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001685 if (VA.needsCustom()) {
1686 // f64 and vector types are split into multiple registers or
1687 // register/stack-slot combinations. The types will not match
1688 // the registers; give up on memory f64 refs until we figure
1689 // out what to do about this.
1690 if (!VA.isRegLoc())
1691 return false;
1692 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001693 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001694 if (RegVT == MVT::v2f64) {
1695 if (!ArgLocs[++i].isRegLoc())
1696 return false;
1697 if (!ArgLocs[++i].isRegLoc())
1698 return false;
1699 }
1700 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001701 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1702 MFI, MRI, TII))
1703 return false;
1704 }
1705 }
1706 }
1707 }
1708
1709 return true;
1710}
1711
Dan Gohman98ca4f22009-08-05 01:29:28 +00001712SDValue
1713ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001714 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001716 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001717 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001718
Bob Wilsondee46d72009-04-17 20:35:10 +00001719 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001720 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001721
Bob Wilsondee46d72009-04-17 20:35:10 +00001722 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1724 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001725
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001727 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1728 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001729
1730 // If this is the first return lowered for this function, add
1731 // the regs to the liveout set for the function.
1732 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1733 for (unsigned i = 0; i != RVLocs.size(); ++i)
1734 if (RVLocs[i].isRegLoc())
1735 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001736 }
1737
Bob Wilson1f595bb2009-04-17 19:07:39 +00001738 SDValue Flag;
1739
1740 // Copy the result values into the output registers.
1741 for (unsigned i = 0, realRVLocIdx = 0;
1742 i != RVLocs.size();
1743 ++i, ++realRVLocIdx) {
1744 CCValAssign &VA = RVLocs[i];
1745 assert(VA.isRegLoc() && "Can only return in registers!");
1746
Dan Gohmanc9403652010-07-07 15:54:55 +00001747 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001748
1749 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001750 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001751 case CCValAssign::Full: break;
1752 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001753 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001754 break;
1755 }
1756
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001759 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001760 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1761 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001762 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001764
1765 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1766 Flag = Chain.getValue(1);
1767 VA = RVLocs[++i]; // skip ahead to next loc
1768 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1769 HalfGPRs.getValue(1), Flag);
1770 Flag = Chain.getValue(1);
1771 VA = RVLocs[++i]; // skip ahead to next loc
1772
1773 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1775 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001776 }
1777 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1778 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001779 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001781 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001782 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001783 VA = RVLocs[++i]; // skip ahead to next loc
1784 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1785 Flag);
1786 } else
1787 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1788
Bob Wilsondee46d72009-04-17 20:35:10 +00001789 // Guarantee that all emitted copies are
1790 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001791 Flag = Chain.getValue(1);
1792 }
1793
1794 SDValue result;
1795 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001797 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001799
1800 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001801}
1802
Evan Cheng3d2125c2010-11-30 23:55:39 +00001803bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1804 if (N->getNumValues() != 1)
1805 return false;
1806 if (!N->hasNUsesOfValue(1, 0))
1807 return false;
1808
1809 unsigned NumCopies = 0;
1810 SDNode* Copies[2];
1811 SDNode *Use = *N->use_begin();
1812 if (Use->getOpcode() == ISD::CopyToReg) {
1813 Copies[NumCopies++] = Use;
1814 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1815 // f64 returned in a pair of GPRs.
1816 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1817 UI != UE; ++UI) {
1818 if (UI->getOpcode() != ISD::CopyToReg)
1819 return false;
1820 Copies[UI.getUse().getResNo()] = *UI;
1821 ++NumCopies;
1822 }
1823 } else if (Use->getOpcode() == ISD::BITCAST) {
1824 // f32 returned in a single GPR.
1825 if (!Use->hasNUsesOfValue(1, 0))
1826 return false;
1827 Use = *Use->use_begin();
1828 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1829 return false;
1830 Copies[NumCopies++] = Use;
1831 } else {
1832 return false;
1833 }
1834
1835 if (NumCopies != 1 && NumCopies != 2)
1836 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001837
1838 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001839 for (unsigned i = 0; i < NumCopies; ++i) {
1840 SDNode *Copy = Copies[i];
1841 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1842 UI != UE; ++UI) {
1843 if (UI->getOpcode() == ISD::CopyToReg) {
1844 SDNode *Use = *UI;
1845 if (Use == Copies[0] || Use == Copies[1])
1846 continue;
1847 return false;
1848 }
1849 if (UI->getOpcode() != ARMISD::RET_FLAG)
1850 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001851 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001852 }
1853 }
1854
Evan Cheng1bf891a2010-12-01 22:59:46 +00001855 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001856}
1857
Evan Cheng485fafc2011-03-21 01:19:09 +00001858bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1859 if (!EnableARMTailCalls)
1860 return false;
1861
1862 if (!CI->isTailCall())
1863 return false;
1864
1865 return !Subtarget->isThumb1Only();
1866}
1867
Bob Wilsonb62d2572009-11-03 00:02:05 +00001868// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1869// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1870// one of the above mentioned nodes. It has to be wrapped because otherwise
1871// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1872// be used to form addressing mode. These wrapped nodes will be selected
1873// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001874static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001875 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001876 // FIXME there is no actual debug info here
1877 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001878 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001879 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001880 if (CP->isMachineConstantPoolEntry())
1881 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1882 CP->getAlignment());
1883 else
1884 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1885 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001886 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001887}
1888
Jim Grosbache1102ca2010-07-19 17:20:38 +00001889unsigned ARMTargetLowering::getJumpTableEncoding() const {
1890 return MachineJumpTableInfo::EK_Inline;
1891}
1892
Dan Gohmand858e902010-04-17 15:26:15 +00001893SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1894 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001895 MachineFunction &MF = DAG.getMachineFunction();
1896 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1897 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001898 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001899 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001900 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001901 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1902 SDValue CPAddr;
1903 if (RelocM == Reloc::Static) {
1904 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1905 } else {
1906 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001907 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001908 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1909 ARMCP::CPBlockAddress,
1910 PCAdj);
1911 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1912 }
1913 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1914 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001915 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001916 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001917 if (RelocM == Reloc::Static)
1918 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001919 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001920 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001921}
1922
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001923// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001924SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001925ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001926 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001927 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001928 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001929 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001930 MachineFunction &MF = DAG.getMachineFunction();
1931 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001932 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001933 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001934 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001935 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001936 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001937 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001938 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001939 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001940 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001941 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001942
Evan Chenge7e0d622009-11-06 22:24:13 +00001943 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001944 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001945
1946 // call __tls_get_addr.
1947 ArgListTy Args;
1948 ArgListEntry Entry;
1949 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001950 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001951 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001952 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001953 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001954 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1955 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001956 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001957 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001958 return CallResult.first;
1959}
1960
1961// Lower ISD::GlobalTLSAddress using the "initial exec" or
1962// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001963SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001964ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001965 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001966 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001967 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SDValue Offset;
1969 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001970 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001971 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001972 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001973
Chris Lattner4fb63d02009-07-15 04:12:33 +00001974 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001975 MachineFunction &MF = DAG.getMachineFunction();
1976 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001977 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001978 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001979 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1980 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001981 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001982 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001983 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001984 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001985 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001986 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001987 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001988 Chain = Offset.getValue(1);
1989
Evan Chenge7e0d622009-11-06 22:24:13 +00001990 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001991 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001992
Evan Cheng9eda6892009-10-31 03:39:36 +00001993 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001994 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001995 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001996 } else {
1997 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001998 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001999 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002001 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002002 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002003 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002004 }
2005
2006 // The address of the thread local variable is the add of the thread
2007 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002008 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002009}
2010
Dan Gohman475871a2008-07-27 21:46:04 +00002011SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002012ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002013 // TODO: implement the "local dynamic" model
2014 assert(Subtarget->isTargetELF() &&
2015 "TLS not implemented for non-ELF targets");
2016 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2017 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2018 // otherwise use the "Local Exec" TLS Model
2019 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2020 return LowerToTLSGeneralDynamicModel(GA, DAG);
2021 else
2022 return LowerToTLSExecModels(GA, DAG);
2023}
2024
Dan Gohman475871a2008-07-27 21:46:04 +00002025SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002026 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002027 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002028 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002029 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002030 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2031 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002032 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002033 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002034 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002035 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002037 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002038 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002039 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002040 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002042 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002043 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002044 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002045 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002046 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002047 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002048 }
2049
2050 // If we have T2 ops, we can materialize the address directly via movt/movw
2051 // pair. This is always cheaper.
2052 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002053 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002054 // FIXME: Once remat is capable of dealing with instructions with register
2055 // operands, expand this into two nodes.
2056 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2057 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002058 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002059 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2060 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2061 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2062 MachinePointerInfo::getConstantPool(),
2063 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002064 }
2065}
2066
Dan Gohman475871a2008-07-27 21:46:04 +00002067SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002068 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002069 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002070 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002071 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002072 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002073 MachineFunction &MF = DAG.getMachineFunction();
2074 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2075
2076 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002077 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002078 // FIXME: Once remat is capable of dealing with instructions with register
2079 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002080 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002081 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2082 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2083
Evan Cheng53519f02011-01-21 18:55:51 +00002084 unsigned Wrapper = (RelocM == Reloc::PIC_)
2085 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2086 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002087 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002088 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2089 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2090 MachinePointerInfo::getGOT(), false, false, 0);
2091 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002092 }
2093
2094 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002095 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002096 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002097 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002098 } else {
2099 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002100 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2101 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002102 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002103 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002104 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002105 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002106
Evan Cheng9eda6892009-10-31 03:39:36 +00002107 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002108 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002109 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002111
2112 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002113 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002114 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002115 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002116
Evan Cheng63476a82009-09-03 07:04:02 +00002117 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002118 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002119 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002120
2121 return Result;
2122}
2123
Dan Gohman475871a2008-07-27 21:46:04 +00002124SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002125 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002126 assert(Subtarget->isTargetELF() &&
2127 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002128 MachineFunction &MF = DAG.getMachineFunction();
2129 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002130 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002131 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002132 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002133 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002134 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2135 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002136 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002137 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002138 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002139 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002140 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002141 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002142 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002143 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002144}
2145
Jim Grosbach0e0da732009-05-12 23:59:14 +00002146SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002147ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2148 const {
2149 DebugLoc dl = Op.getDebugLoc();
2150 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendlingf05b1dc2011-04-05 01:37:43 +00002151 Op.getOperand(0));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002152}
2153
2154SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002155ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2156 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002157 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002158 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2159 Op.getOperand(1), Val);
2160}
2161
2162SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002163ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2164 DebugLoc dl = Op.getDebugLoc();
2165 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2166 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2167}
2168
2169SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002170ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002171 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002172 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002173 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002174 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002175 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002176 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002177 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002178 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2179 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002180 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002181 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002182 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002183 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002184 EVT PtrVT = getPointerTy();
2185 DebugLoc dl = Op.getDebugLoc();
2186 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2187 SDValue CPAddr;
2188 unsigned PCAdj = (RelocM != Reloc::PIC_)
2189 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002190 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002191 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2192 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002193 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002194 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002195 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002196 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002197 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002198 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002199
2200 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002201 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002202 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2203 }
2204 return Result;
2205 }
Evan Cheng92e39162011-03-29 23:06:19 +00002206 case Intrinsic::arm_neon_vmulls:
2207 case Intrinsic::arm_neon_vmullu: {
2208 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2209 ? ARMISD::VMULLs : ARMISD::VMULLu;
2210 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2211 Op.getOperand(1), Op.getOperand(2));
2212 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002213 }
2214}
2215
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002216static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002217 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002218 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002219 if (!Subtarget->hasDataBarrier()) {
2220 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2221 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2222 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002223 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002224 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002225 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002226 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002227 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002228
2229 SDValue Op5 = Op.getOperand(5);
2230 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2231 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2232 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2233 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2234
2235 ARM_MB::MemBOpt DMBOpt;
2236 if (isDeviceBarrier)
2237 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2238 else
2239 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2240 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2241 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002242}
2243
Evan Chengdfed19f2010-11-03 06:34:55 +00002244static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2245 const ARMSubtarget *Subtarget) {
2246 // ARM pre v5TE and Thumb1 does not have preload instructions.
2247 if (!(Subtarget->isThumb2() ||
2248 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2249 // Just preserve the chain.
2250 return Op.getOperand(0);
2251
2252 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002253 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2254 if (!isRead &&
2255 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2256 // ARMv7 with MP extension has PLDW.
2257 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002258
2259 if (Subtarget->isThumb())
2260 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002261 isRead = ~isRead & 1;
2262 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002263
Evan Cheng416941d2010-11-04 05:19:35 +00002264 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002265 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002266 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2267 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002268}
2269
Dan Gohman1e93df62010-04-17 14:41:14 +00002270static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2271 MachineFunction &MF = DAG.getMachineFunction();
2272 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2273
Evan Chenga8e29892007-01-19 07:51:42 +00002274 // vastart just stores the address of the VarArgsFrameIndex slot into the
2275 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002276 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002277 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002278 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002279 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002280 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2281 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002282}
2283
Dan Gohman475871a2008-07-27 21:46:04 +00002284SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002285ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2286 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002287 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002288 MachineFunction &MF = DAG.getMachineFunction();
2289 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2290
2291 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002292 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002293 RC = ARM::tGPRRegisterClass;
2294 else
2295 RC = ARM::GPRRegisterClass;
2296
2297 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002298 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002300
2301 SDValue ArgValue2;
2302 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002303 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002304 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002305
2306 // Create load node to retrieve arguments from the stack.
2307 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002308 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002309 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002310 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002311 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002312 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002313 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002314 }
2315
Jim Grosbache5165492009-11-09 00:11:35 +00002316 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002317}
2318
Stuart Hastingsc7315872011-04-20 16:47:52 +00002319void
2320ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2321 unsigned &VARegSize, unsigned &VARegSaveSize)
2322 const {
2323 unsigned NumGPRs;
2324 if (CCInfo.isFirstByValRegValid())
2325 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2326 else {
2327 unsigned int firstUnalloced;
2328 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2329 sizeof(GPRArgRegs) /
2330 sizeof(GPRArgRegs[0]));
2331 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2332 }
2333
2334 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2335 VARegSize = NumGPRs * 4;
2336 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2337}
2338
2339// The remaining GPRs hold either the beginning of variable-argument
2340// data, or the beginning of an aggregate passed by value (usuall
2341// byval). Either way, we allocate stack slots adjacent to the data
2342// provided by our caller, and store the unallocated registers there.
2343// If this is a variadic function, the va_list pointer will begin with
2344// these values; otherwise, this reassembles a (byval) structure that
2345// was split between registers and memory.
2346void
2347ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2348 DebugLoc dl, SDValue &Chain,
2349 unsigned ArgOffset) const {
2350 MachineFunction &MF = DAG.getMachineFunction();
2351 MachineFrameInfo *MFI = MF.getFrameInfo();
2352 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2353 unsigned firstRegToSaveIndex;
2354 if (CCInfo.isFirstByValRegValid())
2355 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2356 else {
2357 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2358 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2359 }
2360
2361 unsigned VARegSize, VARegSaveSize;
2362 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2363 if (VARegSaveSize) {
2364 // If this function is vararg, store any remaining integer argument regs
2365 // to their spots on the stack so that they may be loaded by deferencing
2366 // the result of va_next.
2367 AFI->setVarArgsRegSaveSize(VARegSaveSize);
2368 AFI->setVarArgsFrameIndex(
2369 MFI->CreateFixedObject(VARegSaveSize,
2370 ArgOffset + VARegSaveSize - VARegSize,
2371 false));
2372 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2373 getPointerTy());
2374
2375 SmallVector<SDValue, 4> MemOps;
2376 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2377 TargetRegisterClass *RC;
2378 if (AFI->isThumb1OnlyFunction())
2379 RC = ARM::tGPRRegisterClass;
2380 else
2381 RC = ARM::GPRRegisterClass;
2382
2383 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2384 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2385 SDValue Store =
2386 DAG.getStore(Val.getValue(1), dl, Val, FIN,
2387 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2388 false, false, 0);
2389 MemOps.push_back(Store);
2390 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2391 DAG.getConstant(4, getPointerTy()));
2392 }
2393 if (!MemOps.empty())
2394 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2395 &MemOps[0], MemOps.size());
2396 } else
2397 // This will point to the next argument passed via stack.
2398 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2399}
2400
Bob Wilson5bafff32009-06-22 23:27:02 +00002401SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002402ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002403 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002404 const SmallVectorImpl<ISD::InputArg>
2405 &Ins,
2406 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002407 SmallVectorImpl<SDValue> &InVals)
2408 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002409 MachineFunction &MF = DAG.getMachineFunction();
2410 MachineFrameInfo *MFI = MF.getFrameInfo();
2411
Bob Wilson1f595bb2009-04-17 19:07:39 +00002412 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2413
2414 // Assign locations to all of the incoming arguments.
2415 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002416 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2417 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00002418 CCInfo.setCallOrPrologue(Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002419 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002420 CCAssignFnForNode(CallConv, /* Return*/ false,
2421 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002422
2423 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002424 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002425
Stuart Hastingsf222e592011-02-28 17:17:53 +00002426 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002427 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2428 CCValAssign &VA = ArgLocs[i];
2429
Bob Wilsondee46d72009-04-17 20:35:10 +00002430 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002431 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002432 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002433
Bob Wilson1f595bb2009-04-17 19:07:39 +00002434 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002435 // f64 and vector types are split up into multiple registers or
2436 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002437 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002438 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002439 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002440 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002441 SDValue ArgValue2;
2442 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002443 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002444 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2445 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002446 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002447 false, false, 0);
2448 } else {
2449 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2450 Chain, DAG, dl);
2451 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002452 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2453 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002454 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002455 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002456 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2457 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002458 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002459
Bob Wilson5bafff32009-06-22 23:27:02 +00002460 } else {
2461 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002462
Owen Anderson825b72b2009-08-11 20:47:22 +00002463 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002464 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002465 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002466 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002467 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002468 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002469 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002470 RC = (AFI->isThumb1OnlyFunction() ?
2471 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002472 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002473 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002474
2475 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002476 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002477 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002478 }
2479
2480 // If this is an 8 or 16-bit value, it is really passed promoted
2481 // to 32 bits. Insert an assert[sz]ext to capture this, then
2482 // truncate to the right size.
2483 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002484 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002485 case CCValAssign::Full: break;
2486 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002487 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002488 break;
2489 case CCValAssign::SExt:
2490 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2491 DAG.getValueType(VA.getValVT()));
2492 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2493 break;
2494 case CCValAssign::ZExt:
2495 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2496 DAG.getValueType(VA.getValVT()));
2497 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2498 break;
2499 }
2500
Dan Gohman98ca4f22009-08-05 01:29:28 +00002501 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002502
2503 } else { // VA.isRegLoc()
2504
2505 // sanity check
2506 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002507 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002508
Stuart Hastingsf222e592011-02-28 17:17:53 +00002509 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002510
Stuart Hastingsf222e592011-02-28 17:17:53 +00002511 // Some Ins[] entries become multiple ArgLoc[] entries.
2512 // Process them only once.
2513 if (index != lastInsIndex)
2514 {
2515 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2516 // FIXME: For now, all byval parameter objects are marked mutable. This can be
2517 // changed with more analysis.
2518 // In case of tail call optimization mark all arguments mutable. Since they
2519 // could be overwritten by lowering of arguments in case of a tail call.
2520 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002521 unsigned VARegSize, VARegSaveSize;
2522 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2523 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2524 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002525 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002526 int FI = MFI->CreateFixedObject(Bytes,
2527 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002528 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2529 } else {
2530 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2531 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002532
Stuart Hastingsf222e592011-02-28 17:17:53 +00002533 // Create load nodes to retrieve arguments from the stack.
2534 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2535 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2536 MachinePointerInfo::getFixedStack(FI),
2537 false, false, 0));
2538 }
2539 lastInsIndex = index;
2540 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002541 }
2542 }
2543
2544 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002545 if (isVarArg)
2546 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002547
Dan Gohman98ca4f22009-08-05 01:29:28 +00002548 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002549}
2550
2551/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002552static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002553 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002554 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002555 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002556 // Maybe this has already been legalized into the constant pool?
2557 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002558 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002559 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002560 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002561 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002562 }
2563 }
2564 return false;
2565}
2566
Evan Chenga8e29892007-01-19 07:51:42 +00002567/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2568/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002569SDValue
2570ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002571 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002572 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002573 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002574 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002575 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002576 // Constant does not fit, try adjusting it by one?
2577 switch (CC) {
2578 default: break;
2579 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002580 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002581 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002582 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002583 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002584 }
2585 break;
2586 case ISD::SETULT:
2587 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002588 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002589 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002590 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002591 }
2592 break;
2593 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002594 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002595 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002596 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002597 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002598 }
2599 break;
2600 case ISD::SETULE:
2601 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002602 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002603 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002604 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002605 }
2606 break;
2607 }
2608 }
2609 }
2610
2611 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002612 ARMISD::NodeType CompareType;
2613 switch (CondCode) {
2614 default:
2615 CompareType = ARMISD::CMP;
2616 break;
2617 case ARMCC::EQ:
2618 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002619 // Uses only Z Flag
2620 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002621 break;
2622 }
Evan Cheng218977b2010-07-13 19:27:42 +00002623 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002624 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002625}
2626
2627/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002628SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002629ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002630 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002631 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002632 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002633 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002634 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002635 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2636 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002637}
2638
Bob Wilson79f56c92011-03-08 01:17:20 +00002639/// duplicateCmp - Glue values can have only one use, so this function
2640/// duplicates a comparison node.
2641SDValue
2642ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2643 unsigned Opc = Cmp.getOpcode();
2644 DebugLoc DL = Cmp.getDebugLoc();
2645 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2646 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2647
2648 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2649 Cmp = Cmp.getOperand(0);
2650 Opc = Cmp.getOpcode();
2651 if (Opc == ARMISD::CMPFP)
2652 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2653 else {
2654 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2655 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2656 }
2657 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2658}
2659
Bill Wendlingde2b1512010-08-11 08:43:16 +00002660SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2661 SDValue Cond = Op.getOperand(0);
2662 SDValue SelectTrue = Op.getOperand(1);
2663 SDValue SelectFalse = Op.getOperand(2);
2664 DebugLoc dl = Op.getDebugLoc();
2665
2666 // Convert:
2667 //
2668 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2669 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2670 //
2671 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2672 const ConstantSDNode *CMOVTrue =
2673 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2674 const ConstantSDNode *CMOVFalse =
2675 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2676
2677 if (CMOVTrue && CMOVFalse) {
2678 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2679 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2680
2681 SDValue True;
2682 SDValue False;
2683 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2684 True = SelectTrue;
2685 False = SelectFalse;
2686 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2687 True = SelectFalse;
2688 False = SelectTrue;
2689 }
2690
2691 if (True.getNode() && False.getNode()) {
2692 EVT VT = Cond.getValueType();
2693 SDValue ARMcc = Cond.getOperand(2);
2694 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002695 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002696 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2697 }
2698 }
2699 }
2700
2701 return DAG.getSelectCC(dl, Cond,
2702 DAG.getConstant(0, Cond.getValueType()),
2703 SelectTrue, SelectFalse, ISD::SETNE);
2704}
2705
Dan Gohmand858e902010-04-17 15:26:15 +00002706SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002707 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002708 SDValue LHS = Op.getOperand(0);
2709 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002710 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002711 SDValue TrueVal = Op.getOperand(2);
2712 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002713 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002714
Owen Anderson825b72b2009-08-11 20:47:22 +00002715 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002716 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002717 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002718 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2719 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002720 }
2721
2722 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002723 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002724
Evan Cheng218977b2010-07-13 19:27:42 +00002725 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2726 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002727 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002728 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002729 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002730 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002731 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002732 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002733 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002734 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002735 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002736 }
2737 return Result;
2738}
2739
Evan Cheng218977b2010-07-13 19:27:42 +00002740/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2741/// to morph to an integer compare sequence.
2742static bool canChangeToInt(SDValue Op, bool &SeenZero,
2743 const ARMSubtarget *Subtarget) {
2744 SDNode *N = Op.getNode();
2745 if (!N->hasOneUse())
2746 // Otherwise it requires moving the value from fp to integer registers.
2747 return false;
2748 if (!N->getNumValues())
2749 return false;
2750 EVT VT = Op.getValueType();
2751 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2752 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2753 // vmrs are very slow, e.g. cortex-a8.
2754 return false;
2755
2756 if (isFloatingPointZero(Op)) {
2757 SeenZero = true;
2758 return true;
2759 }
2760 return ISD::isNormalLoad(N);
2761}
2762
2763static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2764 if (isFloatingPointZero(Op))
2765 return DAG.getConstant(0, MVT::i32);
2766
2767 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2768 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002769 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002770 Ld->isVolatile(), Ld->isNonTemporal(),
2771 Ld->getAlignment());
2772
2773 llvm_unreachable("Unknown VFP cmp argument!");
2774}
2775
2776static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2777 SDValue &RetVal1, SDValue &RetVal2) {
2778 if (isFloatingPointZero(Op)) {
2779 RetVal1 = DAG.getConstant(0, MVT::i32);
2780 RetVal2 = DAG.getConstant(0, MVT::i32);
2781 return;
2782 }
2783
2784 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2785 SDValue Ptr = Ld->getBasePtr();
2786 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2787 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002788 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002789 Ld->isVolatile(), Ld->isNonTemporal(),
2790 Ld->getAlignment());
2791
2792 EVT PtrType = Ptr.getValueType();
2793 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2794 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2795 PtrType, Ptr, DAG.getConstant(4, PtrType));
2796 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2797 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002798 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002799 Ld->isVolatile(), Ld->isNonTemporal(),
2800 NewAlign);
2801 return;
2802 }
2803
2804 llvm_unreachable("Unknown VFP cmp argument!");
2805}
2806
2807/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2808/// f32 and even f64 comparisons to integer ones.
2809SDValue
2810ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2811 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002812 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002813 SDValue LHS = Op.getOperand(2);
2814 SDValue RHS = Op.getOperand(3);
2815 SDValue Dest = Op.getOperand(4);
2816 DebugLoc dl = Op.getDebugLoc();
2817
2818 bool SeenZero = false;
2819 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2820 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002821 // If one of the operand is zero, it's safe to ignore the NaN case since
2822 // we only care about equality comparisons.
2823 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002824 // If unsafe fp math optimization is enabled and there are no other uses of
2825 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002826 // to an integer comparison.
2827 if (CC == ISD::SETOEQ)
2828 CC = ISD::SETEQ;
2829 else if (CC == ISD::SETUNE)
2830 CC = ISD::SETNE;
2831
2832 SDValue ARMcc;
2833 if (LHS.getValueType() == MVT::f32) {
2834 LHS = bitcastf32Toi32(LHS, DAG);
2835 RHS = bitcastf32Toi32(RHS, DAG);
2836 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2837 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2838 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2839 Chain, Dest, ARMcc, CCR, Cmp);
2840 }
2841
2842 SDValue LHS1, LHS2;
2843 SDValue RHS1, RHS2;
2844 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2845 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2846 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2847 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002848 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002849 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2850 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2851 }
2852
2853 return SDValue();
2854}
2855
2856SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2857 SDValue Chain = Op.getOperand(0);
2858 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2859 SDValue LHS = Op.getOperand(2);
2860 SDValue RHS = Op.getOperand(3);
2861 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002862 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002863
Owen Anderson825b72b2009-08-11 20:47:22 +00002864 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002865 SDValue ARMcc;
2866 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002867 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002868 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002869 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002870 }
2871
Owen Anderson825b72b2009-08-11 20:47:22 +00002872 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002873
2874 if (UnsafeFPMath &&
2875 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2876 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2877 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2878 if (Result.getNode())
2879 return Result;
2880 }
2881
Evan Chenga8e29892007-01-19 07:51:42 +00002882 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002883 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002884
Evan Cheng218977b2010-07-13 19:27:42 +00002885 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2886 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002887 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002888 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002889 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002890 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002891 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002892 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2893 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002894 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002895 }
2896 return Res;
2897}
2898
Dan Gohmand858e902010-04-17 15:26:15 +00002899SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002900 SDValue Chain = Op.getOperand(0);
2901 SDValue Table = Op.getOperand(1);
2902 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002903 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002904
Owen Andersone50ed302009-08-10 22:56:29 +00002905 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002906 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2907 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002908 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002909 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002911 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2912 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002913 if (Subtarget->isThumb2()) {
2914 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2915 // which does another jump to the destination. This also makes it easier
2916 // to translate it to TBB / TBH later.
2917 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002918 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002919 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002920 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002921 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002922 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002923 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002924 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002925 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002926 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002927 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002928 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002929 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002930 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002931 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002932 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002933 }
Evan Chenga8e29892007-01-19 07:51:42 +00002934}
2935
Bob Wilson76a312b2010-03-19 22:51:32 +00002936static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2937 DebugLoc dl = Op.getDebugLoc();
2938 unsigned Opc;
2939
2940 switch (Op.getOpcode()) {
2941 default:
2942 assert(0 && "Invalid opcode!");
2943 case ISD::FP_TO_SINT:
2944 Opc = ARMISD::FTOSI;
2945 break;
2946 case ISD::FP_TO_UINT:
2947 Opc = ARMISD::FTOUI;
2948 break;
2949 }
2950 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002951 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002952}
2953
Cameron Zwarich3007d332011-03-29 21:41:55 +00002954static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2955 EVT VT = Op.getValueType();
2956 DebugLoc dl = Op.getDebugLoc();
2957
2958 EVT OperandVT = Op.getOperand(0).getValueType();
2959 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2960 if (VT != MVT::v4f32)
2961 return DAG.UnrollVectorOp(Op.getNode());
2962
2963 unsigned CastOpc;
2964 unsigned Opc;
2965 switch (Op.getOpcode()) {
2966 default:
2967 assert(0 && "Invalid opcode!");
2968 case ISD::SINT_TO_FP:
2969 CastOpc = ISD::SIGN_EXTEND;
2970 Opc = ISD::SINT_TO_FP;
2971 break;
2972 case ISD::UINT_TO_FP:
2973 CastOpc = ISD::ZERO_EXTEND;
2974 Opc = ISD::UINT_TO_FP;
2975 break;
2976 }
2977
2978 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2979 return DAG.getNode(Opc, dl, VT, Op);
2980}
2981
Bob Wilson76a312b2010-03-19 22:51:32 +00002982static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2983 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002984 if (VT.isVector())
2985 return LowerVectorINT_TO_FP(Op, DAG);
2986
Bob Wilson76a312b2010-03-19 22:51:32 +00002987 DebugLoc dl = Op.getDebugLoc();
2988 unsigned Opc;
2989
2990 switch (Op.getOpcode()) {
2991 default:
2992 assert(0 && "Invalid opcode!");
2993 case ISD::SINT_TO_FP:
2994 Opc = ARMISD::SITOF;
2995 break;
2996 case ISD::UINT_TO_FP:
2997 Opc = ARMISD::UITOF;
2998 break;
2999 }
3000
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003001 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003002 return DAG.getNode(Opc, dl, VT, Op);
3003}
3004
Evan Cheng515fe3a2010-07-08 02:08:50 +00003005SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003006 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003007 SDValue Tmp0 = Op.getOperand(0);
3008 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003009 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003010 EVT VT = Op.getValueType();
3011 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003012 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3013 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3014 bool UseNEON = !InGPR && Subtarget->hasNEON();
3015
3016 if (UseNEON) {
3017 // Use VBSL to copy the sign bit.
3018 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3019 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3020 DAG.getTargetConstant(EncodedVal, MVT::i32));
3021 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3022 if (VT == MVT::f64)
3023 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3024 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3025 DAG.getConstant(32, MVT::i32));
3026 else /*if (VT == MVT::f32)*/
3027 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3028 if (SrcVT == MVT::f32) {
3029 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3030 if (VT == MVT::f64)
3031 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3032 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3033 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003034 } else if (VT == MVT::f32)
3035 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3036 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3037 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003038 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3039 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3040
3041 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3042 MVT::i32);
3043 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3044 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3045 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003046
Evan Chenge573fb32011-02-23 02:24:55 +00003047 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3048 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3049 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003050 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003051 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3052 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3053 DAG.getConstant(0, MVT::i32));
3054 } else {
3055 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3056 }
3057
3058 return Res;
3059 }
Evan Chengc143dd42011-02-11 02:28:55 +00003060
3061 // Bitcast operand 1 to i32.
3062 if (SrcVT == MVT::f64)
3063 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3064 &Tmp1, 1).getValue(1);
3065 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3066
Evan Chenge573fb32011-02-23 02:24:55 +00003067 // Or in the signbit with integer operations.
3068 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3069 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3070 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3071 if (VT == MVT::f32) {
3072 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3073 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3074 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3075 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003076 }
3077
Evan Chenge573fb32011-02-23 02:24:55 +00003078 // f64: Or the high part with signbit and then combine two parts.
3079 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3080 &Tmp0, 1);
3081 SDValue Lo = Tmp0.getValue(0);
3082 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3083 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3084 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003085}
3086
Evan Cheng2457f2c2010-05-22 01:47:14 +00003087SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3088 MachineFunction &MF = DAG.getMachineFunction();
3089 MachineFrameInfo *MFI = MF.getFrameInfo();
3090 MFI->setReturnAddressIsTaken(true);
3091
3092 EVT VT = Op.getValueType();
3093 DebugLoc dl = Op.getDebugLoc();
3094 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3095 if (Depth) {
3096 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3097 SDValue Offset = DAG.getConstant(4, MVT::i32);
3098 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3099 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003100 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003101 }
3102
3103 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003104 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003105 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3106}
3107
Dan Gohmand858e902010-04-17 15:26:15 +00003108SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003109 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3110 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003111
Owen Andersone50ed302009-08-10 22:56:29 +00003112 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003113 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3114 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003115 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003116 ? ARM::R7 : ARM::R11;
3117 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3118 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003119 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3120 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003121 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003122 return FrameAddr;
3123}
3124
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003125/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003126/// expand a bit convert where either the source or destination type is i64 to
3127/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3128/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3129/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003130static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003131 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3132 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003133 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003134
Bob Wilson9f3f0612010-04-17 05:30:19 +00003135 // This function is only supposed to be called for i64 types, either as the
3136 // source or destination of the bit convert.
3137 EVT SrcVT = Op.getValueType();
3138 EVT DstVT = N->getValueType(0);
3139 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003140 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003141
Bob Wilson9f3f0612010-04-17 05:30:19 +00003142 // Turn i64->f64 into VMOVDRR.
3143 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3145 DAG.getConstant(0, MVT::i32));
3146 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3147 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003148 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003149 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003150 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003151
Jim Grosbache5165492009-11-09 00:11:35 +00003152 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003153 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3154 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3155 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3156 // Merge the pieces into a single i64 value.
3157 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3158 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003159
Bob Wilson9f3f0612010-04-17 05:30:19 +00003160 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003161}
3162
Bob Wilson5bafff32009-06-22 23:27:02 +00003163/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003164/// Zero vectors are used to represent vector negation and in those cases
3165/// will be implemented with the NEON VNEG instruction. However, VNEG does
3166/// not support i64 elements, so sometimes the zero vectors will need to be
3167/// explicitly constructed. Regardless, use a canonical VMOV to create the
3168/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003169static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003170 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003171 // The canonical modified immediate encoding of a zero vector is....0!
3172 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3173 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3174 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003175 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003176}
3177
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003178/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3179/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003180SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3181 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003182 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3183 EVT VT = Op.getValueType();
3184 unsigned VTBits = VT.getSizeInBits();
3185 DebugLoc dl = Op.getDebugLoc();
3186 SDValue ShOpLo = Op.getOperand(0);
3187 SDValue ShOpHi = Op.getOperand(1);
3188 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003189 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003190 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003191
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003192 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3193
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003194 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3195 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3196 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3197 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3198 DAG.getConstant(VTBits, MVT::i32));
3199 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3200 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003201 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003202
3203 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3204 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003205 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003206 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003207 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003208 CCR, Cmp);
3209
3210 SDValue Ops[2] = { Lo, Hi };
3211 return DAG.getMergeValues(Ops, 2, dl);
3212}
3213
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003214/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3215/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003216SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3217 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003218 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3219 EVT VT = Op.getValueType();
3220 unsigned VTBits = VT.getSizeInBits();
3221 DebugLoc dl = Op.getDebugLoc();
3222 SDValue ShOpLo = Op.getOperand(0);
3223 SDValue ShOpHi = Op.getOperand(1);
3224 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003225 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003226
3227 assert(Op.getOpcode() == ISD::SHL_PARTS);
3228 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3229 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3230 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3231 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3232 DAG.getConstant(VTBits, MVT::i32));
3233 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3234 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3235
3236 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3237 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3238 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003239 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003240 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003241 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003242 CCR, Cmp);
3243
3244 SDValue Ops[2] = { Lo, Hi };
3245 return DAG.getMergeValues(Ops, 2, dl);
3246}
3247
Jim Grosbach4725ca72010-09-08 03:54:02 +00003248SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003249 SelectionDAG &DAG) const {
3250 // The rounding mode is in bits 23:22 of the FPSCR.
3251 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3252 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3253 // so that the shift + and get folded into a bitfield extract.
3254 DebugLoc dl = Op.getDebugLoc();
3255 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3256 DAG.getConstant(Intrinsic::arm_get_fpscr,
3257 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003258 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003259 DAG.getConstant(1U << 22, MVT::i32));
3260 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3261 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003262 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003263 DAG.getConstant(3, MVT::i32));
3264}
3265
Jim Grosbach3482c802010-01-18 19:58:49 +00003266static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3267 const ARMSubtarget *ST) {
3268 EVT VT = N->getValueType(0);
3269 DebugLoc dl = N->getDebugLoc();
3270
3271 if (!ST->hasV6T2Ops())
3272 return SDValue();
3273
3274 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3275 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3276}
3277
Bob Wilson5bafff32009-06-22 23:27:02 +00003278static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3279 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003280 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003281 DebugLoc dl = N->getDebugLoc();
3282
Bob Wilsond5448bb2010-11-18 21:16:28 +00003283 if (!VT.isVector())
3284 return SDValue();
3285
Bob Wilson5bafff32009-06-22 23:27:02 +00003286 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003287 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003288
Bob Wilsond5448bb2010-11-18 21:16:28 +00003289 // Left shifts translate directly to the vshiftu intrinsic.
3290 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003291 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003292 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3293 N->getOperand(0), N->getOperand(1));
3294
3295 assert((N->getOpcode() == ISD::SRA ||
3296 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3297
3298 // NEON uses the same intrinsics for both left and right shifts. For
3299 // right shifts, the shift amounts are negative, so negate the vector of
3300 // shift amounts.
3301 EVT ShiftVT = N->getOperand(1).getValueType();
3302 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3303 getZeroVector(ShiftVT, DAG, dl),
3304 N->getOperand(1));
3305 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3306 Intrinsic::arm_neon_vshifts :
3307 Intrinsic::arm_neon_vshiftu);
3308 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3309 DAG.getConstant(vshiftInt, MVT::i32),
3310 N->getOperand(0), NegatedCount);
3311}
3312
3313static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3314 const ARMSubtarget *ST) {
3315 EVT VT = N->getValueType(0);
3316 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003317
Eli Friedmance392eb2009-08-22 03:13:10 +00003318 // We can get here for a node like i32 = ISD::SHL i32, i64
3319 if (VT != MVT::i64)
3320 return SDValue();
3321
3322 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003323 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003324
Chris Lattner27a6c732007-11-24 07:07:01 +00003325 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3326 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003327 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003328 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003329
Chris Lattner27a6c732007-11-24 07:07:01 +00003330 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003331 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003332
Chris Lattner27a6c732007-11-24 07:07:01 +00003333 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003335 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003337 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003338
Chris Lattner27a6c732007-11-24 07:07:01 +00003339 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3340 // captures the result into a carry flag.
3341 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003342 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003343
Chris Lattner27a6c732007-11-24 07:07:01 +00003344 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003346
Chris Lattner27a6c732007-11-24 07:07:01 +00003347 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003349}
3350
Bob Wilson5bafff32009-06-22 23:27:02 +00003351static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3352 SDValue TmpOp0, TmpOp1;
3353 bool Invert = false;
3354 bool Swap = false;
3355 unsigned Opc = 0;
3356
3357 SDValue Op0 = Op.getOperand(0);
3358 SDValue Op1 = Op.getOperand(1);
3359 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003360 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003361 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3362 DebugLoc dl = Op.getDebugLoc();
3363
3364 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3365 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003366 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003367 case ISD::SETUNE:
3368 case ISD::SETNE: Invert = true; // Fallthrough
3369 case ISD::SETOEQ:
3370 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3371 case ISD::SETOLT:
3372 case ISD::SETLT: Swap = true; // Fallthrough
3373 case ISD::SETOGT:
3374 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3375 case ISD::SETOLE:
3376 case ISD::SETLE: Swap = true; // Fallthrough
3377 case ISD::SETOGE:
3378 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3379 case ISD::SETUGE: Swap = true; // Fallthrough
3380 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3381 case ISD::SETUGT: Swap = true; // Fallthrough
3382 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3383 case ISD::SETUEQ: Invert = true; // Fallthrough
3384 case ISD::SETONE:
3385 // Expand this to (OLT | OGT).
3386 TmpOp0 = Op0;
3387 TmpOp1 = Op1;
3388 Opc = ISD::OR;
3389 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3390 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3391 break;
3392 case ISD::SETUO: Invert = true; // Fallthrough
3393 case ISD::SETO:
3394 // Expand this to (OLT | OGE).
3395 TmpOp0 = Op0;
3396 TmpOp1 = Op1;
3397 Opc = ISD::OR;
3398 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3399 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3400 break;
3401 }
3402 } else {
3403 // Integer comparisons.
3404 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003405 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003406 case ISD::SETNE: Invert = true;
3407 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3408 case ISD::SETLT: Swap = true;
3409 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3410 case ISD::SETLE: Swap = true;
3411 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3412 case ISD::SETULT: Swap = true;
3413 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3414 case ISD::SETULE: Swap = true;
3415 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3416 }
3417
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003418 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003419 if (Opc == ARMISD::VCEQ) {
3420
3421 SDValue AndOp;
3422 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3423 AndOp = Op0;
3424 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3425 AndOp = Op1;
3426
3427 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003428 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003429 AndOp = AndOp.getOperand(0);
3430
3431 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3432 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003433 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3434 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003435 Invert = !Invert;
3436 }
3437 }
3438 }
3439
3440 if (Swap)
3441 std::swap(Op0, Op1);
3442
Owen Andersonc24cb352010-11-08 23:21:22 +00003443 // If one of the operands is a constant vector zero, attempt to fold the
3444 // comparison to a specialized compare-against-zero form.
3445 SDValue SingleOp;
3446 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3447 SingleOp = Op0;
3448 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3449 if (Opc == ARMISD::VCGE)
3450 Opc = ARMISD::VCLEZ;
3451 else if (Opc == ARMISD::VCGT)
3452 Opc = ARMISD::VCLTZ;
3453 SingleOp = Op1;
3454 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003455
Owen Andersonc24cb352010-11-08 23:21:22 +00003456 SDValue Result;
3457 if (SingleOp.getNode()) {
3458 switch (Opc) {
3459 case ARMISD::VCEQ:
3460 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3461 case ARMISD::VCGE:
3462 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3463 case ARMISD::VCLEZ:
3464 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3465 case ARMISD::VCGT:
3466 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3467 case ARMISD::VCLTZ:
3468 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3469 default:
3470 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3471 }
3472 } else {
3473 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3474 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003475
3476 if (Invert)
3477 Result = DAG.getNOT(dl, Result, VT);
3478
3479 return Result;
3480}
3481
Bob Wilsond3c42842010-06-14 22:19:57 +00003482/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3483/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003484/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003485static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3486 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003487 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003488 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003489
Bob Wilson827b2102010-06-15 19:05:35 +00003490 // SplatBitSize is set to the smallest size that splats the vector, so a
3491 // zero vector will always have SplatBitSize == 8. However, NEON modified
3492 // immediate instructions others than VMOV do not support the 8-bit encoding
3493 // of a zero vector, and the default encoding of zero is supposed to be the
3494 // 32-bit version.
3495 if (SplatBits == 0)
3496 SplatBitSize = 32;
3497
Bob Wilson5bafff32009-06-22 23:27:02 +00003498 switch (SplatBitSize) {
3499 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003500 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003501 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003502 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003503 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003504 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003505 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003506 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003507 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003508
3509 case 16:
3510 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003511 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003512 if ((SplatBits & ~0xff) == 0) {
3513 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003514 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003515 Imm = SplatBits;
3516 break;
3517 }
3518 if ((SplatBits & ~0xff00) == 0) {
3519 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003520 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003521 Imm = SplatBits >> 8;
3522 break;
3523 }
3524 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003525
3526 case 32:
3527 // NEON's 32-bit VMOV supports splat values where:
3528 // * only one byte is nonzero, or
3529 // * the least significant byte is 0xff and the second byte is nonzero, or
3530 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003531 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003532 if ((SplatBits & ~0xff) == 0) {
3533 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003534 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003535 Imm = SplatBits;
3536 break;
3537 }
3538 if ((SplatBits & ~0xff00) == 0) {
3539 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003540 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003541 Imm = SplatBits >> 8;
3542 break;
3543 }
3544 if ((SplatBits & ~0xff0000) == 0) {
3545 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003546 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003547 Imm = SplatBits >> 16;
3548 break;
3549 }
3550 if ((SplatBits & ~0xff000000) == 0) {
3551 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003552 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003553 Imm = SplatBits >> 24;
3554 break;
3555 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003556
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003557 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3558 if (type == OtherModImm) return SDValue();
3559
Bob Wilson5bafff32009-06-22 23:27:02 +00003560 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003561 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3562 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003563 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003564 Imm = SplatBits >> 8;
3565 SplatBits |= 0xff;
3566 break;
3567 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003568
3569 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003570 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3571 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003572 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003573 Imm = SplatBits >> 16;
3574 SplatBits |= 0xffff;
3575 break;
3576 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003577
3578 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3579 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3580 // VMOV.I32. A (very) minor optimization would be to replicate the value
3581 // and fall through here to test for a valid 64-bit splat. But, then the
3582 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003583 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003584
3585 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003586 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003587 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003588 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003589 uint64_t BitMask = 0xff;
3590 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003591 unsigned ImmMask = 1;
3592 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003593 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003594 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003595 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003596 Imm |= ImmMask;
3597 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003598 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003599 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003600 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003601 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003602 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003603 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003604 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003605 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003606 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003607 break;
3608 }
3609
Bob Wilson1a913ed2010-06-11 21:34:50 +00003610 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003611 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 return SDValue();
3613 }
3614
Bob Wilsoncba270d2010-07-13 21:16:48 +00003615 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3616 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003617}
3618
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003619static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3620 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003621 unsigned NumElts = VT.getVectorNumElements();
3622 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003623
3624 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3625 if (M[0] < 0)
3626 return false;
3627
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003628 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003629
3630 // If this is a VEXT shuffle, the immediate value is the index of the first
3631 // element. The other shuffle indices must be the successive elements after
3632 // the first one.
3633 unsigned ExpectedElt = Imm;
3634 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003635 // Increment the expected index. If it wraps around, it may still be
3636 // a VEXT but the source vectors must be swapped.
3637 ExpectedElt += 1;
3638 if (ExpectedElt == NumElts * 2) {
3639 ExpectedElt = 0;
3640 ReverseVEXT = true;
3641 }
3642
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003643 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003644 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003645 return false;
3646 }
3647
3648 // Adjust the index value if the source operands will be swapped.
3649 if (ReverseVEXT)
3650 Imm -= NumElts;
3651
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003652 return true;
3653}
3654
Bob Wilson8bb9e482009-07-26 00:39:34 +00003655/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3656/// instruction with the specified blocksize. (The order of the elements
3657/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003658static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3659 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003660 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3661 "Only possible block sizes for VREV are: 16, 32, 64");
3662
Bob Wilson8bb9e482009-07-26 00:39:34 +00003663 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003664 if (EltSz == 64)
3665 return false;
3666
3667 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003668 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003669 // If the first shuffle index is UNDEF, be optimistic.
3670 if (M[0] < 0)
3671 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003672
3673 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3674 return false;
3675
3676 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003677 if (M[i] < 0) continue; // ignore UNDEF indices
3678 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003679 return false;
3680 }
3681
3682 return true;
3683}
3684
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003685static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3686 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3687 // range, then 0 is placed into the resulting vector. So pretty much any mask
3688 // of 8 elements can work here.
3689 return VT == MVT::v8i8 && M.size() == 8;
3690}
3691
Bob Wilsonc692cb72009-08-21 20:54:19 +00003692static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3693 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003694 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3695 if (EltSz == 64)
3696 return false;
3697
Bob Wilsonc692cb72009-08-21 20:54:19 +00003698 unsigned NumElts = VT.getVectorNumElements();
3699 WhichResult = (M[0] == 0 ? 0 : 1);
3700 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003701 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3702 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003703 return false;
3704 }
3705 return true;
3706}
3707
Bob Wilson324f4f12009-12-03 06:40:55 +00003708/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3709/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3710/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3711static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3712 unsigned &WhichResult) {
3713 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3714 if (EltSz == 64)
3715 return false;
3716
3717 unsigned NumElts = VT.getVectorNumElements();
3718 WhichResult = (M[0] == 0 ? 0 : 1);
3719 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003720 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3721 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003722 return false;
3723 }
3724 return true;
3725}
3726
Bob Wilsonc692cb72009-08-21 20:54:19 +00003727static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3728 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003729 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3730 if (EltSz == 64)
3731 return false;
3732
Bob Wilsonc692cb72009-08-21 20:54:19 +00003733 unsigned NumElts = VT.getVectorNumElements();
3734 WhichResult = (M[0] == 0 ? 0 : 1);
3735 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003736 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003737 if ((unsigned) M[i] != 2 * i + WhichResult)
3738 return false;
3739 }
3740
3741 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003742 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003743 return false;
3744
3745 return true;
3746}
3747
Bob Wilson324f4f12009-12-03 06:40:55 +00003748/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3749/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3750/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3751static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3752 unsigned &WhichResult) {
3753 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3754 if (EltSz == 64)
3755 return false;
3756
3757 unsigned Half = VT.getVectorNumElements() / 2;
3758 WhichResult = (M[0] == 0 ? 0 : 1);
3759 for (unsigned j = 0; j != 2; ++j) {
3760 unsigned Idx = WhichResult;
3761 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003762 int MIdx = M[i + j * Half];
3763 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003764 return false;
3765 Idx += 2;
3766 }
3767 }
3768
3769 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3770 if (VT.is64BitVector() && EltSz == 32)
3771 return false;
3772
3773 return true;
3774}
3775
Bob Wilsonc692cb72009-08-21 20:54:19 +00003776static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3777 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003778 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3779 if (EltSz == 64)
3780 return false;
3781
Bob Wilsonc692cb72009-08-21 20:54:19 +00003782 unsigned NumElts = VT.getVectorNumElements();
3783 WhichResult = (M[0] == 0 ? 0 : 1);
3784 unsigned Idx = WhichResult * NumElts / 2;
3785 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003786 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3787 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003788 return false;
3789 Idx += 1;
3790 }
3791
3792 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003793 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003794 return false;
3795
3796 return true;
3797}
3798
Bob Wilson324f4f12009-12-03 06:40:55 +00003799/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3800/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3801/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3802static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3803 unsigned &WhichResult) {
3804 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3805 if (EltSz == 64)
3806 return false;
3807
3808 unsigned NumElts = VT.getVectorNumElements();
3809 WhichResult = (M[0] == 0 ? 0 : 1);
3810 unsigned Idx = WhichResult * NumElts / 2;
3811 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003812 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3813 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003814 return false;
3815 Idx += 1;
3816 }
3817
3818 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3819 if (VT.is64BitVector() && EltSz == 32)
3820 return false;
3821
3822 return true;
3823}
3824
Dale Johannesenf630c712010-07-29 20:10:08 +00003825// If N is an integer constant that can be moved into a register in one
3826// instruction, return an SDValue of such a constant (will become a MOV
3827// instruction). Otherwise return null.
3828static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3829 const ARMSubtarget *ST, DebugLoc dl) {
3830 uint64_t Val;
3831 if (!isa<ConstantSDNode>(N))
3832 return SDValue();
3833 Val = cast<ConstantSDNode>(N)->getZExtValue();
3834
3835 if (ST->isThumb1Only()) {
3836 if (Val <= 255 || ~Val <= 255)
3837 return DAG.getConstant(Val, MVT::i32);
3838 } else {
3839 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3840 return DAG.getConstant(Val, MVT::i32);
3841 }
3842 return SDValue();
3843}
3844
Bob Wilson5bafff32009-06-22 23:27:02 +00003845// If this is a case we can't handle, return null and let the default
3846// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003847SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3848 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003849 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003850 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003851 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003852
3853 APInt SplatBits, SplatUndef;
3854 unsigned SplatBitSize;
3855 bool HasAnyUndefs;
3856 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003857 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003858 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003859 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003860 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003861 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003862 DAG, VmovVT, VT.is128BitVector(),
3863 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003864 if (Val.getNode()) {
3865 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003866 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003867 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003868
3869 // Try an immediate VMVN.
3870 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3871 ((1LL << SplatBitSize) - 1));
3872 Val = isNEONModifiedImm(NegatedImm,
3873 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003874 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003875 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003876 if (Val.getNode()) {
3877 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003878 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003879 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003880 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003881 }
3882
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003883 // Scan through the operands to see if only one value is used.
3884 unsigned NumElts = VT.getVectorNumElements();
3885 bool isOnlyLowElement = true;
3886 bool usesOnlyOneValue = true;
3887 bool isConstant = true;
3888 SDValue Value;
3889 for (unsigned i = 0; i < NumElts; ++i) {
3890 SDValue V = Op.getOperand(i);
3891 if (V.getOpcode() == ISD::UNDEF)
3892 continue;
3893 if (i > 0)
3894 isOnlyLowElement = false;
3895 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3896 isConstant = false;
3897
3898 if (!Value.getNode())
3899 Value = V;
3900 else if (V != Value)
3901 usesOnlyOneValue = false;
3902 }
3903
3904 if (!Value.getNode())
3905 return DAG.getUNDEF(VT);
3906
3907 if (isOnlyLowElement)
3908 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3909
Dale Johannesenf630c712010-07-29 20:10:08 +00003910 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3911
Dale Johannesen575cd142010-10-19 20:00:17 +00003912 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3913 // i32 and try again.
3914 if (usesOnlyOneValue && EltSize <= 32) {
3915 if (!isConstant)
3916 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3917 if (VT.getVectorElementType().isFloatingPoint()) {
3918 SmallVector<SDValue, 8> Ops;
3919 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003920 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003921 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003922 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3923 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003924 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3925 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003926 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003927 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003928 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3929 if (Val.getNode())
3930 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003931 }
3932
3933 // If all elements are constants and the case above didn't get hit, fall back
3934 // to the default expansion, which will generate a load from the constant
3935 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003936 if (isConstant)
3937 return SDValue();
3938
Bob Wilson11a1dff2011-01-07 21:37:30 +00003939 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3940 if (NumElts >= 4) {
3941 SDValue shuffle = ReconstructShuffle(Op, DAG);
3942 if (shuffle != SDValue())
3943 return shuffle;
3944 }
3945
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003946 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003947 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3948 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003949 if (EltSize >= 32) {
3950 // Do the expansion with floating-point types, since that is what the VFP
3951 // registers are defined to use, and since i64 is not legal.
3952 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3953 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003954 SmallVector<SDValue, 8> Ops;
3955 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003956 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003957 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003958 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003959 }
3960
3961 return SDValue();
3962}
3963
Bob Wilson11a1dff2011-01-07 21:37:30 +00003964// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003965// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003966SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3967 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003968 DebugLoc dl = Op.getDebugLoc();
3969 EVT VT = Op.getValueType();
3970 unsigned NumElts = VT.getVectorNumElements();
3971
3972 SmallVector<SDValue, 2> SourceVecs;
3973 SmallVector<unsigned, 2> MinElts;
3974 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003975
Bob Wilson11a1dff2011-01-07 21:37:30 +00003976 for (unsigned i = 0; i < NumElts; ++i) {
3977 SDValue V = Op.getOperand(i);
3978 if (V.getOpcode() == ISD::UNDEF)
3979 continue;
3980 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3981 // A shuffle can only come from building a vector from various
3982 // elements of other vectors.
3983 return SDValue();
3984 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003985
Bob Wilson11a1dff2011-01-07 21:37:30 +00003986 // Record this extraction against the appropriate vector if possible...
3987 SDValue SourceVec = V.getOperand(0);
3988 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3989 bool FoundSource = false;
3990 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3991 if (SourceVecs[j] == SourceVec) {
3992 if (MinElts[j] > EltNo)
3993 MinElts[j] = EltNo;
3994 if (MaxElts[j] < EltNo)
3995 MaxElts[j] = EltNo;
3996 FoundSource = true;
3997 break;
3998 }
3999 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004000
Bob Wilson11a1dff2011-01-07 21:37:30 +00004001 // Or record a new source if not...
4002 if (!FoundSource) {
4003 SourceVecs.push_back(SourceVec);
4004 MinElts.push_back(EltNo);
4005 MaxElts.push_back(EltNo);
4006 }
4007 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004008
Bob Wilson11a1dff2011-01-07 21:37:30 +00004009 // Currently only do something sane when at most two source vectors
4010 // involved.
4011 if (SourceVecs.size() > 2)
4012 return SDValue();
4013
4014 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4015 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004016
Bob Wilson11a1dff2011-01-07 21:37:30 +00004017 // This loop extracts the usage patterns of the source vectors
4018 // and prepares appropriate SDValues for a shuffle if possible.
4019 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4020 if (SourceVecs[i].getValueType() == VT) {
4021 // No VEXT necessary
4022 ShuffleSrcs[i] = SourceVecs[i];
4023 VEXTOffsets[i] = 0;
4024 continue;
4025 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4026 // It probably isn't worth padding out a smaller vector just to
4027 // break it down again in a shuffle.
4028 return SDValue();
4029 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004030
Bob Wilson11a1dff2011-01-07 21:37:30 +00004031 // Since only 64-bit and 128-bit vectors are legal on ARM and
4032 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004033 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4034 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004035
Bob Wilson11a1dff2011-01-07 21:37:30 +00004036 if (MaxElts[i] - MinElts[i] >= NumElts) {
4037 // Span too large for a VEXT to cope
4038 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004039 }
4040
Bob Wilson11a1dff2011-01-07 21:37:30 +00004041 if (MinElts[i] >= NumElts) {
4042 // The extraction can just take the second half
4043 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004044 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4045 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004046 DAG.getIntPtrConstant(NumElts));
4047 } else if (MaxElts[i] < NumElts) {
4048 // The extraction can just take the first half
4049 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004050 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4051 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004052 DAG.getIntPtrConstant(0));
4053 } else {
4054 // An actual VEXT is needed
4055 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004056 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4057 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004058 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004059 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4060 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004061 DAG.getIntPtrConstant(NumElts));
4062 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4063 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4064 }
4065 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004066
Bob Wilson11a1dff2011-01-07 21:37:30 +00004067 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004068
Bob Wilson11a1dff2011-01-07 21:37:30 +00004069 for (unsigned i = 0; i < NumElts; ++i) {
4070 SDValue Entry = Op.getOperand(i);
4071 if (Entry.getOpcode() == ISD::UNDEF) {
4072 Mask.push_back(-1);
4073 continue;
4074 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004075
Bob Wilson11a1dff2011-01-07 21:37:30 +00004076 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004077 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4078 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004079 if (ExtractVec == SourceVecs[0]) {
4080 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4081 } else {
4082 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4083 }
4084 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004085
Bob Wilson11a1dff2011-01-07 21:37:30 +00004086 // Final check before we try to produce nonsense...
4087 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004088 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4089 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004090
Bob Wilson11a1dff2011-01-07 21:37:30 +00004091 return SDValue();
4092}
4093
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004094/// isShuffleMaskLegal - Targets can use this to indicate that they only
4095/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4096/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4097/// are assumed to be legal.
4098bool
4099ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4100 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004101 if (VT.getVectorNumElements() == 4 &&
4102 (VT.is128BitVector() || VT.is64BitVector())) {
4103 unsigned PFIndexes[4];
4104 for (unsigned i = 0; i != 4; ++i) {
4105 if (M[i] < 0)
4106 PFIndexes[i] = 8;
4107 else
4108 PFIndexes[i] = M[i];
4109 }
4110
4111 // Compute the index in the perfect shuffle table.
4112 unsigned PFTableIndex =
4113 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4114 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4115 unsigned Cost = (PFEntry >> 30);
4116
4117 if (Cost <= 4)
4118 return true;
4119 }
4120
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004121 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004122 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004123
Bob Wilson53dd2452010-06-07 23:53:38 +00004124 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4125 return (EltSize >= 32 ||
4126 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004127 isVREVMask(M, VT, 64) ||
4128 isVREVMask(M, VT, 32) ||
4129 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004130 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004131 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004132 isVTRNMask(M, VT, WhichResult) ||
4133 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004134 isVZIPMask(M, VT, WhichResult) ||
4135 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4136 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4137 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004138}
4139
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004140/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4141/// the specified operations to build the shuffle.
4142static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4143 SDValue RHS, SelectionDAG &DAG,
4144 DebugLoc dl) {
4145 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4146 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4147 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4148
4149 enum {
4150 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4151 OP_VREV,
4152 OP_VDUP0,
4153 OP_VDUP1,
4154 OP_VDUP2,
4155 OP_VDUP3,
4156 OP_VEXT1,
4157 OP_VEXT2,
4158 OP_VEXT3,
4159 OP_VUZPL, // VUZP, left result
4160 OP_VUZPR, // VUZP, right result
4161 OP_VZIPL, // VZIP, left result
4162 OP_VZIPR, // VZIP, right result
4163 OP_VTRNL, // VTRN, left result
4164 OP_VTRNR // VTRN, right result
4165 };
4166
4167 if (OpNum == OP_COPY) {
4168 if (LHSID == (1*9+2)*9+3) return LHS;
4169 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4170 return RHS;
4171 }
4172
4173 SDValue OpLHS, OpRHS;
4174 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4175 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4176 EVT VT = OpLHS.getValueType();
4177
4178 switch (OpNum) {
4179 default: llvm_unreachable("Unknown shuffle opcode!");
4180 case OP_VREV:
4181 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4182 case OP_VDUP0:
4183 case OP_VDUP1:
4184 case OP_VDUP2:
4185 case OP_VDUP3:
4186 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004187 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004188 case OP_VEXT1:
4189 case OP_VEXT2:
4190 case OP_VEXT3:
4191 return DAG.getNode(ARMISD::VEXT, dl, VT,
4192 OpLHS, OpRHS,
4193 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4194 case OP_VUZPL:
4195 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004196 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004197 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4198 case OP_VZIPL:
4199 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004200 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004201 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4202 case OP_VTRNL:
4203 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004204 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4205 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004206 }
4207}
4208
Bill Wendling69a05a72011-03-14 23:02:38 +00004209static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4210 SmallVectorImpl<int> &ShuffleMask,
4211 SelectionDAG &DAG) {
4212 // Check to see if we can use the VTBL instruction.
4213 SDValue V1 = Op.getOperand(0);
4214 SDValue V2 = Op.getOperand(1);
4215 DebugLoc DL = Op.getDebugLoc();
4216
4217 SmallVector<SDValue, 8> VTBLMask;
4218 for (SmallVectorImpl<int>::iterator
4219 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4220 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4221
4222 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4223 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4224 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4225 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004226
Owen Anderson76706012011-04-05 21:48:57 +00004227 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004228 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4229 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004230}
4231
Bob Wilson5bafff32009-06-22 23:27:02 +00004232static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004233 SDValue V1 = Op.getOperand(0);
4234 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004235 DebugLoc dl = Op.getDebugLoc();
4236 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004237 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004238 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004239
Bob Wilson28865062009-08-13 02:13:04 +00004240 // Convert shuffles that are directly supported on NEON to target-specific
4241 // DAG nodes, instead of keeping them as shuffles and matching them again
4242 // during code selection. This is more efficient and avoids the possibility
4243 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004244 // FIXME: floating-point vectors should be canonicalized to integer vectors
4245 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004246 SVN->getMask(ShuffleMask);
4247
Bob Wilson53dd2452010-06-07 23:53:38 +00004248 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4249 if (EltSize <= 32) {
4250 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4251 int Lane = SVN->getSplatIndex();
4252 // If this is undef splat, generate it via "just" vdup, if possible.
4253 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004254
Bob Wilson53dd2452010-06-07 23:53:38 +00004255 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4256 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4257 }
4258 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4259 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004260 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004261
4262 bool ReverseVEXT;
4263 unsigned Imm;
4264 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4265 if (ReverseVEXT)
4266 std::swap(V1, V2);
4267 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4268 DAG.getConstant(Imm, MVT::i32));
4269 }
4270
4271 if (isVREVMask(ShuffleMask, VT, 64))
4272 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4273 if (isVREVMask(ShuffleMask, VT, 32))
4274 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4275 if (isVREVMask(ShuffleMask, VT, 16))
4276 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4277
4278 // Check for Neon shuffles that modify both input vectors in place.
4279 // If both results are used, i.e., if there are two shuffles with the same
4280 // source operands and with masks corresponding to both results of one of
4281 // these operations, DAG memoization will ensure that a single node is
4282 // used for both shuffles.
4283 unsigned WhichResult;
4284 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4285 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4286 V1, V2).getValue(WhichResult);
4287 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4288 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4289 V1, V2).getValue(WhichResult);
4290 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4291 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4292 V1, V2).getValue(WhichResult);
4293
4294 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4295 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4296 V1, V1).getValue(WhichResult);
4297 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4298 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4299 V1, V1).getValue(WhichResult);
4300 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4301 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4302 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004303 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004304
Bob Wilsonc692cb72009-08-21 20:54:19 +00004305 // If the shuffle is not directly supported and it has 4 elements, use
4306 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004307 unsigned NumElts = VT.getVectorNumElements();
4308 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004309 unsigned PFIndexes[4];
4310 for (unsigned i = 0; i != 4; ++i) {
4311 if (ShuffleMask[i] < 0)
4312 PFIndexes[i] = 8;
4313 else
4314 PFIndexes[i] = ShuffleMask[i];
4315 }
4316
4317 // Compute the index in the perfect shuffle table.
4318 unsigned PFTableIndex =
4319 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004320 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4321 unsigned Cost = (PFEntry >> 30);
4322
4323 if (Cost <= 4)
4324 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4325 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004326
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004327 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004328 if (EltSize >= 32) {
4329 // Do the expansion with floating-point types, since that is what the VFP
4330 // registers are defined to use, and since i64 is not legal.
4331 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4332 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004333 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4334 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004335 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004336 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004337 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004338 Ops.push_back(DAG.getUNDEF(EltVT));
4339 else
4340 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4341 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4342 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4343 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004344 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004345 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004346 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004347 }
4348
Bill Wendling69a05a72011-03-14 23:02:38 +00004349 if (VT == MVT::v8i8) {
4350 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4351 if (NewOp.getNode())
4352 return NewOp;
4353 }
4354
Bob Wilson22cac0d2009-08-14 05:16:33 +00004355 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004356}
4357
Bob Wilson5bafff32009-06-22 23:27:02 +00004358static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004359 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004360 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004361 if (!isa<ConstantSDNode>(Lane))
4362 return SDValue();
4363
4364 SDValue Vec = Op.getOperand(0);
4365 if (Op.getValueType() == MVT::i32 &&
4366 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4367 DebugLoc dl = Op.getDebugLoc();
4368 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4369 }
4370
4371 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004372}
4373
Bob Wilsona6d65862009-08-03 20:36:38 +00004374static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4375 // The only time a CONCAT_VECTORS operation can have legal types is when
4376 // two 64-bit vectors are concatenated to a 128-bit vector.
4377 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4378 "unexpected CONCAT_VECTORS");
4379 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004380 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004381 SDValue Op0 = Op.getOperand(0);
4382 SDValue Op1 = Op.getOperand(1);
4383 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004384 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004385 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004386 DAG.getIntPtrConstant(0));
4387 if (Op1.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, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004390 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004391 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004392}
4393
Bob Wilson626613d2010-11-23 19:38:38 +00004394/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4395/// element has been zero/sign-extended, depending on the isSigned parameter,
4396/// from an integer type half its size.
4397static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4398 bool isSigned) {
4399 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4400 EVT VT = N->getValueType(0);
4401 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4402 SDNode *BVN = N->getOperand(0).getNode();
4403 if (BVN->getValueType(0) != MVT::v4i32 ||
4404 BVN->getOpcode() != ISD::BUILD_VECTOR)
4405 return false;
4406 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4407 unsigned HiElt = 1 - LoElt;
4408 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4409 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4410 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4411 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4412 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4413 return false;
4414 if (isSigned) {
4415 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4416 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4417 return true;
4418 } else {
4419 if (Hi0->isNullValue() && Hi1->isNullValue())
4420 return true;
4421 }
4422 return false;
4423 }
4424
4425 if (N->getOpcode() != ISD::BUILD_VECTOR)
4426 return false;
4427
4428 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4429 SDNode *Elt = N->getOperand(i).getNode();
4430 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4431 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4432 unsigned HalfSize = EltSize / 2;
4433 if (isSigned) {
4434 int64_t SExtVal = C->getSExtValue();
4435 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4436 return false;
4437 } else {
4438 if ((C->getZExtValue() >> HalfSize) != 0)
4439 return false;
4440 }
4441 continue;
4442 }
4443 return false;
4444 }
4445
4446 return true;
4447}
4448
4449/// isSignExtended - Check if a node is a vector value that is sign-extended
4450/// or a constant BUILD_VECTOR with sign-extended elements.
4451static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4452 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4453 return true;
4454 if (isExtendedBUILD_VECTOR(N, DAG, true))
4455 return true;
4456 return false;
4457}
4458
4459/// isZeroExtended - Check if a node is a vector value that is zero-extended
4460/// or a constant BUILD_VECTOR with zero-extended elements.
4461static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4462 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4463 return true;
4464 if (isExtendedBUILD_VECTOR(N, DAG, false))
4465 return true;
4466 return false;
4467}
4468
4469/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4470/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004471static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4472 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4473 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004474 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4475 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4476 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4477 LD->isNonTemporal(), LD->getAlignment());
4478 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4479 // have been legalized as a BITCAST from v4i32.
4480 if (N->getOpcode() == ISD::BITCAST) {
4481 SDNode *BVN = N->getOperand(0).getNode();
4482 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4483 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4484 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4485 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4486 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4487 }
4488 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4489 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4490 EVT VT = N->getValueType(0);
4491 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4492 unsigned NumElts = VT.getVectorNumElements();
4493 MVT TruncVT = MVT::getIntegerVT(EltSize);
4494 SmallVector<SDValue, 8> Ops;
4495 for (unsigned i = 0; i != NumElts; ++i) {
4496 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4497 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004498 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004499 }
4500 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4501 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004502}
4503
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004504static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4505 unsigned Opcode = N->getOpcode();
4506 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4507 SDNode *N0 = N->getOperand(0).getNode();
4508 SDNode *N1 = N->getOperand(1).getNode();
4509 return N0->hasOneUse() && N1->hasOneUse() &&
4510 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4511 }
4512 return false;
4513}
4514
4515static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4516 unsigned Opcode = N->getOpcode();
4517 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4518 SDNode *N0 = N->getOperand(0).getNode();
4519 SDNode *N1 = N->getOperand(1).getNode();
4520 return N0->hasOneUse() && N1->hasOneUse() &&
4521 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4522 }
4523 return false;
4524}
4525
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004526static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4527 // Multiplications are only custom-lowered for 128-bit vectors so that
4528 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4529 EVT VT = Op.getValueType();
4530 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4531 SDNode *N0 = Op.getOperand(0).getNode();
4532 SDNode *N1 = Op.getOperand(1).getNode();
4533 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004534 bool isMLA = false;
4535 bool isN0SExt = isSignExtended(N0, DAG);
4536 bool isN1SExt = isSignExtended(N1, DAG);
4537 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004538 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004539 else {
4540 bool isN0ZExt = isZeroExtended(N0, DAG);
4541 bool isN1ZExt = isZeroExtended(N1, DAG);
4542 if (isN0ZExt && isN1ZExt)
4543 NewOpc = ARMISD::VMULLu;
4544 else if (isN1SExt || isN1ZExt) {
4545 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4546 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4547 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4548 NewOpc = ARMISD::VMULLs;
4549 isMLA = true;
4550 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4551 NewOpc = ARMISD::VMULLu;
4552 isMLA = true;
4553 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4554 std::swap(N0, N1);
4555 NewOpc = ARMISD::VMULLu;
4556 isMLA = true;
4557 }
4558 }
4559
4560 if (!NewOpc) {
4561 if (VT == MVT::v2i64)
4562 // Fall through to expand this. It is not legal.
4563 return SDValue();
4564 else
4565 // Other vector multiplications are legal.
4566 return Op;
4567 }
4568 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004569
4570 // Legalize to a VMULL instruction.
4571 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004572 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004573 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004574 if (!isMLA) {
4575 Op0 = SkipExtension(N0, DAG);
4576 assert(Op0.getValueType().is64BitVector() &&
4577 Op1.getValueType().is64BitVector() &&
4578 "unexpected types for extended operands to VMULL");
4579 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4580 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004581
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004582 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4583 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4584 // vmull q0, d4, d6
4585 // vmlal q0, d5, d6
4586 // is faster than
4587 // vaddl q0, d4, d5
4588 // vmovl q1, d6
4589 // vmul q0, q0, q1
4590 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4591 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4592 EVT Op1VT = Op1.getValueType();
4593 return DAG.getNode(N0->getOpcode(), DL, VT,
4594 DAG.getNode(NewOpc, DL, VT,
4595 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4596 DAG.getNode(NewOpc, DL, VT,
4597 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004598}
4599
Owen Anderson76706012011-04-05 21:48:57 +00004600static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004601LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4602 // Convert to float
4603 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4604 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4605 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4606 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4607 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4608 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4609 // Get reciprocal estimate.
4610 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004611 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004612 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4613 // Because char has a smaller range than uchar, we can actually get away
4614 // without any newton steps. This requires that we use a weird bias
4615 // of 0xb000, however (again, this has been exhaustively tested).
4616 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4617 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4618 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4619 Y = DAG.getConstant(0xb000, MVT::i32);
4620 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4621 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4622 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4623 // Convert back to short.
4624 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4625 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4626 return X;
4627}
4628
Owen Anderson76706012011-04-05 21:48:57 +00004629static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004630LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4631 SDValue N2;
4632 // Convert to float.
4633 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4634 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4635 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4636 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4637 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4638 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004639
Nate Begeman7973f352011-02-11 20:53:29 +00004640 // Use reciprocal estimate and one refinement step.
4641 // float4 recip = vrecpeq_f32(yf);
4642 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004643 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004644 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004645 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004646 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4647 N1, N2);
4648 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4649 // Because short has a smaller range than ushort, we can actually get away
4650 // with only a single newton step. This requires that we use a weird bias
4651 // of 89, however (again, this has been exhaustively tested).
4652 // float4 result = as_float4(as_int4(xf*recip) + 89);
4653 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4654 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4655 N1 = DAG.getConstant(89, MVT::i32);
4656 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4657 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4658 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4659 // Convert back to integer and return.
4660 // return vmovn_s32(vcvt_s32_f32(result));
4661 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4662 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4663 return N0;
4664}
4665
4666static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4667 EVT VT = Op.getValueType();
4668 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4669 "unexpected type for custom-lowering ISD::SDIV");
4670
4671 DebugLoc dl = Op.getDebugLoc();
4672 SDValue N0 = Op.getOperand(0);
4673 SDValue N1 = Op.getOperand(1);
4674 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004675
Nate Begeman7973f352011-02-11 20:53:29 +00004676 if (VT == MVT::v8i8) {
4677 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4678 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004679
Nate Begeman7973f352011-02-11 20:53:29 +00004680 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4681 DAG.getIntPtrConstant(4));
4682 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004683 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004684 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4685 DAG.getIntPtrConstant(0));
4686 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4687 DAG.getIntPtrConstant(0));
4688
4689 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4690 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4691
4692 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4693 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004694
Nate Begeman7973f352011-02-11 20:53:29 +00004695 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4696 return N0;
4697 }
4698 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4699}
4700
4701static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4702 EVT VT = Op.getValueType();
4703 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4704 "unexpected type for custom-lowering ISD::UDIV");
4705
4706 DebugLoc dl = Op.getDebugLoc();
4707 SDValue N0 = Op.getOperand(0);
4708 SDValue N1 = Op.getOperand(1);
4709 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004710
Nate Begeman7973f352011-02-11 20:53:29 +00004711 if (VT == MVT::v8i8) {
4712 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4713 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004714
Nate Begeman7973f352011-02-11 20:53:29 +00004715 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4716 DAG.getIntPtrConstant(4));
4717 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004718 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004719 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4720 DAG.getIntPtrConstant(0));
4721 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4722 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004723
Nate Begeman7973f352011-02-11 20:53:29 +00004724 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4725 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004726
Nate Begeman7973f352011-02-11 20:53:29 +00004727 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4728 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004729
4730 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004731 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4732 N0);
4733 return N0;
4734 }
Owen Anderson76706012011-04-05 21:48:57 +00004735
Nate Begeman7973f352011-02-11 20:53:29 +00004736 // v4i16 sdiv ... Convert to float.
4737 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4738 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4739 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4740 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4741 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4742 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4743
4744 // Use reciprocal estimate and two refinement steps.
4745 // float4 recip = vrecpeq_f32(yf);
4746 // recip *= vrecpsq_f32(yf, recip);
4747 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004748 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004749 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004750 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004751 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4752 N1, N2);
4753 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
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);
4758 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4759 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4760 // and that it will never cause us to return an answer too large).
4761 // float4 result = as_float4(as_int4(xf*recip) + 89);
4762 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4763 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4764 N1 = DAG.getConstant(2, MVT::i32);
4765 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4766 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4767 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4768 // Convert back to integer and return.
4769 // return vmovn_u32(vcvt_s32_f32(result));
4770 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4771 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4772 return N0;
4773}
4774
Dan Gohmand858e902010-04-17 15:26:15 +00004775SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004776 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004777 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004778 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004779 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004780 case ISD::GlobalAddress:
4781 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4782 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004783 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004784 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004785 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4786 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004787 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004788 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004789 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004790 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004791 case ISD::SINT_TO_FP:
4792 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4793 case ISD::FP_TO_SINT:
4794 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004795 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004796 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004797 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004798 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004799 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004800 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004801 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004802 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4803 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004804 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004805 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004806 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004807 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004808 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004809 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004810 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004811 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004812 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004813 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004814 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004815 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004816 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004817 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004818 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004819 case ISD::SDIV: return LowerSDIV(Op, DAG);
4820 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004821 }
Dan Gohman475871a2008-07-27 21:46:04 +00004822 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004823}
4824
Duncan Sands1607f052008-12-01 11:39:25 +00004825/// ReplaceNodeResults - Replace the results of node with an illegal result
4826/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004827void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4828 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004829 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004830 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004831 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004832 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004833 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004834 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004835 case ISD::BITCAST:
4836 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004837 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004838 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004839 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004840 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004841 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004842 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004843 if (Res.getNode())
4844 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004845}
Chris Lattner27a6c732007-11-24 07:07:01 +00004846
Evan Chenga8e29892007-01-19 07:51:42 +00004847//===----------------------------------------------------------------------===//
4848// ARM Scheduler Hooks
4849//===----------------------------------------------------------------------===//
4850
4851MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004852ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4853 MachineBasicBlock *BB,
4854 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004855 unsigned dest = MI->getOperand(0).getReg();
4856 unsigned ptr = MI->getOperand(1).getReg();
4857 unsigned oldval = MI->getOperand(2).getReg();
4858 unsigned newval = MI->getOperand(3).getReg();
4859 unsigned scratch = BB->getParent()->getRegInfo()
4860 .createVirtualRegister(ARM::GPRRegisterClass);
4861 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4862 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004863 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004864
4865 unsigned ldrOpc, strOpc;
4866 switch (Size) {
4867 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004868 case 1:
4869 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004870 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004871 break;
4872 case 2:
4873 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4874 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4875 break;
4876 case 4:
4877 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4878 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4879 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004880 }
4881
4882 MachineFunction *MF = BB->getParent();
4883 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4884 MachineFunction::iterator It = BB;
4885 ++It; // insert the new blocks after the current block
4886
4887 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4888 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4889 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4890 MF->insert(It, loop1MBB);
4891 MF->insert(It, loop2MBB);
4892 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004893
4894 // Transfer the remainder of BB and its successor edges to exitMBB.
4895 exitMBB->splice(exitMBB->begin(), BB,
4896 llvm::next(MachineBasicBlock::iterator(MI)),
4897 BB->end());
4898 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004899
4900 // thisMBB:
4901 // ...
4902 // fallthrough --> loop1MBB
4903 BB->addSuccessor(loop1MBB);
4904
4905 // loop1MBB:
4906 // ldrex dest, [ptr]
4907 // cmp dest, oldval
4908 // bne exitMBB
4909 BB = loop1MBB;
4910 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004911 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004912 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004913 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4914 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004915 BB->addSuccessor(loop2MBB);
4916 BB->addSuccessor(exitMBB);
4917
4918 // loop2MBB:
4919 // strex scratch, newval, [ptr]
4920 // cmp scratch, #0
4921 // bne loop1MBB
4922 BB = loop2MBB;
4923 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4924 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004925 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004926 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004927 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4928 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004929 BB->addSuccessor(loop1MBB);
4930 BB->addSuccessor(exitMBB);
4931
4932 // exitMBB:
4933 // ...
4934 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004935
Dan Gohman14152b42010-07-06 20:24:04 +00004936 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004937
Jim Grosbach5278eb82009-12-11 01:42:04 +00004938 return BB;
4939}
4940
4941MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004942ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4943 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004944 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4945 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4946
4947 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004948 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004949 MachineFunction::iterator It = BB;
4950 ++It;
4951
4952 unsigned dest = MI->getOperand(0).getReg();
4953 unsigned ptr = MI->getOperand(1).getReg();
4954 unsigned incr = MI->getOperand(2).getReg();
4955 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004956
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004957 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004958 unsigned ldrOpc, strOpc;
4959 switch (Size) {
4960 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004961 case 1:
4962 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004963 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004964 break;
4965 case 2:
4966 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4967 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4968 break;
4969 case 4:
4970 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4971 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4972 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004973 }
4974
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004975 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4976 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4977 MF->insert(It, loopMBB);
4978 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004979
4980 // Transfer the remainder of BB and its successor edges to exitMBB.
4981 exitMBB->splice(exitMBB->begin(), BB,
4982 llvm::next(MachineBasicBlock::iterator(MI)),
4983 BB->end());
4984 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004985
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004986 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004987 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4988 unsigned scratch2 = (!BinOpcode) ? incr :
4989 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4990
4991 // thisMBB:
4992 // ...
4993 // fallthrough --> loopMBB
4994 BB->addSuccessor(loopMBB);
4995
4996 // loopMBB:
4997 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004998 // <binop> scratch2, dest, incr
4999 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005000 // cmp scratch, #0
5001 // bne- loopMBB
5002 // fallthrough --> exitMBB
5003 BB = loopMBB;
5004 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005005 if (BinOpcode) {
5006 // operand order needs to go the other way for NAND
5007 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5008 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5009 addReg(incr).addReg(dest)).addReg(0);
5010 else
5011 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5012 addReg(dest).addReg(incr)).addReg(0);
5013 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005014
5015 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5016 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005017 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005018 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005019 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5020 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005021
5022 BB->addSuccessor(loopMBB);
5023 BB->addSuccessor(exitMBB);
5024
5025 // exitMBB:
5026 // ...
5027 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005028
Dan Gohman14152b42010-07-06 20:24:04 +00005029 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005030
Jim Grosbachc3c23542009-12-14 04:22:04 +00005031 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005032}
5033
Evan Cheng218977b2010-07-13 19:27:42 +00005034static
5035MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5036 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5037 E = MBB->succ_end(); I != E; ++I)
5038 if (*I != Succ)
5039 return *I;
5040 llvm_unreachable("Expecting a BB with two successors!");
5041}
5042
Andrew Trick1c3af772011-04-23 03:55:32 +00005043// FIXME: This opcode table should obviously be expressed in the target
5044// description. We probably just need a "machine opcode" value in the pseudo
5045// instruction. But the ideal solution maybe to simply remove the "S" version
5046// of the opcode altogether.
5047struct AddSubFlagsOpcodePair {
5048 unsigned PseudoOpc;
5049 unsigned MachineOpc;
5050};
5051
5052static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5053 {ARM::ADCSri, ARM::ADCri},
5054 {ARM::ADCSrr, ARM::ADCrr},
5055 {ARM::ADCSrs, ARM::ADCrs},
5056 {ARM::SBCSri, ARM::SBCri},
5057 {ARM::SBCSrr, ARM::SBCrr},
5058 {ARM::SBCSrs, ARM::SBCrs},
5059 {ARM::RSBSri, ARM::RSBri},
5060 {ARM::RSBSrr, ARM::RSBrr},
5061 {ARM::RSBSrs, ARM::RSBrs},
5062 {ARM::RSCSri, ARM::RSCri},
5063 {ARM::RSCSrs, ARM::RSCrs},
5064 {ARM::t2ADCSri, ARM::t2ADCri},
5065 {ARM::t2ADCSrr, ARM::t2ADCrr},
5066 {ARM::t2ADCSrs, ARM::t2ADCrs},
5067 {ARM::t2SBCSri, ARM::t2SBCri},
5068 {ARM::t2SBCSrr, ARM::t2SBCrr},
5069 {ARM::t2SBCSrs, ARM::t2SBCrs},
5070 {ARM::t2RSBSri, ARM::t2RSBri},
5071 {ARM::t2RSBSrs, ARM::t2RSBrs},
5072};
5073
5074// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5075// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5076//
5077// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5078// position to be recognized by the target descrition as the 'S' bit.
5079bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5080 MachineBasicBlock *BB) const {
5081 unsigned OldOpc = MI->getOpcode();
5082 unsigned NewOpc = 0;
5083
5084 // This is only called for instructions that need remapping, so iterating over
5085 // the tiny opcode table is not costly.
5086 static const int NPairs =
5087 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5088 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5089 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5090 if (OldOpc == Pair->PseudoOpc) {
5091 NewOpc = Pair->MachineOpc;
5092 break;
5093 }
5094 }
5095 if (!NewOpc)
5096 return false;
5097
5098 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5099 DebugLoc dl = MI->getDebugLoc();
5100 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5101 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5102 MIB.addOperand(MI->getOperand(i));
5103 AddDefaultPred(MIB);
5104 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5105 MI->eraseFromParent();
5106 return true;
5107}
5108
Jim Grosbache801dc42009-12-12 01:40:06 +00005109MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005110ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005111 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005112 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005113 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005114 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005115 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005116 default: {
5117 if (RemapAddSubWithFlags(MI, BB))
5118 return BB;
5119
Jim Grosbach5278eb82009-12-11 01:42:04 +00005120 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005121 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005122 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005123 case ARM::ATOMIC_LOAD_ADD_I8:
5124 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5125 case ARM::ATOMIC_LOAD_ADD_I16:
5126 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5127 case ARM::ATOMIC_LOAD_ADD_I32:
5128 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005129
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005130 case ARM::ATOMIC_LOAD_AND_I8:
5131 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5132 case ARM::ATOMIC_LOAD_AND_I16:
5133 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5134 case ARM::ATOMIC_LOAD_AND_I32:
5135 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005136
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005137 case ARM::ATOMIC_LOAD_OR_I8:
5138 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5139 case ARM::ATOMIC_LOAD_OR_I16:
5140 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5141 case ARM::ATOMIC_LOAD_OR_I32:
5142 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005143
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005144 case ARM::ATOMIC_LOAD_XOR_I8:
5145 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5146 case ARM::ATOMIC_LOAD_XOR_I16:
5147 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5148 case ARM::ATOMIC_LOAD_XOR_I32:
5149 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005150
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005151 case ARM::ATOMIC_LOAD_NAND_I8:
5152 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5153 case ARM::ATOMIC_LOAD_NAND_I16:
5154 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5155 case ARM::ATOMIC_LOAD_NAND_I32:
5156 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005157
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005158 case ARM::ATOMIC_LOAD_SUB_I8:
5159 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5160 case ARM::ATOMIC_LOAD_SUB_I16:
5161 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5162 case ARM::ATOMIC_LOAD_SUB_I32:
5163 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005164
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005165 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5166 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5167 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005168
5169 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5170 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5171 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005172
Evan Cheng007ea272009-08-12 05:17:19 +00005173 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005174 // To "insert" a SELECT_CC instruction, we actually have to insert the
5175 // diamond control-flow pattern. The incoming instruction knows the
5176 // destination vreg to set, the condition code register to branch on, the
5177 // true/false values to select between, and a branch opcode to use.
5178 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005179 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005180 ++It;
5181
5182 // thisMBB:
5183 // ...
5184 // TrueVal = ...
5185 // cmpTY ccX, r1, r2
5186 // bCC copy1MBB
5187 // fallthrough --> copy0MBB
5188 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005189 MachineFunction *F = BB->getParent();
5190 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5191 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005192 F->insert(It, copy0MBB);
5193 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005194
5195 // Transfer the remainder of BB and its successor edges to sinkMBB.
5196 sinkMBB->splice(sinkMBB->begin(), BB,
5197 llvm::next(MachineBasicBlock::iterator(MI)),
5198 BB->end());
5199 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5200
Dan Gohman258c58c2010-07-06 15:49:48 +00005201 BB->addSuccessor(copy0MBB);
5202 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005203
Dan Gohman14152b42010-07-06 20:24:04 +00005204 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5205 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5206
Evan Chenga8e29892007-01-19 07:51:42 +00005207 // copy0MBB:
5208 // %FalseValue = ...
5209 // # fallthrough to sinkMBB
5210 BB = copy0MBB;
5211
5212 // Update machine-CFG edges
5213 BB->addSuccessor(sinkMBB);
5214
5215 // sinkMBB:
5216 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5217 // ...
5218 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005219 BuildMI(*BB, BB->begin(), dl,
5220 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005221 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5222 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5223
Dan Gohman14152b42010-07-06 20:24:04 +00005224 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005225 return BB;
5226 }
Evan Cheng86198642009-08-07 00:34:42 +00005227
Evan Cheng218977b2010-07-13 19:27:42 +00005228 case ARM::BCCi64:
5229 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005230 // If there is an unconditional branch to the other successor, remove it.
5231 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005232
Evan Cheng218977b2010-07-13 19:27:42 +00005233 // Compare both parts that make up the double comparison separately for
5234 // equality.
5235 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5236
5237 unsigned LHS1 = MI->getOperand(1).getReg();
5238 unsigned LHS2 = MI->getOperand(2).getReg();
5239 if (RHSisZero) {
5240 AddDefaultPred(BuildMI(BB, dl,
5241 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5242 .addReg(LHS1).addImm(0));
5243 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5244 .addReg(LHS2).addImm(0)
5245 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5246 } else {
5247 unsigned RHS1 = MI->getOperand(3).getReg();
5248 unsigned RHS2 = MI->getOperand(4).getReg();
5249 AddDefaultPred(BuildMI(BB, dl,
5250 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5251 .addReg(LHS1).addReg(RHS1));
5252 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5253 .addReg(LHS2).addReg(RHS2)
5254 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5255 }
5256
5257 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5258 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5259 if (MI->getOperand(0).getImm() == ARMCC::NE)
5260 std::swap(destMBB, exitMBB);
5261
5262 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5263 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5264 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5265 .addMBB(exitMBB);
5266
5267 MI->eraseFromParent(); // The pseudo instruction is gone now.
5268 return BB;
5269 }
Evan Chenga8e29892007-01-19 07:51:42 +00005270 }
5271}
5272
5273//===----------------------------------------------------------------------===//
5274// ARM Optimization Hooks
5275//===----------------------------------------------------------------------===//
5276
Chris Lattnerd1980a52009-03-12 06:52:53 +00005277static
5278SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5279 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005280 SelectionDAG &DAG = DCI.DAG;
5281 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005282 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005283 unsigned Opc = N->getOpcode();
5284 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5285 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5286 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5287 ISD::CondCode CC = ISD::SETCC_INVALID;
5288
5289 if (isSlctCC) {
5290 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5291 } else {
5292 SDValue CCOp = Slct.getOperand(0);
5293 if (CCOp.getOpcode() == ISD::SETCC)
5294 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5295 }
5296
5297 bool DoXform = false;
5298 bool InvCC = false;
5299 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5300 "Bad input!");
5301
5302 if (LHS.getOpcode() == ISD::Constant &&
5303 cast<ConstantSDNode>(LHS)->isNullValue()) {
5304 DoXform = true;
5305 } else if (CC != ISD::SETCC_INVALID &&
5306 RHS.getOpcode() == ISD::Constant &&
5307 cast<ConstantSDNode>(RHS)->isNullValue()) {
5308 std::swap(LHS, RHS);
5309 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005310 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005311 Op0.getOperand(0).getValueType();
5312 bool isInt = OpVT.isInteger();
5313 CC = ISD::getSetCCInverse(CC, isInt);
5314
5315 if (!TLI.isCondCodeLegal(CC, OpVT))
5316 return SDValue(); // Inverse operator isn't legal.
5317
5318 DoXform = true;
5319 InvCC = true;
5320 }
5321
5322 if (DoXform) {
5323 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5324 if (isSlctCC)
5325 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5326 Slct.getOperand(0), Slct.getOperand(1), CC);
5327 SDValue CCOp = Slct.getOperand(0);
5328 if (InvCC)
5329 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5330 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5331 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5332 CCOp, OtherOp, Result);
5333 }
5334 return SDValue();
5335}
5336
Bob Wilson3d5792a2010-07-29 20:34:14 +00005337/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5338/// operands N0 and N1. This is a helper for PerformADDCombine that is
5339/// called with the default operands, and if that fails, with commuted
5340/// operands.
5341static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5342 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005343 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5344 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5345 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5346 if (Result.getNode()) return Result;
5347 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005348 return SDValue();
5349}
5350
Bob Wilson3d5792a2010-07-29 20:34:14 +00005351/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5352///
5353static SDValue PerformADDCombine(SDNode *N,
5354 TargetLowering::DAGCombinerInfo &DCI) {
5355 SDValue N0 = N->getOperand(0);
5356 SDValue N1 = N->getOperand(1);
5357
5358 // First try with the default operand order.
5359 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5360 if (Result.getNode())
5361 return Result;
5362
5363 // If that didn't work, try again with the operands commuted.
5364 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5365}
5366
Chris Lattnerd1980a52009-03-12 06:52:53 +00005367/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005368///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005369static SDValue PerformSUBCombine(SDNode *N,
5370 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005371 SDValue N0 = N->getOperand(0);
5372 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005373
Chris Lattnerd1980a52009-03-12 06:52:53 +00005374 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5375 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5376 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5377 if (Result.getNode()) return Result;
5378 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005379
Chris Lattnerd1980a52009-03-12 06:52:53 +00005380 return SDValue();
5381}
5382
Evan Cheng463d3582011-03-31 19:38:48 +00005383/// PerformVMULCombine
5384/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5385/// special multiplier accumulator forwarding.
5386/// vmul d3, d0, d2
5387/// vmla d3, d1, d2
5388/// is faster than
5389/// vadd d3, d0, d1
5390/// vmul d3, d3, d2
5391static SDValue PerformVMULCombine(SDNode *N,
5392 TargetLowering::DAGCombinerInfo &DCI,
5393 const ARMSubtarget *Subtarget) {
5394 if (!Subtarget->hasVMLxForwarding())
5395 return SDValue();
5396
5397 SelectionDAG &DAG = DCI.DAG;
5398 SDValue N0 = N->getOperand(0);
5399 SDValue N1 = N->getOperand(1);
5400 unsigned Opcode = N0.getOpcode();
5401 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5402 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5403 Opcode = N0.getOpcode();
5404 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5405 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5406 return SDValue();
5407 std::swap(N0, N1);
5408 }
5409
5410 EVT VT = N->getValueType(0);
5411 DebugLoc DL = N->getDebugLoc();
5412 SDValue N00 = N0->getOperand(0);
5413 SDValue N01 = N0->getOperand(1);
5414 return DAG.getNode(Opcode, DL, VT,
5415 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5416 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5417}
5418
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005419static SDValue PerformMULCombine(SDNode *N,
5420 TargetLowering::DAGCombinerInfo &DCI,
5421 const ARMSubtarget *Subtarget) {
5422 SelectionDAG &DAG = DCI.DAG;
5423
5424 if (Subtarget->isThumb1Only())
5425 return SDValue();
5426
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005427 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5428 return SDValue();
5429
5430 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005431 if (VT.is64BitVector() || VT.is128BitVector())
5432 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005433 if (VT != MVT::i32)
5434 return SDValue();
5435
5436 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5437 if (!C)
5438 return SDValue();
5439
5440 uint64_t MulAmt = C->getZExtValue();
5441 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5442 ShiftAmt = ShiftAmt & (32 - 1);
5443 SDValue V = N->getOperand(0);
5444 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005445
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005446 SDValue Res;
5447 MulAmt >>= ShiftAmt;
5448 if (isPowerOf2_32(MulAmt - 1)) {
5449 // (mul x, 2^N + 1) => (add (shl x, N), x)
5450 Res = DAG.getNode(ISD::ADD, DL, VT,
5451 V, DAG.getNode(ISD::SHL, DL, VT,
5452 V, DAG.getConstant(Log2_32(MulAmt-1),
5453 MVT::i32)));
5454 } else if (isPowerOf2_32(MulAmt + 1)) {
5455 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5456 Res = DAG.getNode(ISD::SUB, DL, VT,
5457 DAG.getNode(ISD::SHL, DL, VT,
5458 V, DAG.getConstant(Log2_32(MulAmt+1),
5459 MVT::i32)),
5460 V);
5461 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005462 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005463
5464 if (ShiftAmt != 0)
5465 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5466 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005467
5468 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005469 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005470 return SDValue();
5471}
5472
Owen Anderson080c0922010-11-05 19:27:46 +00005473static SDValue PerformANDCombine(SDNode *N,
5474 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005475
Owen Anderson080c0922010-11-05 19:27:46 +00005476 // Attempt to use immediate-form VBIC
5477 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5478 DebugLoc dl = N->getDebugLoc();
5479 EVT VT = N->getValueType(0);
5480 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005481
Tanya Lattner0433b212011-04-07 15:24:20 +00005482 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5483 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005484
Owen Anderson080c0922010-11-05 19:27:46 +00005485 APInt SplatBits, SplatUndef;
5486 unsigned SplatBitSize;
5487 bool HasAnyUndefs;
5488 if (BVN &&
5489 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5490 if (SplatBitSize <= 64) {
5491 EVT VbicVT;
5492 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5493 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005494 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005495 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005496 if (Val.getNode()) {
5497 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005498 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005499 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005500 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005501 }
5502 }
5503 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005504
Owen Anderson080c0922010-11-05 19:27:46 +00005505 return SDValue();
5506}
5507
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005508/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5509static SDValue PerformORCombine(SDNode *N,
5510 TargetLowering::DAGCombinerInfo &DCI,
5511 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005512 // Attempt to use immediate-form VORR
5513 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5514 DebugLoc dl = N->getDebugLoc();
5515 EVT VT = N->getValueType(0);
5516 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005517
Tanya Lattner0433b212011-04-07 15:24:20 +00005518 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5519 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005520
Owen Anderson60f48702010-11-03 23:15:26 +00005521 APInt SplatBits, SplatUndef;
5522 unsigned SplatBitSize;
5523 bool HasAnyUndefs;
5524 if (BVN && Subtarget->hasNEON() &&
5525 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5526 if (SplatBitSize <= 64) {
5527 EVT VorrVT;
5528 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5529 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005530 DAG, VorrVT, VT.is128BitVector(),
5531 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005532 if (Val.getNode()) {
5533 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005534 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005535 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005536 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005537 }
5538 }
5539 }
5540
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005541 SDValue N0 = N->getOperand(0);
5542 if (N0.getOpcode() != ISD::AND)
5543 return SDValue();
5544 SDValue N1 = N->getOperand(1);
5545
5546 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5547 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5548 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5549 APInt SplatUndef;
5550 unsigned SplatBitSize;
5551 bool HasAnyUndefs;
5552
5553 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5554 APInt SplatBits0;
5555 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5556 HasAnyUndefs) && !HasAnyUndefs) {
5557 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5558 APInt SplatBits1;
5559 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5560 HasAnyUndefs) && !HasAnyUndefs &&
5561 SplatBits0 == ~SplatBits1) {
5562 // Canonicalize the vector type to make instruction selection simpler.
5563 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5564 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5565 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005566 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005567 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5568 }
5569 }
5570 }
5571
Jim Grosbach54238562010-07-17 03:30:54 +00005572 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5573 // reasonable.
5574
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005575 // BFI is only available on V6T2+
5576 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5577 return SDValue();
5578
Jim Grosbach54238562010-07-17 03:30:54 +00005579 DebugLoc DL = N->getDebugLoc();
5580 // 1) or (and A, mask), val => ARMbfi A, val, mask
5581 // iff (val & mask) == val
5582 //
5583 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5584 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005585 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005586 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005587 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005588 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005589
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005590 if (VT != MVT::i32)
5591 return SDValue();
5592
Evan Cheng30fb13f2010-12-13 20:32:54 +00005593 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005594
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005595 // The value and the mask need to be constants so we can verify this is
5596 // actually a bitfield set. If the mask is 0xffff, we can do better
5597 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005598 SDValue MaskOp = N0.getOperand(1);
5599 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5600 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005601 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005602 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005603 if (Mask == 0xffff)
5604 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005605 SDValue Res;
5606 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005607 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5608 if (N1C) {
5609 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005610 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005611 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005612
Evan Chenga9688c42010-12-11 04:11:38 +00005613 if (ARM::isBitFieldInvertedMask(Mask)) {
5614 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005615
Evan Cheng30fb13f2010-12-13 20:32:54 +00005616 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005617 DAG.getConstant(Val, MVT::i32),
5618 DAG.getConstant(Mask, MVT::i32));
5619
5620 // Do not add new nodes to DAG combiner worklist.
5621 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005622 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005623 }
Jim Grosbach54238562010-07-17 03:30:54 +00005624 } else if (N1.getOpcode() == ISD::AND) {
5625 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005626 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5627 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005628 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005629 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005630
Eric Christopher29aeed12011-03-26 01:21:03 +00005631 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5632 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005633 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005634 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005635 // The pack halfword instruction works better for masks that fit it,
5636 // so use that when it's available.
5637 if (Subtarget->hasT2ExtractPack() &&
5638 (Mask == 0xffff || Mask == 0xffff0000))
5639 return SDValue();
5640 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005641 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005642 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005643 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005644 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005645 DAG.getConstant(Mask, MVT::i32));
5646 // Do not add new nodes to DAG combiner worklist.
5647 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005648 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005649 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005650 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005651 // The pack halfword instruction works better for masks that fit it,
5652 // so use that when it's available.
5653 if (Subtarget->hasT2ExtractPack() &&
5654 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5655 return SDValue();
5656 // 2b
5657 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005658 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005659 DAG.getConstant(lsb, MVT::i32));
5660 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005661 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005662 // Do not add new nodes to DAG combiner worklist.
5663 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005664 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005665 }
5666 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005667
Evan Cheng30fb13f2010-12-13 20:32:54 +00005668 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5669 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5670 ARM::isBitFieldInvertedMask(~Mask)) {
5671 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5672 // where lsb(mask) == #shamt and masked bits of B are known zero.
5673 SDValue ShAmt = N00.getOperand(1);
5674 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5675 unsigned LSB = CountTrailingZeros_32(Mask);
5676 if (ShAmtC != LSB)
5677 return SDValue();
5678
5679 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5680 DAG.getConstant(~Mask, MVT::i32));
5681
5682 // Do not add new nodes to DAG combiner worklist.
5683 DCI.CombineTo(N, Res, false);
5684 }
5685
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005686 return SDValue();
5687}
5688
Evan Cheng0c1aec12010-12-14 03:22:07 +00005689/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5690/// C1 & C2 == C1.
5691static SDValue PerformBFICombine(SDNode *N,
5692 TargetLowering::DAGCombinerInfo &DCI) {
5693 SDValue N1 = N->getOperand(1);
5694 if (N1.getOpcode() == ISD::AND) {
5695 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5696 if (!N11C)
5697 return SDValue();
5698 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5699 unsigned Mask2 = N11C->getZExtValue();
5700 if ((Mask & Mask2) == Mask2)
5701 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5702 N->getOperand(0), N1.getOperand(0),
5703 N->getOperand(2));
5704 }
5705 return SDValue();
5706}
5707
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005708/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5709/// ARMISD::VMOVRRD.
5710static SDValue PerformVMOVRRDCombine(SDNode *N,
5711 TargetLowering::DAGCombinerInfo &DCI) {
5712 // vmovrrd(vmovdrr x, y) -> x,y
5713 SDValue InDouble = N->getOperand(0);
5714 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5715 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00005716
5717 // vmovrrd(load f64) -> (load i32), (load i32)
5718 SDNode *InNode = InDouble.getNode();
5719 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
5720 InNode->getValueType(0) == MVT::f64 &&
5721 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
5722 !cast<LoadSDNode>(InNode)->isVolatile()) {
5723 // TODO: Should this be done for non-FrameIndex operands?
5724 LoadSDNode *LD = cast<LoadSDNode>(InNode);
5725
5726 SelectionDAG &DAG = DCI.DAG;
5727 DebugLoc DL = LD->getDebugLoc();
5728 SDValue BasePtr = LD->getBasePtr();
5729 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
5730 LD->getPointerInfo(), LD->isVolatile(),
5731 LD->isNonTemporal(), LD->getAlignment());
5732
5733 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5734 DAG.getConstant(4, MVT::i32));
5735 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
5736 LD->getPointerInfo(), LD->isVolatile(),
5737 LD->isNonTemporal(),
5738 std::min(4U, LD->getAlignment() / 2));
5739
5740 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
5741 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
5742 DCI.RemoveFromWorklist(LD);
5743 DAG.DeleteNode(LD);
5744 return Result;
5745 }
5746
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005747 return SDValue();
5748}
5749
5750/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5751/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5752static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5753 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5754 SDValue Op0 = N->getOperand(0);
5755 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005756 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005757 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005758 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005759 Op1 = Op1.getOperand(0);
5760 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5761 Op0.getNode() == Op1.getNode() &&
5762 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005763 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005764 N->getValueType(0), Op0.getOperand(0));
5765 return SDValue();
5766}
5767
Bob Wilson31600902010-12-21 06:43:19 +00005768/// PerformSTORECombine - Target-specific dag combine xforms for
5769/// ISD::STORE.
5770static SDValue PerformSTORECombine(SDNode *N,
5771 TargetLowering::DAGCombinerInfo &DCI) {
5772 // Bitcast an i64 store extracted from a vector to f64.
5773 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5774 StoreSDNode *St = cast<StoreSDNode>(N);
5775 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00005776 if (!ISD::isNormalStore(St) || St->isVolatile())
5777 return SDValue();
5778
5779 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
5780 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
5781 SelectionDAG &DAG = DCI.DAG;
5782 DebugLoc DL = St->getDebugLoc();
5783 SDValue BasePtr = St->getBasePtr();
5784 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
5785 StVal.getNode()->getOperand(0), BasePtr,
5786 St->getPointerInfo(), St->isVolatile(),
5787 St->isNonTemporal(), St->getAlignment());
5788
5789 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5790 DAG.getConstant(4, MVT::i32));
5791 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
5792 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
5793 St->isNonTemporal(),
5794 std::min(4U, St->getAlignment() / 2));
5795 }
5796
5797 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00005798 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5799 return SDValue();
5800
5801 SelectionDAG &DAG = DCI.DAG;
5802 DebugLoc dl = StVal.getDebugLoc();
5803 SDValue IntVec = StVal.getOperand(0);
5804 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5805 IntVec.getValueType().getVectorNumElements());
5806 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5807 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5808 Vec, StVal.getOperand(1));
5809 dl = N->getDebugLoc();
5810 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5811 // Make the DAGCombiner fold the bitcasts.
5812 DCI.AddToWorklist(Vec.getNode());
5813 DCI.AddToWorklist(ExtElt.getNode());
5814 DCI.AddToWorklist(V.getNode());
5815 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5816 St->getPointerInfo(), St->isVolatile(),
5817 St->isNonTemporal(), St->getAlignment(),
5818 St->getTBAAInfo());
5819}
5820
5821/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5822/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5823/// i64 vector to have f64 elements, since the value can then be loaded
5824/// directly into a VFP register.
5825static bool hasNormalLoadOperand(SDNode *N) {
5826 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5827 for (unsigned i = 0; i < NumElts; ++i) {
5828 SDNode *Elt = N->getOperand(i).getNode();
5829 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5830 return true;
5831 }
5832 return false;
5833}
5834
Bob Wilson75f02882010-09-17 22:59:05 +00005835/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5836/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005837static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5838 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005839 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5840 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5841 // into a pair of GPRs, which is fine when the value is used as a scalar,
5842 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005843 SelectionDAG &DAG = DCI.DAG;
5844 if (N->getNumOperands() == 2) {
5845 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5846 if (RV.getNode())
5847 return RV;
5848 }
Bob Wilson75f02882010-09-17 22:59:05 +00005849
Bob Wilson31600902010-12-21 06:43:19 +00005850 // Load i64 elements as f64 values so that type legalization does not split
5851 // them up into i32 values.
5852 EVT VT = N->getValueType(0);
5853 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5854 return SDValue();
5855 DebugLoc dl = N->getDebugLoc();
5856 SmallVector<SDValue, 8> Ops;
5857 unsigned NumElts = VT.getVectorNumElements();
5858 for (unsigned i = 0; i < NumElts; ++i) {
5859 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5860 Ops.push_back(V);
5861 // Make the DAGCombiner fold the bitcast.
5862 DCI.AddToWorklist(V.getNode());
5863 }
5864 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5865 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5866 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5867}
5868
5869/// PerformInsertEltCombine - Target-specific dag combine xforms for
5870/// ISD::INSERT_VECTOR_ELT.
5871static SDValue PerformInsertEltCombine(SDNode *N,
5872 TargetLowering::DAGCombinerInfo &DCI) {
5873 // Bitcast an i64 load inserted into a vector to f64.
5874 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5875 EVT VT = N->getValueType(0);
5876 SDNode *Elt = N->getOperand(1).getNode();
5877 if (VT.getVectorElementType() != MVT::i64 ||
5878 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5879 return SDValue();
5880
5881 SelectionDAG &DAG = DCI.DAG;
5882 DebugLoc dl = N->getDebugLoc();
5883 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5884 VT.getVectorNumElements());
5885 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5886 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5887 // Make the DAGCombiner fold the bitcasts.
5888 DCI.AddToWorklist(Vec.getNode());
5889 DCI.AddToWorklist(V.getNode());
5890 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5891 Vec, V, N->getOperand(2));
5892 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005893}
5894
Bob Wilsonf20700c2010-10-27 20:38:28 +00005895/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5896/// ISD::VECTOR_SHUFFLE.
5897static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5898 // The LLVM shufflevector instruction does not require the shuffle mask
5899 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5900 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5901 // operands do not match the mask length, they are extended by concatenating
5902 // them with undef vectors. That is probably the right thing for other
5903 // targets, but for NEON it is better to concatenate two double-register
5904 // size vector operands into a single quad-register size vector. Do that
5905 // transformation here:
5906 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5907 // shuffle(concat(v1, v2), undef)
5908 SDValue Op0 = N->getOperand(0);
5909 SDValue Op1 = N->getOperand(1);
5910 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5911 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5912 Op0.getNumOperands() != 2 ||
5913 Op1.getNumOperands() != 2)
5914 return SDValue();
5915 SDValue Concat0Op1 = Op0.getOperand(1);
5916 SDValue Concat1Op1 = Op1.getOperand(1);
5917 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5918 Concat1Op1.getOpcode() != ISD::UNDEF)
5919 return SDValue();
5920 // Skip the transformation if any of the types are illegal.
5921 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5922 EVT VT = N->getValueType(0);
5923 if (!TLI.isTypeLegal(VT) ||
5924 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5925 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5926 return SDValue();
5927
5928 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5929 Op0.getOperand(0), Op1.getOperand(0));
5930 // Translate the shuffle mask.
5931 SmallVector<int, 16> NewMask;
5932 unsigned NumElts = VT.getVectorNumElements();
5933 unsigned HalfElts = NumElts/2;
5934 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5935 for (unsigned n = 0; n < NumElts; ++n) {
5936 int MaskElt = SVN->getMaskElt(n);
5937 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005938 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005939 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005940 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005941 NewElt = HalfElts + MaskElt - NumElts;
5942 NewMask.push_back(NewElt);
5943 }
5944 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5945 DAG.getUNDEF(VT), NewMask.data());
5946}
5947
Bob Wilson1c3ef902011-02-07 17:43:21 +00005948/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5949/// NEON load/store intrinsics to merge base address updates.
5950static SDValue CombineBaseUpdate(SDNode *N,
5951 TargetLowering::DAGCombinerInfo &DCI) {
5952 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5953 return SDValue();
5954
5955 SelectionDAG &DAG = DCI.DAG;
5956 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5957 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5958 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5959 SDValue Addr = N->getOperand(AddrOpIdx);
5960
5961 // Search for a use of the address operand that is an increment.
5962 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5963 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5964 SDNode *User = *UI;
5965 if (User->getOpcode() != ISD::ADD ||
5966 UI.getUse().getResNo() != Addr.getResNo())
5967 continue;
5968
5969 // Check that the add is independent of the load/store. Otherwise, folding
5970 // it would create a cycle.
5971 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5972 continue;
5973
5974 // Find the new opcode for the updating load/store.
5975 bool isLoad = true;
5976 bool isLaneOp = false;
5977 unsigned NewOpc = 0;
5978 unsigned NumVecs = 0;
5979 if (isIntrinsic) {
5980 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5981 switch (IntNo) {
5982 default: assert(0 && "unexpected intrinsic for Neon base update");
5983 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5984 NumVecs = 1; break;
5985 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5986 NumVecs = 2; break;
5987 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5988 NumVecs = 3; break;
5989 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5990 NumVecs = 4; break;
5991 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5992 NumVecs = 2; isLaneOp = true; break;
5993 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5994 NumVecs = 3; isLaneOp = true; break;
5995 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5996 NumVecs = 4; isLaneOp = true; break;
5997 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5998 NumVecs = 1; isLoad = false; break;
5999 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6000 NumVecs = 2; isLoad = false; break;
6001 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6002 NumVecs = 3; isLoad = false; break;
6003 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6004 NumVecs = 4; isLoad = false; break;
6005 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6006 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6007 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6008 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6009 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6010 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6011 }
6012 } else {
6013 isLaneOp = true;
6014 switch (N->getOpcode()) {
6015 default: assert(0 && "unexpected opcode for Neon base update");
6016 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6017 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6018 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6019 }
6020 }
6021
6022 // Find the size of memory referenced by the load/store.
6023 EVT VecTy;
6024 if (isLoad)
6025 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006026 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006027 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6028 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6029 if (isLaneOp)
6030 NumBytes /= VecTy.getVectorNumElements();
6031
6032 // If the increment is a constant, it must match the memory ref size.
6033 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6034 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6035 uint64_t IncVal = CInc->getZExtValue();
6036 if (IncVal != NumBytes)
6037 continue;
6038 } else if (NumBytes >= 3 * 16) {
6039 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6040 // separate instructions that make it harder to use a non-constant update.
6041 continue;
6042 }
6043
6044 // Create the new updating load/store node.
6045 EVT Tys[6];
6046 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6047 unsigned n;
6048 for (n = 0; n < NumResultVecs; ++n)
6049 Tys[n] = VecTy;
6050 Tys[n++] = MVT::i32;
6051 Tys[n] = MVT::Other;
6052 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6053 SmallVector<SDValue, 8> Ops;
6054 Ops.push_back(N->getOperand(0)); // incoming chain
6055 Ops.push_back(N->getOperand(AddrOpIdx));
6056 Ops.push_back(Inc);
6057 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6058 Ops.push_back(N->getOperand(i));
6059 }
6060 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6061 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6062 Ops.data(), Ops.size(),
6063 MemInt->getMemoryVT(),
6064 MemInt->getMemOperand());
6065
6066 // Update the uses.
6067 std::vector<SDValue> NewResults;
6068 for (unsigned i = 0; i < NumResultVecs; ++i) {
6069 NewResults.push_back(SDValue(UpdN.getNode(), i));
6070 }
6071 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6072 DCI.CombineTo(N, NewResults);
6073 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6074
6075 break;
Owen Anderson76706012011-04-05 21:48:57 +00006076 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006077 return SDValue();
6078}
6079
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006080/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6081/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6082/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6083/// return true.
6084static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6085 SelectionDAG &DAG = DCI.DAG;
6086 EVT VT = N->getValueType(0);
6087 // vldN-dup instructions only support 64-bit vectors for N > 1.
6088 if (!VT.is64BitVector())
6089 return false;
6090
6091 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6092 SDNode *VLD = N->getOperand(0).getNode();
6093 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6094 return false;
6095 unsigned NumVecs = 0;
6096 unsigned NewOpc = 0;
6097 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6098 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6099 NumVecs = 2;
6100 NewOpc = ARMISD::VLD2DUP;
6101 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6102 NumVecs = 3;
6103 NewOpc = ARMISD::VLD3DUP;
6104 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6105 NumVecs = 4;
6106 NewOpc = ARMISD::VLD4DUP;
6107 } else {
6108 return false;
6109 }
6110
6111 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6112 // numbers match the load.
6113 unsigned VLDLaneNo =
6114 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6115 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6116 UI != UE; ++UI) {
6117 // Ignore uses of the chain result.
6118 if (UI.getUse().getResNo() == NumVecs)
6119 continue;
6120 SDNode *User = *UI;
6121 if (User->getOpcode() != ARMISD::VDUPLANE ||
6122 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6123 return false;
6124 }
6125
6126 // Create the vldN-dup node.
6127 EVT Tys[5];
6128 unsigned n;
6129 for (n = 0; n < NumVecs; ++n)
6130 Tys[n] = VT;
6131 Tys[n] = MVT::Other;
6132 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6133 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6134 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6135 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6136 Ops, 2, VLDMemInt->getMemoryVT(),
6137 VLDMemInt->getMemOperand());
6138
6139 // Update the uses.
6140 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6141 UI != UE; ++UI) {
6142 unsigned ResNo = UI.getUse().getResNo();
6143 // Ignore uses of the chain result.
6144 if (ResNo == NumVecs)
6145 continue;
6146 SDNode *User = *UI;
6147 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6148 }
6149
6150 // Now the vldN-lane intrinsic is dead except for its chain result.
6151 // Update uses of the chain.
6152 std::vector<SDValue> VLDDupResults;
6153 for (unsigned n = 0; n < NumVecs; ++n)
6154 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6155 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6156 DCI.CombineTo(VLD, VLDDupResults);
6157
6158 return true;
6159}
6160
Bob Wilson9e82bf12010-07-14 01:22:12 +00006161/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6162/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006163static SDValue PerformVDUPLANECombine(SDNode *N,
6164 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006165 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006166
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006167 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6168 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6169 if (CombineVLDDUP(N, DCI))
6170 return SDValue(N, 0);
6171
6172 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6173 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006174 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006175 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006176 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006177 return SDValue();
6178
6179 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6180 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6181 // The canonical VMOV for a zero vector uses a 32-bit element size.
6182 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6183 unsigned EltBits;
6184 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6185 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006186 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006187 if (EltSize > VT.getVectorElementType().getSizeInBits())
6188 return SDValue();
6189
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006190 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006191}
6192
Bob Wilson5bafff32009-06-22 23:27:02 +00006193/// getVShiftImm - Check if this is a valid build_vector for the immediate
6194/// operand of a vector shift operation, where all the elements of the
6195/// build_vector must have the same constant integer value.
6196static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6197 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006198 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006199 Op = Op.getOperand(0);
6200 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6201 APInt SplatBits, SplatUndef;
6202 unsigned SplatBitSize;
6203 bool HasAnyUndefs;
6204 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6205 HasAnyUndefs, ElementBits) ||
6206 SplatBitSize > ElementBits)
6207 return false;
6208 Cnt = SplatBits.getSExtValue();
6209 return true;
6210}
6211
6212/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6213/// operand of a vector shift left operation. That value must be in the range:
6214/// 0 <= Value < ElementBits for a left shift; or
6215/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006216static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006217 assert(VT.isVector() && "vector shift count is not a vector type");
6218 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6219 if (! getVShiftImm(Op, ElementBits, Cnt))
6220 return false;
6221 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6222}
6223
6224/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6225/// operand of a vector shift right operation. For a shift opcode, the value
6226/// is positive, but for an intrinsic the value count must be negative. The
6227/// absolute value must be in the range:
6228/// 1 <= |Value| <= ElementBits for a right shift; or
6229/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006230static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006231 int64_t &Cnt) {
6232 assert(VT.isVector() && "vector shift count is not a vector type");
6233 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6234 if (! getVShiftImm(Op, ElementBits, Cnt))
6235 return false;
6236 if (isIntrinsic)
6237 Cnt = -Cnt;
6238 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6239}
6240
6241/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6242static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6243 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6244 switch (IntNo) {
6245 default:
6246 // Don't do anything for most intrinsics.
6247 break;
6248
6249 // Vector shifts: check for immediate versions and lower them.
6250 // Note: This is done during DAG combining instead of DAG legalizing because
6251 // the build_vectors for 64-bit vector element shift counts are generally
6252 // not legal, and it is hard to see their values after they get legalized to
6253 // loads from a constant pool.
6254 case Intrinsic::arm_neon_vshifts:
6255 case Intrinsic::arm_neon_vshiftu:
6256 case Intrinsic::arm_neon_vshiftls:
6257 case Intrinsic::arm_neon_vshiftlu:
6258 case Intrinsic::arm_neon_vshiftn:
6259 case Intrinsic::arm_neon_vrshifts:
6260 case Intrinsic::arm_neon_vrshiftu:
6261 case Intrinsic::arm_neon_vrshiftn:
6262 case Intrinsic::arm_neon_vqshifts:
6263 case Intrinsic::arm_neon_vqshiftu:
6264 case Intrinsic::arm_neon_vqshiftsu:
6265 case Intrinsic::arm_neon_vqshiftns:
6266 case Intrinsic::arm_neon_vqshiftnu:
6267 case Intrinsic::arm_neon_vqshiftnsu:
6268 case Intrinsic::arm_neon_vqrshiftns:
6269 case Intrinsic::arm_neon_vqrshiftnu:
6270 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006271 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006272 int64_t Cnt;
6273 unsigned VShiftOpc = 0;
6274
6275 switch (IntNo) {
6276 case Intrinsic::arm_neon_vshifts:
6277 case Intrinsic::arm_neon_vshiftu:
6278 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6279 VShiftOpc = ARMISD::VSHL;
6280 break;
6281 }
6282 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6283 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6284 ARMISD::VSHRs : ARMISD::VSHRu);
6285 break;
6286 }
6287 return SDValue();
6288
6289 case Intrinsic::arm_neon_vshiftls:
6290 case Intrinsic::arm_neon_vshiftlu:
6291 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6292 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006293 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006294
6295 case Intrinsic::arm_neon_vrshifts:
6296 case Intrinsic::arm_neon_vrshiftu:
6297 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6298 break;
6299 return SDValue();
6300
6301 case Intrinsic::arm_neon_vqshifts:
6302 case Intrinsic::arm_neon_vqshiftu:
6303 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6304 break;
6305 return SDValue();
6306
6307 case Intrinsic::arm_neon_vqshiftsu:
6308 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6309 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006310 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006311
6312 case Intrinsic::arm_neon_vshiftn:
6313 case Intrinsic::arm_neon_vrshiftn:
6314 case Intrinsic::arm_neon_vqshiftns:
6315 case Intrinsic::arm_neon_vqshiftnu:
6316 case Intrinsic::arm_neon_vqshiftnsu:
6317 case Intrinsic::arm_neon_vqrshiftns:
6318 case Intrinsic::arm_neon_vqrshiftnu:
6319 case Intrinsic::arm_neon_vqrshiftnsu:
6320 // Narrowing shifts require an immediate right shift.
6321 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6322 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006323 llvm_unreachable("invalid shift count for narrowing vector shift "
6324 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006325
6326 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006327 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006328 }
6329
6330 switch (IntNo) {
6331 case Intrinsic::arm_neon_vshifts:
6332 case Intrinsic::arm_neon_vshiftu:
6333 // Opcode already set above.
6334 break;
6335 case Intrinsic::arm_neon_vshiftls:
6336 case Intrinsic::arm_neon_vshiftlu:
6337 if (Cnt == VT.getVectorElementType().getSizeInBits())
6338 VShiftOpc = ARMISD::VSHLLi;
6339 else
6340 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6341 ARMISD::VSHLLs : ARMISD::VSHLLu);
6342 break;
6343 case Intrinsic::arm_neon_vshiftn:
6344 VShiftOpc = ARMISD::VSHRN; break;
6345 case Intrinsic::arm_neon_vrshifts:
6346 VShiftOpc = ARMISD::VRSHRs; break;
6347 case Intrinsic::arm_neon_vrshiftu:
6348 VShiftOpc = ARMISD::VRSHRu; break;
6349 case Intrinsic::arm_neon_vrshiftn:
6350 VShiftOpc = ARMISD::VRSHRN; break;
6351 case Intrinsic::arm_neon_vqshifts:
6352 VShiftOpc = ARMISD::VQSHLs; break;
6353 case Intrinsic::arm_neon_vqshiftu:
6354 VShiftOpc = ARMISD::VQSHLu; break;
6355 case Intrinsic::arm_neon_vqshiftsu:
6356 VShiftOpc = ARMISD::VQSHLsu; break;
6357 case Intrinsic::arm_neon_vqshiftns:
6358 VShiftOpc = ARMISD::VQSHRNs; break;
6359 case Intrinsic::arm_neon_vqshiftnu:
6360 VShiftOpc = ARMISD::VQSHRNu; break;
6361 case Intrinsic::arm_neon_vqshiftnsu:
6362 VShiftOpc = ARMISD::VQSHRNsu; break;
6363 case Intrinsic::arm_neon_vqrshiftns:
6364 VShiftOpc = ARMISD::VQRSHRNs; break;
6365 case Intrinsic::arm_neon_vqrshiftnu:
6366 VShiftOpc = ARMISD::VQRSHRNu; break;
6367 case Intrinsic::arm_neon_vqrshiftnsu:
6368 VShiftOpc = ARMISD::VQRSHRNsu; break;
6369 }
6370
6371 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006372 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006373 }
6374
6375 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006376 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006377 int64_t Cnt;
6378 unsigned VShiftOpc = 0;
6379
6380 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6381 VShiftOpc = ARMISD::VSLI;
6382 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6383 VShiftOpc = ARMISD::VSRI;
6384 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006385 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006386 }
6387
6388 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6389 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006390 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006391 }
6392
6393 case Intrinsic::arm_neon_vqrshifts:
6394 case Intrinsic::arm_neon_vqrshiftu:
6395 // No immediate versions of these to check for.
6396 break;
6397 }
6398
6399 return SDValue();
6400}
6401
6402/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6403/// lowers them. As with the vector shift intrinsics, this is done during DAG
6404/// combining instead of DAG legalizing because the build_vectors for 64-bit
6405/// vector element shift counts are generally not legal, and it is hard to see
6406/// their values after they get legalized to loads from a constant pool.
6407static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6408 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006409 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006410
6411 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006412 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6413 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006414 return SDValue();
6415
6416 assert(ST->hasNEON() && "unexpected vector shift");
6417 int64_t Cnt;
6418
6419 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006420 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006421
6422 case ISD::SHL:
6423 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6424 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006425 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006426 break;
6427
6428 case ISD::SRA:
6429 case ISD::SRL:
6430 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6431 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6432 ARMISD::VSHRs : ARMISD::VSHRu);
6433 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006434 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006435 }
6436 }
6437 return SDValue();
6438}
6439
6440/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6441/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6442static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6443 const ARMSubtarget *ST) {
6444 SDValue N0 = N->getOperand(0);
6445
6446 // Check for sign- and zero-extensions of vector extract operations of 8-
6447 // and 16-bit vector elements. NEON supports these directly. They are
6448 // handled during DAG combining because type legalization will promote them
6449 // to 32-bit types and it is messy to recognize the operations after that.
6450 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6451 SDValue Vec = N0.getOperand(0);
6452 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006453 EVT VT = N->getValueType(0);
6454 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006455 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6456
Owen Anderson825b72b2009-08-11 20:47:22 +00006457 if (VT == MVT::i32 &&
6458 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006459 TLI.isTypeLegal(Vec.getValueType()) &&
6460 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006461
6462 unsigned Opc = 0;
6463 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006464 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006465 case ISD::SIGN_EXTEND:
6466 Opc = ARMISD::VGETLANEs;
6467 break;
6468 case ISD::ZERO_EXTEND:
6469 case ISD::ANY_EXTEND:
6470 Opc = ARMISD::VGETLANEu;
6471 break;
6472 }
6473 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6474 }
6475 }
6476
6477 return SDValue();
6478}
6479
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006480/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6481/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6482static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6483 const ARMSubtarget *ST) {
6484 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006485 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006486 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6487 // a NaN; only do the transformation when it matches that behavior.
6488
6489 // For now only do this when using NEON for FP operations; if using VFP, it
6490 // is not obvious that the benefit outweighs the cost of switching to the
6491 // NEON pipeline.
6492 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6493 N->getValueType(0) != MVT::f32)
6494 return SDValue();
6495
6496 SDValue CondLHS = N->getOperand(0);
6497 SDValue CondRHS = N->getOperand(1);
6498 SDValue LHS = N->getOperand(2);
6499 SDValue RHS = N->getOperand(3);
6500 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6501
6502 unsigned Opcode = 0;
6503 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006504 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006505 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006506 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006507 IsReversed = true ; // x CC y ? y : x
6508 } else {
6509 return SDValue();
6510 }
6511
Bob Wilsone742bb52010-02-24 22:15:53 +00006512 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006513 switch (CC) {
6514 default: break;
6515 case ISD::SETOLT:
6516 case ISD::SETOLE:
6517 case ISD::SETLT:
6518 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006519 case ISD::SETULT:
6520 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006521 // If LHS is NaN, an ordered comparison will be false and the result will
6522 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6523 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6524 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6525 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6526 break;
6527 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6528 // will return -0, so vmin can only be used for unsafe math or if one of
6529 // the operands is known to be nonzero.
6530 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6531 !UnsafeFPMath &&
6532 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6533 break;
6534 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006535 break;
6536
6537 case ISD::SETOGT:
6538 case ISD::SETOGE:
6539 case ISD::SETGT:
6540 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006541 case ISD::SETUGT:
6542 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006543 // If LHS is NaN, an ordered comparison will be false and the result will
6544 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6545 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6546 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6547 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6548 break;
6549 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6550 // will return +0, so vmax can only be used for unsafe math or if one of
6551 // the operands is known to be nonzero.
6552 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6553 !UnsafeFPMath &&
6554 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6555 break;
6556 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006557 break;
6558 }
6559
6560 if (!Opcode)
6561 return SDValue();
6562 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6563}
6564
Dan Gohman475871a2008-07-27 21:46:04 +00006565SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006566 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006567 switch (N->getOpcode()) {
6568 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006569 case ISD::ADD: return PerformADDCombine(N, DCI);
6570 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006571 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006572 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006573 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006574 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006575 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006576 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006577 case ISD::STORE: return PerformSTORECombine(N, DCI);
6578 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6579 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006580 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006581 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006582 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006583 case ISD::SHL:
6584 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006585 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006586 case ISD::SIGN_EXTEND:
6587 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006588 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6589 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006590 case ARMISD::VLD2DUP:
6591 case ARMISD::VLD3DUP:
6592 case ARMISD::VLD4DUP:
6593 return CombineBaseUpdate(N, DCI);
6594 case ISD::INTRINSIC_VOID:
6595 case ISD::INTRINSIC_W_CHAIN:
6596 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6597 case Intrinsic::arm_neon_vld1:
6598 case Intrinsic::arm_neon_vld2:
6599 case Intrinsic::arm_neon_vld3:
6600 case Intrinsic::arm_neon_vld4:
6601 case Intrinsic::arm_neon_vld2lane:
6602 case Intrinsic::arm_neon_vld3lane:
6603 case Intrinsic::arm_neon_vld4lane:
6604 case Intrinsic::arm_neon_vst1:
6605 case Intrinsic::arm_neon_vst2:
6606 case Intrinsic::arm_neon_vst3:
6607 case Intrinsic::arm_neon_vst4:
6608 case Intrinsic::arm_neon_vst2lane:
6609 case Intrinsic::arm_neon_vst3lane:
6610 case Intrinsic::arm_neon_vst4lane:
6611 return CombineBaseUpdate(N, DCI);
6612 default: break;
6613 }
6614 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006615 }
Dan Gohman475871a2008-07-27 21:46:04 +00006616 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006617}
6618
Evan Cheng31959b12011-02-02 01:06:55 +00006619bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6620 EVT VT) const {
6621 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6622}
6623
Bill Wendlingaf566342009-08-15 21:21:19 +00006624bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006625 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006626 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006627
6628 switch (VT.getSimpleVT().SimpleTy) {
6629 default:
6630 return false;
6631 case MVT::i8:
6632 case MVT::i16:
6633 case MVT::i32:
6634 return true;
6635 // FIXME: VLD1 etc with standard alignment is legal.
6636 }
6637}
6638
Evan Chenge6c835f2009-08-14 20:09:37 +00006639static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6640 if (V < 0)
6641 return false;
6642
6643 unsigned Scale = 1;
6644 switch (VT.getSimpleVT().SimpleTy) {
6645 default: return false;
6646 case MVT::i1:
6647 case MVT::i8:
6648 // Scale == 1;
6649 break;
6650 case MVT::i16:
6651 // Scale == 2;
6652 Scale = 2;
6653 break;
6654 case MVT::i32:
6655 // Scale == 4;
6656 Scale = 4;
6657 break;
6658 }
6659
6660 if ((V & (Scale - 1)) != 0)
6661 return false;
6662 V /= Scale;
6663 return V == (V & ((1LL << 5) - 1));
6664}
6665
6666static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6667 const ARMSubtarget *Subtarget) {
6668 bool isNeg = false;
6669 if (V < 0) {
6670 isNeg = true;
6671 V = - V;
6672 }
6673
6674 switch (VT.getSimpleVT().SimpleTy) {
6675 default: return false;
6676 case MVT::i1:
6677 case MVT::i8:
6678 case MVT::i16:
6679 case MVT::i32:
6680 // + imm12 or - imm8
6681 if (isNeg)
6682 return V == (V & ((1LL << 8) - 1));
6683 return V == (V & ((1LL << 12) - 1));
6684 case MVT::f32:
6685 case MVT::f64:
6686 // Same as ARM mode. FIXME: NEON?
6687 if (!Subtarget->hasVFP2())
6688 return false;
6689 if ((V & 3) != 0)
6690 return false;
6691 V >>= 2;
6692 return V == (V & ((1LL << 8) - 1));
6693 }
6694}
6695
Evan Chengb01fad62007-03-12 23:30:29 +00006696/// isLegalAddressImmediate - Return true if the integer value can be used
6697/// as the offset of the target addressing mode for load / store of the
6698/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006699static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006700 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006701 if (V == 0)
6702 return true;
6703
Evan Cheng65011532009-03-09 19:15:00 +00006704 if (!VT.isSimple())
6705 return false;
6706
Evan Chenge6c835f2009-08-14 20:09:37 +00006707 if (Subtarget->isThumb1Only())
6708 return isLegalT1AddressImmediate(V, VT);
6709 else if (Subtarget->isThumb2())
6710 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006711
Evan Chenge6c835f2009-08-14 20:09:37 +00006712 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006713 if (V < 0)
6714 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006715 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006716 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006717 case MVT::i1:
6718 case MVT::i8:
6719 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006720 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006721 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006722 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006723 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006724 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006725 case MVT::f32:
6726 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006727 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006728 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006729 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006730 return false;
6731 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006732 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006733 }
Evan Chenga8e29892007-01-19 07:51:42 +00006734}
6735
Evan Chenge6c835f2009-08-14 20:09:37 +00006736bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6737 EVT VT) const {
6738 int Scale = AM.Scale;
6739 if (Scale < 0)
6740 return false;
6741
6742 switch (VT.getSimpleVT().SimpleTy) {
6743 default: return false;
6744 case MVT::i1:
6745 case MVT::i8:
6746 case MVT::i16:
6747 case MVT::i32:
6748 if (Scale == 1)
6749 return true;
6750 // r + r << imm
6751 Scale = Scale & ~1;
6752 return Scale == 2 || Scale == 4 || Scale == 8;
6753 case MVT::i64:
6754 // r + r
6755 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6756 return true;
6757 return false;
6758 case MVT::isVoid:
6759 // Note, we allow "void" uses (basically, uses that aren't loads or
6760 // stores), because arm allows folding a scale into many arithmetic
6761 // operations. This should be made more precise and revisited later.
6762
6763 // Allow r << imm, but the imm has to be a multiple of two.
6764 if (Scale & 1) return false;
6765 return isPowerOf2_32(Scale);
6766 }
6767}
6768
Chris Lattner37caf8c2007-04-09 23:33:39 +00006769/// isLegalAddressingMode - Return true if the addressing mode represented
6770/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006771bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006772 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006773 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006774 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006775 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006776
Chris Lattner37caf8c2007-04-09 23:33:39 +00006777 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006778 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006779 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006780
Chris Lattner37caf8c2007-04-09 23:33:39 +00006781 switch (AM.Scale) {
6782 case 0: // no scale reg, must be "r+i" or "r", or "i".
6783 break;
6784 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006785 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006786 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006787 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006788 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006789 // ARM doesn't support any R+R*scale+imm addr modes.
6790 if (AM.BaseOffs)
6791 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006792
Bob Wilson2c7dab12009-04-08 17:55:28 +00006793 if (!VT.isSimple())
6794 return false;
6795
Evan Chenge6c835f2009-08-14 20:09:37 +00006796 if (Subtarget->isThumb2())
6797 return isLegalT2ScaledAddressingMode(AM, VT);
6798
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006799 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006800 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006801 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006802 case MVT::i1:
6803 case MVT::i8:
6804 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006805 if (Scale < 0) Scale = -Scale;
6806 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006807 return true;
6808 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006809 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006810 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006811 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006812 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006813 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006814 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006815 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006816
Owen Anderson825b72b2009-08-11 20:47:22 +00006817 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006818 // Note, we allow "void" uses (basically, uses that aren't loads or
6819 // stores), because arm allows folding a scale into many arithmetic
6820 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006821
Chris Lattner37caf8c2007-04-09 23:33:39 +00006822 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006823 if (Scale & 1) return false;
6824 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006825 }
6826 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006827 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006828 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006829}
6830
Evan Cheng77e47512009-11-11 19:05:52 +00006831/// isLegalICmpImmediate - Return true if the specified immediate is legal
6832/// icmp immediate, that is the target has icmp instructions which can compare
6833/// a register against the immediate without having to materialize the
6834/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006835bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006836 if (!Subtarget->isThumb())
6837 return ARM_AM::getSOImmVal(Imm) != -1;
6838 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006839 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006840 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006841}
6842
Owen Andersone50ed302009-08-10 22:56:29 +00006843static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006844 bool isSEXTLoad, SDValue &Base,
6845 SDValue &Offset, bool &isInc,
6846 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006847 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6848 return false;
6849
Owen Anderson825b72b2009-08-11 20:47:22 +00006850 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006851 // AddressingMode 3
6852 Base = Ptr->getOperand(0);
6853 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006854 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006855 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006856 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006857 isInc = false;
6858 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6859 return true;
6860 }
6861 }
6862 isInc = (Ptr->getOpcode() == ISD::ADD);
6863 Offset = Ptr->getOperand(1);
6864 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006865 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006866 // AddressingMode 2
6867 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006868 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006869 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006870 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006871 isInc = false;
6872 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6873 Base = Ptr->getOperand(0);
6874 return true;
6875 }
6876 }
6877
6878 if (Ptr->getOpcode() == ISD::ADD) {
6879 isInc = true;
6880 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6881 if (ShOpcVal != ARM_AM::no_shift) {
6882 Base = Ptr->getOperand(1);
6883 Offset = Ptr->getOperand(0);
6884 } else {
6885 Base = Ptr->getOperand(0);
6886 Offset = Ptr->getOperand(1);
6887 }
6888 return true;
6889 }
6890
6891 isInc = (Ptr->getOpcode() == ISD::ADD);
6892 Base = Ptr->getOperand(0);
6893 Offset = Ptr->getOperand(1);
6894 return true;
6895 }
6896
Jim Grosbache5165492009-11-09 00:11:35 +00006897 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006898 return false;
6899}
6900
Owen Andersone50ed302009-08-10 22:56:29 +00006901static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006902 bool isSEXTLoad, SDValue &Base,
6903 SDValue &Offset, bool &isInc,
6904 SelectionDAG &DAG) {
6905 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6906 return false;
6907
6908 Base = Ptr->getOperand(0);
6909 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6910 int RHSC = (int)RHS->getZExtValue();
6911 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6912 assert(Ptr->getOpcode() == ISD::ADD);
6913 isInc = false;
6914 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6915 return true;
6916 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6917 isInc = Ptr->getOpcode() == ISD::ADD;
6918 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6919 return true;
6920 }
6921 }
6922
6923 return false;
6924}
6925
Evan Chenga8e29892007-01-19 07:51:42 +00006926/// getPreIndexedAddressParts - returns true by value, base pointer and
6927/// offset pointer and addressing mode by reference if the node's address
6928/// can be legally represented as pre-indexed load / store address.
6929bool
Dan Gohman475871a2008-07-27 21:46:04 +00006930ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6931 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006932 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006933 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006934 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006935 return false;
6936
Owen Andersone50ed302009-08-10 22:56:29 +00006937 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006938 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006939 bool isSEXTLoad = false;
6940 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6941 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006942 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006943 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6944 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6945 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006946 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006947 } else
6948 return false;
6949
6950 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006951 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006952 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006953 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6954 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006955 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006956 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006957 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006958 if (!isLegal)
6959 return false;
6960
6961 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6962 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006963}
6964
6965/// getPostIndexedAddressParts - returns true by value, base pointer and
6966/// offset pointer and addressing mode by reference if this node can be
6967/// combined with a load / store to form a post-indexed load / store.
6968bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006969 SDValue &Base,
6970 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006971 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006972 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006973 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006974 return false;
6975
Owen Andersone50ed302009-08-10 22:56:29 +00006976 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006977 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006978 bool isSEXTLoad = false;
6979 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006980 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006981 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006982 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6983 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006984 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006985 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006986 } else
6987 return false;
6988
6989 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006990 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006991 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006992 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006993 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006994 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006995 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6996 isInc, DAG);
6997 if (!isLegal)
6998 return false;
6999
Evan Cheng28dad2a2010-05-18 21:31:17 +00007000 if (Ptr != Base) {
7001 // Swap base ptr and offset to catch more post-index load / store when
7002 // it's legal. In Thumb2 mode, offset must be an immediate.
7003 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7004 !Subtarget->isThumb2())
7005 std::swap(Base, Offset);
7006
7007 // Post-indexed load / store update the base pointer.
7008 if (Ptr != Base)
7009 return false;
7010 }
7011
Evan Chenge88d5ce2009-07-02 07:28:31 +00007012 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7013 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007014}
7015
Dan Gohman475871a2008-07-27 21:46:04 +00007016void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007017 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007018 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007019 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007020 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007021 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007022 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007023 switch (Op.getOpcode()) {
7024 default: break;
7025 case ARMISD::CMOV: {
7026 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007027 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007028 if (KnownZero == 0 && KnownOne == 0) return;
7029
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007030 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007031 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7032 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007033 KnownZero &= KnownZeroRHS;
7034 KnownOne &= KnownOneRHS;
7035 return;
7036 }
7037 }
7038}
7039
7040//===----------------------------------------------------------------------===//
7041// ARM Inline Assembly Support
7042//===----------------------------------------------------------------------===//
7043
Evan Cheng55d42002011-01-08 01:24:27 +00007044bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7045 // Looking for "rev" which is V6+.
7046 if (!Subtarget->hasV6Ops())
7047 return false;
7048
7049 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7050 std::string AsmStr = IA->getAsmString();
7051 SmallVector<StringRef, 4> AsmPieces;
7052 SplitString(AsmStr, AsmPieces, ";\n");
7053
7054 switch (AsmPieces.size()) {
7055 default: return false;
7056 case 1:
7057 AsmStr = AsmPieces[0];
7058 AsmPieces.clear();
7059 SplitString(AsmStr, AsmPieces, " \t,");
7060
7061 // rev $0, $1
7062 if (AsmPieces.size() == 3 &&
7063 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7064 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7065 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7066 if (Ty && Ty->getBitWidth() == 32)
7067 return IntrinsicLowering::LowerToByteSwap(CI);
7068 }
7069 break;
7070 }
7071
7072 return false;
7073}
7074
Evan Chenga8e29892007-01-19 07:51:42 +00007075/// getConstraintType - Given a constraint letter, return the type of
7076/// constraint it is for this target.
7077ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007078ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7079 if (Constraint.size() == 1) {
7080 switch (Constraint[0]) {
7081 default: break;
7082 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007083 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00007084 }
Evan Chenga8e29892007-01-19 07:51:42 +00007085 }
Chris Lattner4234f572007-03-25 02:14:49 +00007086 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007087}
7088
John Thompson44ab89e2010-10-29 17:29:13 +00007089/// Examine constraint type and operand type and determine a weight value.
7090/// This object must already have been set up with the operand type
7091/// and the current alternative constraint selected.
7092TargetLowering::ConstraintWeight
7093ARMTargetLowering::getSingleConstraintMatchWeight(
7094 AsmOperandInfo &info, const char *constraint) const {
7095 ConstraintWeight weight = CW_Invalid;
7096 Value *CallOperandVal = info.CallOperandVal;
7097 // If we don't have a value, we can't do a match,
7098 // but allow it at the lowest weight.
7099 if (CallOperandVal == NULL)
7100 return CW_Default;
7101 const Type *type = CallOperandVal->getType();
7102 // Look at the constraint type.
7103 switch (*constraint) {
7104 default:
7105 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7106 break;
7107 case 'l':
7108 if (type->isIntegerTy()) {
7109 if (Subtarget->isThumb())
7110 weight = CW_SpecificReg;
7111 else
7112 weight = CW_Register;
7113 }
7114 break;
7115 case 'w':
7116 if (type->isFloatingPointTy())
7117 weight = CW_Register;
7118 break;
7119 }
7120 return weight;
7121}
7122
Bob Wilson2dc4f542009-03-20 22:42:55 +00007123std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007124ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007125 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007126 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007127 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007128 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007129 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007130 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007131 return std::make_pair(0U, ARM::tGPRRegisterClass);
7132 else
7133 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007134 case 'r':
7135 return std::make_pair(0U, ARM::GPRRegisterClass);
7136 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007138 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007139 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007140 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007141 if (VT.getSizeInBits() == 128)
7142 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007143 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007144 }
7145 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007146 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007147 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007148
Evan Chenga8e29892007-01-19 07:51:42 +00007149 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7150}
7151
7152std::vector<unsigned> ARMTargetLowering::
7153getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007154 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007155 if (Constraint.size() != 1)
7156 return std::vector<unsigned>();
7157
7158 switch (Constraint[0]) { // GCC ARM Constraint Letters
7159 default: break;
7160 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007161 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7162 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7163 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007164 case 'r':
7165 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7166 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7167 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7168 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007169 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007170 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007171 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7172 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7173 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7174 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7175 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7176 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7177 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7178 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007179 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007180 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7181 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7182 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7183 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007184 if (VT.getSizeInBits() == 128)
7185 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7186 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007187 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007188 }
7189
7190 return std::vector<unsigned>();
7191}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007192
7193/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7194/// vector. If it is invalid, don't add anything to Ops.
7195void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
7196 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007197 std::vector<SDValue>&Ops,
7198 SelectionDAG &DAG) const {
7199 SDValue Result(0, 0);
7200
7201 switch (Constraint) {
7202 default: break;
7203 case 'I': case 'J': case 'K': case 'L':
7204 case 'M': case 'N': case 'O':
7205 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7206 if (!C)
7207 return;
7208
7209 int64_t CVal64 = C->getSExtValue();
7210 int CVal = (int) CVal64;
7211 // None of these constraints allow values larger than 32 bits. Check
7212 // that the value fits in an int.
7213 if (CVal != CVal64)
7214 return;
7215
7216 switch (Constraint) {
7217 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007218 if (Subtarget->isThumb1Only()) {
7219 // This must be a constant between 0 and 255, for ADD
7220 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007221 if (CVal >= 0 && CVal <= 255)
7222 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007223 } else if (Subtarget->isThumb2()) {
7224 // A constant that can be used as an immediate value in a
7225 // data-processing instruction.
7226 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7227 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007228 } else {
7229 // A constant that can be used as an immediate value in a
7230 // data-processing instruction.
7231 if (ARM_AM::getSOImmVal(CVal) != -1)
7232 break;
7233 }
7234 return;
7235
7236 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007237 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007238 // This must be a constant between -255 and -1, for negated ADD
7239 // immediates. This can be used in GCC with an "n" modifier that
7240 // prints the negated value, for use with SUB instructions. It is
7241 // not useful otherwise but is implemented for compatibility.
7242 if (CVal >= -255 && CVal <= -1)
7243 break;
7244 } else {
7245 // This must be a constant between -4095 and 4095. It is not clear
7246 // what this constraint is intended for. Implemented for
7247 // compatibility with GCC.
7248 if (CVal >= -4095 && CVal <= 4095)
7249 break;
7250 }
7251 return;
7252
7253 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007254 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007255 // A 32-bit value where only one byte has a nonzero value. Exclude
7256 // zero to match GCC. This constraint is used by GCC internally for
7257 // constants that can be loaded with a move/shift combination.
7258 // It is not useful otherwise but is implemented for compatibility.
7259 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7260 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007261 } else if (Subtarget->isThumb2()) {
7262 // A constant whose bitwise inverse can be used as an immediate
7263 // value in a data-processing instruction. This can be used in GCC
7264 // with a "B" modifier that prints the inverted value, for use with
7265 // BIC and MVN instructions. It is not useful otherwise but is
7266 // implemented for compatibility.
7267 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7268 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007269 } else {
7270 // A constant whose bitwise inverse can be used as an immediate
7271 // value in a data-processing instruction. This can be used in GCC
7272 // with a "B" modifier that prints the inverted value, for use with
7273 // BIC and MVN instructions. It is not useful otherwise but is
7274 // implemented for compatibility.
7275 if (ARM_AM::getSOImmVal(~CVal) != -1)
7276 break;
7277 }
7278 return;
7279
7280 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007281 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007282 // This must be a constant between -7 and 7,
7283 // for 3-operand ADD/SUB immediate instructions.
7284 if (CVal >= -7 && CVal < 7)
7285 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007286 } else if (Subtarget->isThumb2()) {
7287 // A constant whose negation can be used as an immediate value in a
7288 // data-processing instruction. This can be used in GCC with an "n"
7289 // modifier that prints the negated value, for use with SUB
7290 // instructions. It is not useful otherwise but is implemented for
7291 // compatibility.
7292 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7293 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007294 } else {
7295 // A constant whose negation can be used as an immediate value in a
7296 // data-processing instruction. This can be used in GCC with an "n"
7297 // modifier that prints the negated value, for use with SUB
7298 // instructions. It is not useful otherwise but is implemented for
7299 // compatibility.
7300 if (ARM_AM::getSOImmVal(-CVal) != -1)
7301 break;
7302 }
7303 return;
7304
7305 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007306 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007307 // This must be a multiple of 4 between 0 and 1020, for
7308 // ADD sp + immediate.
7309 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7310 break;
7311 } else {
7312 // A power of two or a constant between 0 and 32. This is used in
7313 // GCC for the shift amount on shifted register operands, but it is
7314 // useful in general for any shift amounts.
7315 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7316 break;
7317 }
7318 return;
7319
7320 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007321 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007322 // This must be a constant between 0 and 31, for shift amounts.
7323 if (CVal >= 0 && CVal <= 31)
7324 break;
7325 }
7326 return;
7327
7328 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007329 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007330 // This must be a multiple of 4 between -508 and 508, for
7331 // ADD/SUB sp = sp + immediate.
7332 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7333 break;
7334 }
7335 return;
7336 }
7337 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7338 break;
7339 }
7340
7341 if (Result.getNode()) {
7342 Ops.push_back(Result);
7343 return;
7344 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007345 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007346}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007347
7348bool
7349ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7350 // The ARM target isn't yet aware of offsets.
7351 return false;
7352}
Evan Cheng39382422009-10-28 01:44:26 +00007353
7354int ARM::getVFPf32Imm(const APFloat &FPImm) {
7355 APInt Imm = FPImm.bitcastToAPInt();
7356 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7357 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7358 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7359
7360 // We can handle 4 bits of mantissa.
7361 // mantissa = (16+UInt(e:f:g:h))/16.
7362 if (Mantissa & 0x7ffff)
7363 return -1;
7364 Mantissa >>= 19;
7365 if ((Mantissa & 0xf) != Mantissa)
7366 return -1;
7367
7368 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7369 if (Exp < -3 || Exp > 4)
7370 return -1;
7371 Exp = ((Exp+3) & 0x7) ^ 4;
7372
7373 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7374}
7375
7376int ARM::getVFPf64Imm(const APFloat &FPImm) {
7377 APInt Imm = FPImm.bitcastToAPInt();
7378 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7379 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7380 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7381
7382 // We can handle 4 bits of mantissa.
7383 // mantissa = (16+UInt(e:f:g:h))/16.
7384 if (Mantissa & 0xffffffffffffLL)
7385 return -1;
7386 Mantissa >>= 48;
7387 if ((Mantissa & 0xf) != Mantissa)
7388 return -1;
7389
7390 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7391 if (Exp < -3 || Exp > 4)
7392 return -1;
7393 Exp = ((Exp+3) & 0x7) ^ 4;
7394
7395 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7396}
7397
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007398bool ARM::isBitFieldInvertedMask(unsigned v) {
7399 if (v == 0xffffffff)
7400 return 0;
7401 // there can be 1's on either or both "outsides", all the "inside"
7402 // bits must be 0's
7403 unsigned int lsb = 0, msb = 31;
7404 while (v & (1 << msb)) --msb;
7405 while (v & (1 << lsb)) ++lsb;
7406 for (unsigned int i = lsb; i <= msb; ++i) {
7407 if (v & (1 << i))
7408 return 0;
7409 }
7410 return 1;
7411}
7412
Evan Cheng39382422009-10-28 01:44:26 +00007413/// isFPImmLegal - Returns true if the target can instruction select the
7414/// specified FP immediate natively. If false, the legalizer will
7415/// materialize the FP immediate as a load from a constant pool.
7416bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7417 if (!Subtarget->hasVFP3())
7418 return false;
7419 if (VT == MVT::f32)
7420 return ARM::getVFPf32Imm(Imm) != -1;
7421 if (VT == MVT::f64)
7422 return ARM::getVFPf64Imm(Imm) != -1;
7423 return false;
7424}
Bob Wilson65ffec42010-09-21 17:56:22 +00007425
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007426/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007427/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7428/// specified in the intrinsic calls.
7429bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7430 const CallInst &I,
7431 unsigned Intrinsic) const {
7432 switch (Intrinsic) {
7433 case Intrinsic::arm_neon_vld1:
7434 case Intrinsic::arm_neon_vld2:
7435 case Intrinsic::arm_neon_vld3:
7436 case Intrinsic::arm_neon_vld4:
7437 case Intrinsic::arm_neon_vld2lane:
7438 case Intrinsic::arm_neon_vld3lane:
7439 case Intrinsic::arm_neon_vld4lane: {
7440 Info.opc = ISD::INTRINSIC_W_CHAIN;
7441 // Conservatively set memVT to the entire set of vectors loaded.
7442 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7443 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7444 Info.ptrVal = I.getArgOperand(0);
7445 Info.offset = 0;
7446 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7447 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7448 Info.vol = false; // volatile loads with NEON intrinsics not supported
7449 Info.readMem = true;
7450 Info.writeMem = false;
7451 return true;
7452 }
7453 case Intrinsic::arm_neon_vst1:
7454 case Intrinsic::arm_neon_vst2:
7455 case Intrinsic::arm_neon_vst3:
7456 case Intrinsic::arm_neon_vst4:
7457 case Intrinsic::arm_neon_vst2lane:
7458 case Intrinsic::arm_neon_vst3lane:
7459 case Intrinsic::arm_neon_vst4lane: {
7460 Info.opc = ISD::INTRINSIC_VOID;
7461 // Conservatively set memVT to the entire set of vectors stored.
7462 unsigned NumElts = 0;
7463 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7464 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7465 if (!ArgTy->isVectorTy())
7466 break;
7467 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7468 }
7469 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7470 Info.ptrVal = I.getArgOperand(0);
7471 Info.offset = 0;
7472 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7473 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7474 Info.vol = false; // volatile stores with NEON intrinsics not supported
7475 Info.readMem = false;
7476 Info.writeMem = true;
7477 return true;
7478 }
7479 default:
7480 break;
7481 }
7482
7483 return false;
7484}