blob: 50dd4091070739689715c6edaf5b4c7df942be09 [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 Cheng2c69f8e2011-04-07 00:58:44 +0000401 if (HasDivModLibcall) {
Evan Cheng8e23e812011-04-01 00:42:02 +0000402 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
403 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
404 }
405
David Goodwinf1daf7d2009-07-08 23:10:31 +0000406 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000408 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000410 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000412 if (!Subtarget->isFPOnlySP())
413 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000414
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000416 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000417
418 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 addDRTypeForNEON(MVT::v2f32);
420 addDRTypeForNEON(MVT::v8i8);
421 addDRTypeForNEON(MVT::v4i16);
422 addDRTypeForNEON(MVT::v2i32);
423 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000424
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addQRTypeForNEON(MVT::v4f32);
426 addQRTypeForNEON(MVT::v2f64);
427 addQRTypeForNEON(MVT::v16i8);
428 addQRTypeForNEON(MVT::v8i16);
429 addQRTypeForNEON(MVT::v4i32);
430 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000431
Bob Wilson74dc72e2009-09-15 23:55:57 +0000432 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
433 // neither Neon nor VFP support any arithmetic operations on it.
434 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
435 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
436 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
437 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
438 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
439 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
440 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
441 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
442 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
443 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
444 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
445 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
446 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
447 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
448 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
449 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
450 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
451 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
452 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
453 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
454 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
455 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
456 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
457 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
458
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000459 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
460
Bob Wilson642b3292009-09-16 00:32:15 +0000461 // Neon does not support some operations on v1i64 and v2i64 types.
462 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000463 // Custom handling for some quad-vector types to detect VMULL.
464 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
465 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
466 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000467 // Custom handling for some vector types to avoid expensive expansions
468 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
469 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
470 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
471 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000472 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
473 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000474 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
475 // a destination type that is wider than the source.
476 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
477 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000478
Bob Wilson1c3ef902011-02-07 17:43:21 +0000479 setTargetDAGCombine(ISD::INTRINSIC_VOID);
480 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000481 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
482 setTargetDAGCombine(ISD::SHL);
483 setTargetDAGCombine(ISD::SRL);
484 setTargetDAGCombine(ISD::SRA);
485 setTargetDAGCombine(ISD::SIGN_EXTEND);
486 setTargetDAGCombine(ISD::ZERO_EXTEND);
487 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000488 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000489 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000490 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000491 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
492 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000493 }
494
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000495 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000496
497 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000499
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000500 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000502
Evan Chenga8e29892007-01-19 07:51:42 +0000503 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000504 if (!Subtarget->isThumb1Only()) {
505 for (unsigned im = (unsigned)ISD::PRE_INC;
506 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setIndexedLoadAction(im, MVT::i1, Legal);
508 setIndexedLoadAction(im, MVT::i8, Legal);
509 setIndexedLoadAction(im, MVT::i16, Legal);
510 setIndexedLoadAction(im, MVT::i32, Legal);
511 setIndexedStoreAction(im, MVT::i1, Legal);
512 setIndexedStoreAction(im, MVT::i8, Legal);
513 setIndexedStoreAction(im, MVT::i16, Legal);
514 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000515 }
Evan Chenga8e29892007-01-19 07:51:42 +0000516 }
517
518 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000519 setOperationAction(ISD::MUL, MVT::i64, Expand);
520 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000521 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
523 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000524 }
Eric Christopher2cc40132011-04-19 18:49:19 +0000525 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops())
526 setOperationAction(ISD::MULHS, MVT::i32, Expand);
527
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000528 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000529 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000530 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::SRL, MVT::i64, Custom);
532 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000533
534 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000536 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000538 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000540
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000541 // Only ARMv6 has BSWAP.
542 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000544
Evan Chenga8e29892007-01-19 07:51:42 +0000545 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000546 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000547 // v7M has a hardware divider
548 setOperationAction(ISD::SDIV, MVT::i32, Expand);
549 setOperationAction(ISD::UDIV, MVT::i32, Expand);
550 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000551 setOperationAction(ISD::SREM, MVT::i32, Expand);
552 setOperationAction(ISD::UREM, MVT::i32, Expand);
553 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
554 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000555
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
557 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
558 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
559 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000560 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000561
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000562 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000563
Evan Chenga8e29892007-01-19 07:51:42 +0000564 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::VASTART, MVT::Other, Custom);
566 setOperationAction(ISD::VAARG, MVT::Other, Expand);
567 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
568 setOperationAction(ISD::VAEND, MVT::Other, Expand);
569 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
570 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000571 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000572 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
573 setExceptionPointerRegister(ARM::R0);
574 setExceptionSelectorRegister(ARM::R1);
575
Evan Cheng3a1588a2010-04-15 22:20:34 +0000576 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000577 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
578 // the default expansion.
579 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000580 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000581 // membarrier needs custom lowering; the rest are legal and handled
582 // normally.
583 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
584 } else {
585 // Set them all for expansion, which will force libcalls.
586 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
587 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
588 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
589 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000590 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
591 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
592 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000593 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
594 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
595 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
596 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
606 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
607 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
608 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
609 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000611 // Since the libcalls include locking, fold in the fences
612 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000613 }
614 // 64-bit versions are always libcalls (for now)
615 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000616 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000617 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000623
Evan Cheng416941d2010-11-04 05:19:35 +0000624 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000625
Eli Friedmana2c6f452010-06-26 04:36:50 +0000626 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
627 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
629 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000630 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000632
Nate Begemand1fb5832010-08-03 21:31:55 +0000633 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000634 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
635 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000636 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000637 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
638 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000639
640 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000642 if (Subtarget->isTargetDarwin()) {
643 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
644 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000645 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000646 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::SETCC, MVT::i32, Expand);
649 setOperationAction(ISD::SETCC, MVT::f32, Expand);
650 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000651 setOperationAction(ISD::SELECT, MVT::i32, Custom);
652 setOperationAction(ISD::SELECT, MVT::f32, Custom);
653 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
655 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
656 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000657
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
659 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
660 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
661 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
662 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000663
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000664 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FSIN, MVT::f64, Expand);
666 setOperationAction(ISD::FSIN, MVT::f32, Expand);
667 setOperationAction(ISD::FCOS, MVT::f32, Expand);
668 setOperationAction(ISD::FCOS, MVT::f64, Expand);
669 setOperationAction(ISD::FREM, MVT::f64, Expand);
670 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000671 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
673 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000674 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::FPOW, MVT::f64, Expand);
676 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000677
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000678 // Various VFP goodness
679 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000680 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
681 if (Subtarget->hasVFP2()) {
682 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
683 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
684 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
685 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
686 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000687 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000688 if (!Subtarget->hasFP16()) {
689 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
690 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000691 }
Evan Cheng110cf482008-04-01 01:50:16 +0000692 }
Evan Chenga8e29892007-01-19 07:51:42 +0000693
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000694 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000695 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000696 setTargetDAGCombine(ISD::ADD);
697 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000698 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000699
Owen Anderson080c0922010-11-05 19:27:46 +0000700 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000701 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000702 if (Subtarget->hasNEON())
703 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000704
Evan Chenga8e29892007-01-19 07:51:42 +0000705 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000706
Evan Chengf7d87ee2010-05-21 00:43:17 +0000707 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
708 setSchedulingPreference(Sched::RegPressure);
709 else
710 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000711
Evan Cheng05219282011-01-06 06:52:41 +0000712 //// temporary - rewrite interface to use type
713 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000714
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000715 // On ARM arguments smaller than 4 bytes are extended, so all arguments
716 // are at least 4 bytes aligned.
717 setMinStackArgumentAlignment(4);
718
Evan Chengfff606d2010-09-24 19:07:23 +0000719 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000720}
721
Andrew Trick32cec0a2011-01-19 02:35:27 +0000722// FIXME: It might make sense to define the representative register class as the
723// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
724// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
725// SPR's representative would be DPR_VFP2. This should work well if register
726// pressure tracking were modified such that a register use would increment the
727// pressure of the register class's representative and all of it's super
728// classes' representatives transitively. We have not implemented this because
729// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000730// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000731// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000732std::pair<const TargetRegisterClass*, uint8_t>
733ARMTargetLowering::findRepresentativeClass(EVT VT) const{
734 const TargetRegisterClass *RRC = 0;
735 uint8_t Cost = 1;
736 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000737 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000738 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000739 // Use DPR as representative register class for all floating point
740 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
741 // the cost is 1 for both f32 and f64.
742 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000743 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000744 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000745 // When NEON is used for SP, only half of the register file is available
746 // because operations that define both SP and DP results will be constrained
747 // to the VFP2 class (D0-D15). We currently model this constraint prior to
748 // coalescing by double-counting the SP regs. See the FIXME above.
749 if (Subtarget->useNEONForSinglePrecisionFP())
750 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000751 break;
752 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
753 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000754 RRC = ARM::DPRRegisterClass;
755 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000756 break;
757 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000758 RRC = ARM::DPRRegisterClass;
759 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000760 break;
761 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000762 RRC = ARM::DPRRegisterClass;
763 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000764 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000765 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000766 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000767}
768
Evan Chenga8e29892007-01-19 07:51:42 +0000769const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
770 switch (Opcode) {
771 default: return 0;
772 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000773 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000774 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000775 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
776 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000777 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000778 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
779 case ARMISD::tCALL: return "ARMISD::tCALL";
780 case ARMISD::BRCOND: return "ARMISD::BRCOND";
781 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000782 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000783 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
784 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
785 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000786 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000787 case ARMISD::CMPFP: return "ARMISD::CMPFP";
788 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000789 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000790 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
791 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000792
Jim Grosbach3482c802010-01-18 19:58:49 +0000793 case ARMISD::RBIT: return "ARMISD::RBIT";
794
Bob Wilson76a312b2010-03-19 22:51:32 +0000795 case ARMISD::FTOSI: return "ARMISD::FTOSI";
796 case ARMISD::FTOUI: return "ARMISD::FTOUI";
797 case ARMISD::SITOF: return "ARMISD::SITOF";
798 case ARMISD::UITOF: return "ARMISD::UITOF";
799
Evan Chenga8e29892007-01-19 07:51:42 +0000800 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
801 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
802 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000803
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000804 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
805 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000806
Evan Chengc5942082009-10-28 06:55:03 +0000807 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
808 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000809 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000810
Dale Johannesen51e28e62010-06-03 21:09:53 +0000811 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000812
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000813 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000814
Evan Cheng86198642009-08-07 00:34:42 +0000815 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
816
Jim Grosbach3728e962009-12-10 00:11:09 +0000817 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000818 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000819
Evan Chengdfed19f2010-11-03 06:34:55 +0000820 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
821
Bob Wilson5bafff32009-06-22 23:27:02 +0000822 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000823 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000824 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000825 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
826 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000827 case ARMISD::VCGEU: return "ARMISD::VCGEU";
828 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000829 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
830 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000831 case ARMISD::VCGTU: return "ARMISD::VCGTU";
832 case ARMISD::VTST: return "ARMISD::VTST";
833
834 case ARMISD::VSHL: return "ARMISD::VSHL";
835 case ARMISD::VSHRs: return "ARMISD::VSHRs";
836 case ARMISD::VSHRu: return "ARMISD::VSHRu";
837 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
838 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
839 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
840 case ARMISD::VSHRN: return "ARMISD::VSHRN";
841 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
842 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
843 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
844 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
845 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
846 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
847 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
848 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
849 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
850 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
851 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
852 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
853 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
854 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000855 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000856 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000857 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000858 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000859 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000860 case ARMISD::VREV64: return "ARMISD::VREV64";
861 case ARMISD::VREV32: return "ARMISD::VREV32";
862 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000863 case ARMISD::VZIP: return "ARMISD::VZIP";
864 case ARMISD::VUZP: return "ARMISD::VUZP";
865 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000866 case ARMISD::VTBL1: return "ARMISD::VTBL1";
867 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000868 case ARMISD::VMULLs: return "ARMISD::VMULLs";
869 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000870 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000871 case ARMISD::FMAX: return "ARMISD::FMAX";
872 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000873 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000874 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
875 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000876 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000877 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
878 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
879 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000880 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
881 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
882 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
883 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
884 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
885 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
886 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
887 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
888 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
889 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
890 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
891 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
892 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
893 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
894 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
895 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
896 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000897 }
898}
899
Evan Cheng06b666c2010-05-15 02:18:07 +0000900/// getRegClassFor - Return the register class that should be used for the
901/// specified value type.
902TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
903 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
904 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
905 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000906 if (Subtarget->hasNEON()) {
907 if (VT == MVT::v4i64)
908 return ARM::QQPRRegisterClass;
909 else if (VT == MVT::v8i64)
910 return ARM::QQQQPRRegisterClass;
911 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000912 return TargetLowering::getRegClassFor(VT);
913}
914
Eric Christopherab695882010-07-21 22:26:11 +0000915// Create a fast isel object.
916FastISel *
917ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
918 return ARM::createFastISel(funcInfo);
919}
920
Bill Wendlingb4202b82009-07-01 18:50:55 +0000921/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000922unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000923 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000924}
925
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000926/// getMaximalGlobalOffset - Returns the maximal possible offset which can
927/// be used for loads / stores from the global.
928unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
929 return (Subtarget->isThumb1Only() ? 127 : 4095);
930}
931
Evan Cheng1cc39842010-05-20 23:26:43 +0000932Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000933 unsigned NumVals = N->getNumValues();
934 if (!NumVals)
935 return Sched::RegPressure;
936
937 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000938 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000939 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000940 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000941 if (VT.isFloatingPoint() || VT.isVector())
942 return Sched::Latency;
943 }
Evan Chengc10f5432010-05-28 23:25:23 +0000944
945 if (!N->isMachineOpcode())
946 return Sched::RegPressure;
947
948 // Load are scheduled for latency even if there instruction itinerary
949 // is not available.
950 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
951 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000952
953 if (TID.getNumDefs() == 0)
954 return Sched::RegPressure;
955 if (!Itins->isEmpty() &&
956 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000957 return Sched::Latency;
958
Evan Cheng1cc39842010-05-20 23:26:43 +0000959 return Sched::RegPressure;
960}
961
Evan Chenga8e29892007-01-19 07:51:42 +0000962//===----------------------------------------------------------------------===//
963// Lowering Code
964//===----------------------------------------------------------------------===//
965
Evan Chenga8e29892007-01-19 07:51:42 +0000966/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
967static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
968 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000969 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000970 case ISD::SETNE: return ARMCC::NE;
971 case ISD::SETEQ: return ARMCC::EQ;
972 case ISD::SETGT: return ARMCC::GT;
973 case ISD::SETGE: return ARMCC::GE;
974 case ISD::SETLT: return ARMCC::LT;
975 case ISD::SETLE: return ARMCC::LE;
976 case ISD::SETUGT: return ARMCC::HI;
977 case ISD::SETUGE: return ARMCC::HS;
978 case ISD::SETULT: return ARMCC::LO;
979 case ISD::SETULE: return ARMCC::LS;
980 }
981}
982
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000983/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
984static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000985 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000986 CondCode2 = ARMCC::AL;
987 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000988 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000989 case ISD::SETEQ:
990 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
991 case ISD::SETGT:
992 case ISD::SETOGT: CondCode = ARMCC::GT; break;
993 case ISD::SETGE:
994 case ISD::SETOGE: CondCode = ARMCC::GE; break;
995 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000996 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000997 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
998 case ISD::SETO: CondCode = ARMCC::VC; break;
999 case ISD::SETUO: CondCode = ARMCC::VS; break;
1000 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1001 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1002 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1003 case ISD::SETLT:
1004 case ISD::SETULT: CondCode = ARMCC::LT; break;
1005 case ISD::SETLE:
1006 case ISD::SETULE: CondCode = ARMCC::LE; break;
1007 case ISD::SETNE:
1008 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1009 }
Evan Chenga8e29892007-01-19 07:51:42 +00001010}
1011
Bob Wilson1f595bb2009-04-17 19:07:39 +00001012//===----------------------------------------------------------------------===//
1013// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001014//===----------------------------------------------------------------------===//
1015
1016#include "ARMGenCallingConv.inc"
1017
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001018/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1019/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001020CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001021 bool Return,
1022 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001023 switch (CC) {
1024 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001025 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001026 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001027 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001028 if (!Subtarget->isAAPCS_ABI())
1029 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1030 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1031 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1032 }
1033 // Fallthrough
1034 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001035 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001036 if (!Subtarget->isAAPCS_ABI())
1037 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1038 else if (Subtarget->hasVFP2() &&
1039 FloatABIType == FloatABI::Hard && !isVarArg)
1040 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1041 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1042 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001043 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001044 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001045 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001046 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001047 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001048 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001049 }
1050}
1051
Dan Gohman98ca4f22009-08-05 01:29:28 +00001052/// LowerCallResult - Lower the result values of a call into the
1053/// appropriate copies out of appropriate physical registers.
1054SDValue
1055ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001056 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001057 const SmallVectorImpl<ISD::InputArg> &Ins,
1058 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001059 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001060
Bob Wilson1f595bb2009-04-17 19:07:39 +00001061 // Assign locations to each value returned by this call.
1062 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001063 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001064 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001065 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001066 CCAssignFnForNode(CallConv, /* Return*/ true,
1067 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001068
1069 // Copy all of the result registers out of their specified physreg.
1070 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1071 CCValAssign VA = RVLocs[i];
1072
Bob Wilson80915242009-04-25 00:33:20 +00001073 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001074 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001075 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001076 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001077 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001078 Chain = Lo.getValue(1);
1079 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001080 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001081 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001082 InFlag);
1083 Chain = Hi.getValue(1);
1084 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001085 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001086
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 if (VA.getLocVT() == MVT::v2f64) {
1088 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1089 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1090 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001091
1092 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001093 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001094 Chain = Lo.getValue(1);
1095 InFlag = Lo.getValue(2);
1096 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001097 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001098 Chain = Hi.getValue(1);
1099 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001100 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001101 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1102 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001103 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001105 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1106 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001107 Chain = Val.getValue(1);
1108 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109 }
Bob Wilson80915242009-04-25 00:33:20 +00001110
1111 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001112 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001113 case CCValAssign::Full: break;
1114 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001115 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001116 break;
1117 }
1118
Dan Gohman98ca4f22009-08-05 01:29:28 +00001119 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001120 }
1121
Dan Gohman98ca4f22009-08-05 01:29:28 +00001122 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001123}
1124
Bob Wilsondee46d72009-04-17 20:35:10 +00001125/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001127ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1128 SDValue StackPtr, SDValue Arg,
1129 DebugLoc dl, SelectionDAG &DAG,
1130 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001131 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132 unsigned LocMemOffset = VA.getLocMemOffset();
1133 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1134 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001136 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001137 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001138}
1139
Dan Gohman98ca4f22009-08-05 01:29:28 +00001140void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001141 SDValue Chain, SDValue &Arg,
1142 RegsToPassVector &RegsToPass,
1143 CCValAssign &VA, CCValAssign &NextVA,
1144 SDValue &StackPtr,
1145 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001146 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001147
Jim Grosbache5165492009-11-09 00:11:35 +00001148 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001150 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1151
1152 if (NextVA.isRegLoc())
1153 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1154 else {
1155 assert(NextVA.isMemLoc());
1156 if (StackPtr.getNode() == 0)
1157 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1158
Dan Gohman98ca4f22009-08-05 01:29:28 +00001159 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1160 dl, DAG, NextVA,
1161 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001162 }
1163}
1164
Dan Gohman98ca4f22009-08-05 01:29:28 +00001165/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001166/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1167/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001168SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001169ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001170 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001171 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001172 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001173 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001174 const SmallVectorImpl<ISD::InputArg> &Ins,
1175 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001176 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001177 MachineFunction &MF = DAG.getMachineFunction();
1178 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1179 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001180 // Temporarily disable tail calls so things don't break.
1181 if (!EnableARMTailCalls)
1182 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001183 if (isTailCall) {
1184 // Check if it's really possible to do a tail call.
1185 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1186 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001187 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001188 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1189 // detected sibcalls.
1190 if (isTailCall) {
1191 ++NumTailCalls;
1192 IsSibCall = true;
1193 }
1194 }
Evan Chenga8e29892007-01-19 07:51:42 +00001195
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196 // Analyze operands of the call, assigning locations to each operand.
1197 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1199 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00001200 CCInfo.setCallOrPrologue(Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001201 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001202 CCAssignFnForNode(CallConv, /* Return*/ false,
1203 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001204
Bob Wilson1f595bb2009-04-17 19:07:39 +00001205 // Get a count of how many bytes are to be pushed on the stack.
1206 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001207
Dale Johannesen51e28e62010-06-03 21:09:53 +00001208 // For tail calls, memory operands are available in our caller's stack.
1209 if (IsSibCall)
1210 NumBytes = 0;
1211
Evan Chenga8e29892007-01-19 07:51:42 +00001212 // Adjust the stack pointer for the new arguments...
1213 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001214 if (!IsSibCall)
1215 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001216
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001217 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001218
Bob Wilson5bafff32009-06-22 23:27:02 +00001219 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001220 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001221
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001223 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1225 i != e;
1226 ++i, ++realArgIdx) {
1227 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001228 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001229 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001230 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001231
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 // Promote the value if needed.
1233 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001234 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001235 case CCValAssign::Full: break;
1236 case CCValAssign::SExt:
1237 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1238 break;
1239 case CCValAssign::ZExt:
1240 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1241 break;
1242 case CCValAssign::AExt:
1243 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1244 break;
1245 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001246 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001248 }
1249
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001250 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 if (VA.getLocVT() == MVT::v2f64) {
1253 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1254 DAG.getConstant(0, MVT::i32));
1255 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1256 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001259 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1260
1261 VA = ArgLocs[++i]; // skip ahead to next loc
1262 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001263 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001264 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1265 } else {
1266 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001267
Dan Gohman98ca4f22009-08-05 01:29:28 +00001268 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1269 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001270 }
1271 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001272 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001273 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001274 }
1275 } else if (VA.isRegLoc()) {
1276 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001277 } else if (isByVal) {
1278 assert(VA.isMemLoc());
1279 unsigned offset = 0;
1280
1281 // True if this byval aggregate will be split between registers
1282 // and memory.
1283 if (CCInfo.isFirstByValRegValid()) {
1284 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1285 unsigned int i, j;
1286 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1287 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1288 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1289 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1290 MachinePointerInfo(),
1291 false, false, 0);
1292 MemOpChains.push_back(Load.getValue(1));
1293 RegsToPass.push_back(std::make_pair(j, Load));
1294 }
1295 offset = ARM::R4 - CCInfo.getFirstByValReg();
1296 CCInfo.clearFirstByValReg();
1297 }
1298
1299 unsigned LocMemOffset = VA.getLocMemOffset();
1300 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1301 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1302 StkPtrOff);
1303 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1304 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1305 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1306 MVT::i32);
1307 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1308 Flags.getByValAlign(),
1309 /*isVolatile=*/false,
1310 /*AlwaysInline=*/false,
1311 MachinePointerInfo(0),
1312 MachinePointerInfo(0)));
1313
1314 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001315 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001316
Dan Gohman98ca4f22009-08-05 01:29:28 +00001317 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1318 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001319 }
Evan Chenga8e29892007-01-19 07:51:42 +00001320 }
1321
1322 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001324 &MemOpChains[0], MemOpChains.size());
1325
1326 // Build a sequence of copy-to-reg nodes chained together with token chain
1327 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001328 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001329 // Tail call byval lowering might overwrite argument registers so in case of
1330 // tail call optimization the copies to registers are lowered later.
1331 if (!isTailCall)
1332 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1333 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1334 RegsToPass[i].second, InFlag);
1335 InFlag = Chain.getValue(1);
1336 }
Evan Chenga8e29892007-01-19 07:51:42 +00001337
Dale Johannesen51e28e62010-06-03 21:09:53 +00001338 // For tail calls lower the arguments to the 'real' stack slot.
1339 if (isTailCall) {
1340 // Force all the incoming stack arguments to be loaded from the stack
1341 // before any new outgoing arguments are stored to the stack, because the
1342 // outgoing stack slots may alias the incoming argument stack slots, and
1343 // the alias isn't otherwise explicit. This is slightly more conservative
1344 // than necessary, because it means that each store effectively depends
1345 // on every argument instead of just those arguments it would clobber.
1346
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001347 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001348 InFlag = SDValue();
1349 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1350 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1351 RegsToPass[i].second, InFlag);
1352 InFlag = Chain.getValue(1);
1353 }
1354 InFlag =SDValue();
1355 }
1356
Bill Wendling056292f2008-09-16 21:48:12 +00001357 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1358 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1359 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001360 bool isDirect = false;
1361 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001362 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001363 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001364
1365 if (EnableARMLongCalls) {
1366 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1367 && "long-calls with non-static relocation model!");
1368 // Handle a global address or an external symbol. If it's not one of
1369 // those, the target's already in a register, so we don't need to do
1370 // anything extra.
1371 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001372 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001373 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001374 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001375 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1376 ARMPCLabelIndex,
1377 ARMCP::CPValue, 0);
1378 // Get the address of the callee into a register
1379 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1380 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1381 Callee = DAG.getLoad(getPointerTy(), dl,
1382 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001383 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001384 false, false, 0);
1385 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1386 const char *Sym = S->getSymbol();
1387
1388 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001389 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001390 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1391 Sym, ARMPCLabelIndex, 0);
1392 // Get the address of the callee into a register
1393 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1394 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1395 Callee = DAG.getLoad(getPointerTy(), dl,
1396 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001397 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001398 false, false, 0);
1399 }
1400 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001401 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001402 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001403 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001404 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001405 getTargetMachine().getRelocationModel() != Reloc::Static;
1406 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001407 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001408 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001409 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001410 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001411 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001412 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001413 ARMPCLabelIndex,
1414 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001415 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001416 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001417 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001418 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001419 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001420 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001421 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001422 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001423 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001424 } else {
1425 // On ELF targets for PIC code, direct calls should go through the PLT
1426 unsigned OpFlags = 0;
1427 if (Subtarget->isTargetELF() &&
1428 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1429 OpFlags = ARMII::MO_PLT;
1430 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1431 }
Bill Wendling056292f2008-09-16 21:48:12 +00001432 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001433 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001434 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001435 getTargetMachine().getRelocationModel() != Reloc::Static;
1436 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001437 // tBX takes a register source operand.
1438 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001439 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001440 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001441 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001442 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001443 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001444 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001445 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001446 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001447 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001448 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001449 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001450 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001451 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001452 } else {
1453 unsigned OpFlags = 0;
1454 // On ELF targets for PIC code, direct calls should go through the PLT
1455 if (Subtarget->isTargetELF() &&
1456 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1457 OpFlags = ARMII::MO_PLT;
1458 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1459 }
Evan Chenga8e29892007-01-19 07:51:42 +00001460 }
1461
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001462 // FIXME: handle tail calls differently.
1463 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001464 if (Subtarget->isThumb()) {
1465 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001466 CallOpc = ARMISD::CALL_NOLINK;
1467 else
1468 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1469 } else {
1470 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001471 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1472 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001473 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001474
Dan Gohman475871a2008-07-27 21:46:04 +00001475 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001476 Ops.push_back(Chain);
1477 Ops.push_back(Callee);
1478
1479 // Add argument registers to the end of the list so that they are known live
1480 // into the call.
1481 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1482 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1483 RegsToPass[i].second.getValueType()));
1484
Gabor Greifba36cb52008-08-28 21:40:38 +00001485 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001486 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001487
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001488 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001489 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001490 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001491
Duncan Sands4bdcb612008-07-02 17:40:58 +00001492 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001493 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001494 InFlag = Chain.getValue(1);
1495
Chris Lattnere563bbc2008-10-11 22:08:30 +00001496 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1497 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001498 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001499 InFlag = Chain.getValue(1);
1500
Bob Wilson1f595bb2009-04-17 19:07:39 +00001501 // Handle result values, copying them out of physregs into vregs that we
1502 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001503 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1504 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001505}
1506
Stuart Hastingsf222e592011-02-28 17:17:53 +00001507/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001508/// on the stack. Remember the next parameter register to allocate,
1509/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001510/// this.
1511void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001512llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1513 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1514 assert((State->getCallOrPrologue() == Prologue ||
1515 State->getCallOrPrologue() == Call) &&
1516 "unhandled ParmContext");
1517 if ((!State->isFirstByValRegValid()) &&
1518 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1519 State->setFirstByValReg(reg);
1520 // At a call site, a byval parameter that is split between
1521 // registers and memory needs its size truncated here. In a
1522 // function prologue, such byval parameters are reassembled in
1523 // memory, and are not truncated.
1524 if (State->getCallOrPrologue() == Call) {
1525 unsigned excess = 4 * (ARM::R4 - reg);
1526 assert(size >= excess && "expected larger existing stack allocation");
1527 size -= excess;
1528 }
1529 }
1530 // Confiscate any remaining parameter registers to preclude their
1531 // assignment to subsequent parameters.
1532 while (State->AllocateReg(GPRArgRegs, 4))
1533 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001534}
1535
Dale Johannesen51e28e62010-06-03 21:09:53 +00001536/// MatchingStackOffset - Return true if the given stack call argument is
1537/// already available in the same position (relatively) of the caller's
1538/// incoming argument stack.
1539static
1540bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1541 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1542 const ARMInstrInfo *TII) {
1543 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1544 int FI = INT_MAX;
1545 if (Arg.getOpcode() == ISD::CopyFromReg) {
1546 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001547 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001548 return false;
1549 MachineInstr *Def = MRI->getVRegDef(VR);
1550 if (!Def)
1551 return false;
1552 if (!Flags.isByVal()) {
1553 if (!TII->isLoadFromStackSlot(Def, FI))
1554 return false;
1555 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001556 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001557 }
1558 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1559 if (Flags.isByVal())
1560 // ByVal argument is passed in as a pointer but it's now being
1561 // dereferenced. e.g.
1562 // define @foo(%struct.X* %A) {
1563 // tail call @bar(%struct.X* byval %A)
1564 // }
1565 return false;
1566 SDValue Ptr = Ld->getBasePtr();
1567 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1568 if (!FINode)
1569 return false;
1570 FI = FINode->getIndex();
1571 } else
1572 return false;
1573
1574 assert(FI != INT_MAX);
1575 if (!MFI->isFixedObjectIndex(FI))
1576 return false;
1577 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1578}
1579
1580/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1581/// for tail call optimization. Targets which want to do tail call
1582/// optimization should implement this function.
1583bool
1584ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1585 CallingConv::ID CalleeCC,
1586 bool isVarArg,
1587 bool isCalleeStructRet,
1588 bool isCallerStructRet,
1589 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001590 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001591 const SmallVectorImpl<ISD::InputArg> &Ins,
1592 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001593 const Function *CallerF = DAG.getMachineFunction().getFunction();
1594 CallingConv::ID CallerCC = CallerF->getCallingConv();
1595 bool CCMatch = CallerCC == CalleeCC;
1596
1597 // Look for obvious safe cases to perform tail call optimization that do not
1598 // require ABI changes. This is what gcc calls sibcall.
1599
Jim Grosbach7616b642010-06-16 23:45:49 +00001600 // Do not sibcall optimize vararg calls unless the call site is not passing
1601 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001602 if (isVarArg && !Outs.empty())
1603 return false;
1604
1605 // Also avoid sibcall optimization if either caller or callee uses struct
1606 // return semantics.
1607 if (isCalleeStructRet || isCallerStructRet)
1608 return false;
1609
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001610 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001611 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001612 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1613 // LR. This means if we need to reload LR, it takes an extra instructions,
1614 // which outweighs the value of the tail call; but here we don't know yet
1615 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001616 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001617 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001618
1619 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1620 // but we need to make sure there are enough registers; the only valid
1621 // registers are the 4 used for parameters. We don't currently do this
1622 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001623 if (Subtarget->isThumb1Only())
1624 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001625
Dale Johannesen51e28e62010-06-03 21:09:53 +00001626 // If the calling conventions do not match, then we'd better make sure the
1627 // results are returned in the same way as what the caller expects.
1628 if (!CCMatch) {
1629 SmallVector<CCValAssign, 16> RVLocs1;
1630 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1631 RVLocs1, *DAG.getContext());
1632 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1633
1634 SmallVector<CCValAssign, 16> RVLocs2;
1635 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1636 RVLocs2, *DAG.getContext());
1637 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1638
1639 if (RVLocs1.size() != RVLocs2.size())
1640 return false;
1641 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1642 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1643 return false;
1644 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1645 return false;
1646 if (RVLocs1[i].isRegLoc()) {
1647 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1648 return false;
1649 } else {
1650 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1651 return false;
1652 }
1653 }
1654 }
1655
1656 // If the callee takes no arguments then go on to check the results of the
1657 // call.
1658 if (!Outs.empty()) {
1659 // Check if stack adjustment is needed. For now, do not do this if any
1660 // argument is passed on the stack.
1661 SmallVector<CCValAssign, 16> ArgLocs;
1662 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1663 ArgLocs, *DAG.getContext());
1664 CCInfo.AnalyzeCallOperands(Outs,
1665 CCAssignFnForNode(CalleeCC, false, isVarArg));
1666 if (CCInfo.getNextStackOffset()) {
1667 MachineFunction &MF = DAG.getMachineFunction();
1668
1669 // Check if the arguments are already laid out in the right way as
1670 // the caller's fixed stack objects.
1671 MachineFrameInfo *MFI = MF.getFrameInfo();
1672 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1673 const ARMInstrInfo *TII =
1674 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001675 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1676 i != e;
1677 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001678 CCValAssign &VA = ArgLocs[i];
1679 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001680 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001681 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001682 if (VA.getLocInfo() == CCValAssign::Indirect)
1683 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001684 if (VA.needsCustom()) {
1685 // f64 and vector types are split into multiple registers or
1686 // register/stack-slot combinations. The types will not match
1687 // the registers; give up on memory f64 refs until we figure
1688 // out what to do about this.
1689 if (!VA.isRegLoc())
1690 return false;
1691 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001692 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001693 if (RegVT == MVT::v2f64) {
1694 if (!ArgLocs[++i].isRegLoc())
1695 return false;
1696 if (!ArgLocs[++i].isRegLoc())
1697 return false;
1698 }
1699 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001700 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1701 MFI, MRI, TII))
1702 return false;
1703 }
1704 }
1705 }
1706 }
1707
1708 return true;
1709}
1710
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711SDValue
1712ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001713 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001715 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001716 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001717
Bob Wilsondee46d72009-04-17 20:35:10 +00001718 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001719 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001720
Bob Wilsondee46d72009-04-17 20:35:10 +00001721 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1723 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001724
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001726 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1727 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001728
1729 // If this is the first return lowered for this function, add
1730 // the regs to the liveout set for the function.
1731 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1732 for (unsigned i = 0; i != RVLocs.size(); ++i)
1733 if (RVLocs[i].isRegLoc())
1734 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001735 }
1736
Bob Wilson1f595bb2009-04-17 19:07:39 +00001737 SDValue Flag;
1738
1739 // Copy the result values into the output registers.
1740 for (unsigned i = 0, realRVLocIdx = 0;
1741 i != RVLocs.size();
1742 ++i, ++realRVLocIdx) {
1743 CCValAssign &VA = RVLocs[i];
1744 assert(VA.isRegLoc() && "Can only return in registers!");
1745
Dan Gohmanc9403652010-07-07 15:54:55 +00001746 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001747
1748 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001749 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001750 case CCValAssign::Full: break;
1751 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001752 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753 break;
1754 }
1755
Bob Wilson1f595bb2009-04-17 19:07:39 +00001756 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001757 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001758 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001759 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1760 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001761 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001763
1764 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1765 Flag = Chain.getValue(1);
1766 VA = RVLocs[++i]; // skip ahead to next loc
1767 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1768 HalfGPRs.getValue(1), Flag);
1769 Flag = Chain.getValue(1);
1770 VA = RVLocs[++i]; // skip ahead to next loc
1771
1772 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001773 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1774 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001775 }
1776 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1777 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001778 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001780 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001781 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001782 VA = RVLocs[++i]; // skip ahead to next loc
1783 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1784 Flag);
1785 } else
1786 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1787
Bob Wilsondee46d72009-04-17 20:35:10 +00001788 // Guarantee that all emitted copies are
1789 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001790 Flag = Chain.getValue(1);
1791 }
1792
1793 SDValue result;
1794 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001796 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001798
1799 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001800}
1801
Evan Cheng3d2125c2010-11-30 23:55:39 +00001802bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1803 if (N->getNumValues() != 1)
1804 return false;
1805 if (!N->hasNUsesOfValue(1, 0))
1806 return false;
1807
1808 unsigned NumCopies = 0;
1809 SDNode* Copies[2];
1810 SDNode *Use = *N->use_begin();
1811 if (Use->getOpcode() == ISD::CopyToReg) {
1812 Copies[NumCopies++] = Use;
1813 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1814 // f64 returned in a pair of GPRs.
1815 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1816 UI != UE; ++UI) {
1817 if (UI->getOpcode() != ISD::CopyToReg)
1818 return false;
1819 Copies[UI.getUse().getResNo()] = *UI;
1820 ++NumCopies;
1821 }
1822 } else if (Use->getOpcode() == ISD::BITCAST) {
1823 // f32 returned in a single GPR.
1824 if (!Use->hasNUsesOfValue(1, 0))
1825 return false;
1826 Use = *Use->use_begin();
1827 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1828 return false;
1829 Copies[NumCopies++] = Use;
1830 } else {
1831 return false;
1832 }
1833
1834 if (NumCopies != 1 && NumCopies != 2)
1835 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001836
1837 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001838 for (unsigned i = 0; i < NumCopies; ++i) {
1839 SDNode *Copy = Copies[i];
1840 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1841 UI != UE; ++UI) {
1842 if (UI->getOpcode() == ISD::CopyToReg) {
1843 SDNode *Use = *UI;
1844 if (Use == Copies[0] || Use == Copies[1])
1845 continue;
1846 return false;
1847 }
1848 if (UI->getOpcode() != ARMISD::RET_FLAG)
1849 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001850 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001851 }
1852 }
1853
Evan Cheng1bf891a2010-12-01 22:59:46 +00001854 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001855}
1856
Evan Cheng485fafc2011-03-21 01:19:09 +00001857bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1858 if (!EnableARMTailCalls)
1859 return false;
1860
1861 if (!CI->isTailCall())
1862 return false;
1863
1864 return !Subtarget->isThumb1Only();
1865}
1866
Bob Wilsonb62d2572009-11-03 00:02:05 +00001867// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1868// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1869// one of the above mentioned nodes. It has to be wrapped because otherwise
1870// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1871// be used to form addressing mode. These wrapped nodes will be selected
1872// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001873static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001874 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001875 // FIXME there is no actual debug info here
1876 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001877 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001878 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001879 if (CP->isMachineConstantPoolEntry())
1880 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1881 CP->getAlignment());
1882 else
1883 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1884 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001885 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001886}
1887
Jim Grosbache1102ca2010-07-19 17:20:38 +00001888unsigned ARMTargetLowering::getJumpTableEncoding() const {
1889 return MachineJumpTableInfo::EK_Inline;
1890}
1891
Dan Gohmand858e902010-04-17 15:26:15 +00001892SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1893 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001894 MachineFunction &MF = DAG.getMachineFunction();
1895 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1896 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001897 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001898 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001899 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001900 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1901 SDValue CPAddr;
1902 if (RelocM == Reloc::Static) {
1903 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1904 } else {
1905 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001906 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001907 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1908 ARMCP::CPBlockAddress,
1909 PCAdj);
1910 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1911 }
1912 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1913 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001914 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001915 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001916 if (RelocM == Reloc::Static)
1917 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001918 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001919 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001920}
1921
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001922// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001923SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001924ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001925 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001926 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001927 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001928 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001929 MachineFunction &MF = DAG.getMachineFunction();
1930 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001931 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001932 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001933 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001934 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001935 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001936 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001937 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001938 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001939 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001941
Evan Chenge7e0d622009-11-06 22:24:13 +00001942 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001943 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001944
1945 // call __tls_get_addr.
1946 ArgListTy Args;
1947 ArgListEntry Entry;
1948 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001949 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001950 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001951 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001952 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001953 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1954 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001955 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001956 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001957 return CallResult.first;
1958}
1959
1960// Lower ISD::GlobalTLSAddress using the "initial exec" or
1961// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001962SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001963ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001964 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001965 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001966 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue Offset;
1968 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001969 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001970 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001971 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001972
Chris Lattner4fb63d02009-07-15 04:12:33 +00001973 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001974 MachineFunction &MF = DAG.getMachineFunction();
1975 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001976 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001977 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001978 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1979 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001980 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001981 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001982 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001984 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001985 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001986 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001987 Chain = Offset.getValue(1);
1988
Evan Chenge7e0d622009-11-06 22:24:13 +00001989 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001990 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001991
Evan Cheng9eda6892009-10-31 03:39:36 +00001992 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001993 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001994 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001995 } else {
1996 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001997 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001998 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002000 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002001 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002002 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002003 }
2004
2005 // The address of the thread local variable is the add of the thread
2006 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002007 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002008}
2009
Dan Gohman475871a2008-07-27 21:46:04 +00002010SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002011ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012 // TODO: implement the "local dynamic" model
2013 assert(Subtarget->isTargetELF() &&
2014 "TLS not implemented for non-ELF targets");
2015 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2016 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2017 // otherwise use the "Local Exec" TLS Model
2018 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2019 return LowerToTLSGeneralDynamicModel(GA, DAG);
2020 else
2021 return LowerToTLSExecModels(GA, DAG);
2022}
2023
Dan Gohman475871a2008-07-27 21:46:04 +00002024SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002025 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002026 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002027 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002028 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002029 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2030 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002031 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002032 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002033 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002034 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002036 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002037 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002038 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002039 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002040 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002041 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002042 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002043 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002044 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002045 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002046 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002047 }
2048
2049 // If we have T2 ops, we can materialize the address directly via movt/movw
2050 // pair. This is always cheaper.
2051 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002052 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002053 // FIXME: Once remat is capable of dealing with instructions with register
2054 // operands, expand this into two nodes.
2055 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2056 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002057 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002058 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2059 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2060 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2061 MachinePointerInfo::getConstantPool(),
2062 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002063 }
2064}
2065
Dan Gohman475871a2008-07-27 21:46:04 +00002066SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002067 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002068 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002069 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002070 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002071 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002072 MachineFunction &MF = DAG.getMachineFunction();
2073 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2074
2075 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002076 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002077 // FIXME: Once remat is capable of dealing with instructions with register
2078 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002079 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002080 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2081 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2082
Evan Cheng53519f02011-01-21 18:55:51 +00002083 unsigned Wrapper = (RelocM == Reloc::PIC_)
2084 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2085 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002086 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002087 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2088 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2089 MachinePointerInfo::getGOT(), false, false, 0);
2090 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002091 }
2092
2093 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002094 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002095 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002096 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002097 } else {
2098 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002099 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2100 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002101 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002102 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002103 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002104 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002105
Evan Cheng9eda6892009-10-31 03:39:36 +00002106 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002107 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002108 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002109 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002110
2111 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002112 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002113 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002114 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002115
Evan Cheng63476a82009-09-03 07:04:02 +00002116 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002117 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002118 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002119
2120 return Result;
2121}
2122
Dan Gohman475871a2008-07-27 21:46:04 +00002123SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002124 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002125 assert(Subtarget->isTargetELF() &&
2126 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002127 MachineFunction &MF = DAG.getMachineFunction();
2128 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002129 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002130 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002131 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002132 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002133 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2134 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002135 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002136 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002137 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002138 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002139 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002140 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002141 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002142 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002143}
2144
Jim Grosbach0e0da732009-05-12 23:59:14 +00002145SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002146ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2147 const {
2148 DebugLoc dl = Op.getDebugLoc();
2149 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendlingf05b1dc2011-04-05 01:37:43 +00002150 Op.getOperand(0));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002151}
2152
2153SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002154ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2155 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002156 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002157 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2158 Op.getOperand(1), Val);
2159}
2160
2161SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002162ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2163 DebugLoc dl = Op.getDebugLoc();
2164 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2165 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2166}
2167
2168SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002169ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002170 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002171 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002172 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002173 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002174 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002175 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002176 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002177 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2178 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002179 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002180 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002181 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002182 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002183 EVT PtrVT = getPointerTy();
2184 DebugLoc dl = Op.getDebugLoc();
2185 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2186 SDValue CPAddr;
2187 unsigned PCAdj = (RelocM != Reloc::PIC_)
2188 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002189 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002190 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2191 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002192 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002193 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002194 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002195 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002196 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002197 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002198
2199 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002200 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002201 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2202 }
2203 return Result;
2204 }
Evan Cheng92e39162011-03-29 23:06:19 +00002205 case Intrinsic::arm_neon_vmulls:
2206 case Intrinsic::arm_neon_vmullu: {
2207 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2208 ? ARMISD::VMULLs : ARMISD::VMULLu;
2209 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2210 Op.getOperand(1), Op.getOperand(2));
2211 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002212 }
2213}
2214
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002215static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002216 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002217 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002218 if (!Subtarget->hasDataBarrier()) {
2219 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2220 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2221 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002222 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002223 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002224 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002225 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002226 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002227
2228 SDValue Op5 = Op.getOperand(5);
2229 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2230 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2231 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2232 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2233
2234 ARM_MB::MemBOpt DMBOpt;
2235 if (isDeviceBarrier)
2236 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2237 else
2238 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2239 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2240 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002241}
2242
Evan Chengdfed19f2010-11-03 06:34:55 +00002243static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2244 const ARMSubtarget *Subtarget) {
2245 // ARM pre v5TE and Thumb1 does not have preload instructions.
2246 if (!(Subtarget->isThumb2() ||
2247 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2248 // Just preserve the chain.
2249 return Op.getOperand(0);
2250
2251 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002252 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2253 if (!isRead &&
2254 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2255 // ARMv7 with MP extension has PLDW.
2256 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002257
2258 if (Subtarget->isThumb())
2259 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002260 isRead = ~isRead & 1;
2261 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002262
Evan Cheng416941d2010-11-04 05:19:35 +00002263 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002264 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002265 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2266 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002267}
2268
Dan Gohman1e93df62010-04-17 14:41:14 +00002269static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2270 MachineFunction &MF = DAG.getMachineFunction();
2271 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2272
Evan Chenga8e29892007-01-19 07:51:42 +00002273 // vastart just stores the address of the VarArgsFrameIndex slot into the
2274 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002275 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002276 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002277 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002278 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002279 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2280 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002281}
2282
Dan Gohman475871a2008-07-27 21:46:04 +00002283SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002284ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2285 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002286 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002287 MachineFunction &MF = DAG.getMachineFunction();
2288 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2289
2290 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002291 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002292 RC = ARM::tGPRRegisterClass;
2293 else
2294 RC = ARM::GPRRegisterClass;
2295
2296 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002297 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002298 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002299
2300 SDValue ArgValue2;
2301 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002302 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002303 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002304
2305 // Create load node to retrieve arguments from the stack.
2306 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002307 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002308 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002309 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002310 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002311 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002312 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002313 }
2314
Jim Grosbache5165492009-11-09 00:11:35 +00002315 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002316}
2317
Stuart Hastingsc7315872011-04-20 16:47:52 +00002318void
2319ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2320 unsigned &VARegSize, unsigned &VARegSaveSize)
2321 const {
2322 unsigned NumGPRs;
2323 if (CCInfo.isFirstByValRegValid())
2324 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2325 else {
2326 unsigned int firstUnalloced;
2327 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2328 sizeof(GPRArgRegs) /
2329 sizeof(GPRArgRegs[0]));
2330 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2331 }
2332
2333 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2334 VARegSize = NumGPRs * 4;
2335 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2336}
2337
2338// The remaining GPRs hold either the beginning of variable-argument
2339// data, or the beginning of an aggregate passed by value (usuall
2340// byval). Either way, we allocate stack slots adjacent to the data
2341// provided by our caller, and store the unallocated registers there.
2342// If this is a variadic function, the va_list pointer will begin with
2343// these values; otherwise, this reassembles a (byval) structure that
2344// was split between registers and memory.
2345void
2346ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2347 DebugLoc dl, SDValue &Chain,
2348 unsigned ArgOffset) const {
2349 MachineFunction &MF = DAG.getMachineFunction();
2350 MachineFrameInfo *MFI = MF.getFrameInfo();
2351 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2352 unsigned firstRegToSaveIndex;
2353 if (CCInfo.isFirstByValRegValid())
2354 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2355 else {
2356 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2357 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2358 }
2359
2360 unsigned VARegSize, VARegSaveSize;
2361 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2362 if (VARegSaveSize) {
2363 // If this function is vararg, store any remaining integer argument regs
2364 // to their spots on the stack so that they may be loaded by deferencing
2365 // the result of va_next.
2366 AFI->setVarArgsRegSaveSize(VARegSaveSize);
2367 AFI->setVarArgsFrameIndex(
2368 MFI->CreateFixedObject(VARegSaveSize,
2369 ArgOffset + VARegSaveSize - VARegSize,
2370 false));
2371 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2372 getPointerTy());
2373
2374 SmallVector<SDValue, 4> MemOps;
2375 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2376 TargetRegisterClass *RC;
2377 if (AFI->isThumb1OnlyFunction())
2378 RC = ARM::tGPRRegisterClass;
2379 else
2380 RC = ARM::GPRRegisterClass;
2381
2382 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2383 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2384 SDValue Store =
2385 DAG.getStore(Val.getValue(1), dl, Val, FIN,
2386 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2387 false, false, 0);
2388 MemOps.push_back(Store);
2389 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2390 DAG.getConstant(4, getPointerTy()));
2391 }
2392 if (!MemOps.empty())
2393 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2394 &MemOps[0], MemOps.size());
2395 } else
2396 // This will point to the next argument passed via stack.
2397 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2398}
2399
Bob Wilson5bafff32009-06-22 23:27:02 +00002400SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002401ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002402 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002403 const SmallVectorImpl<ISD::InputArg>
2404 &Ins,
2405 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002406 SmallVectorImpl<SDValue> &InVals)
2407 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002408 MachineFunction &MF = DAG.getMachineFunction();
2409 MachineFrameInfo *MFI = MF.getFrameInfo();
2410
Bob Wilson1f595bb2009-04-17 19:07:39 +00002411 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2412
2413 // Assign locations to all of the incoming arguments.
2414 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002415 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2416 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00002417 CCInfo.setCallOrPrologue(Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002418 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002419 CCAssignFnForNode(CallConv, /* Return*/ false,
2420 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002421
2422 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002423 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002424
Stuart Hastingsf222e592011-02-28 17:17:53 +00002425 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002426 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2427 CCValAssign &VA = ArgLocs[i];
2428
Bob Wilsondee46d72009-04-17 20:35:10 +00002429 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002430 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002431 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002432
Bob Wilson1f595bb2009-04-17 19:07:39 +00002433 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002434 // f64 and vector types are split up into multiple registers or
2435 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002436 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002437 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002438 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002439 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002440 SDValue ArgValue2;
2441 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002442 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002443 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2444 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002445 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002446 false, false, 0);
2447 } else {
2448 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2449 Chain, DAG, dl);
2450 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002451 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2452 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002453 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002455 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2456 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002457 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002458
Bob Wilson5bafff32009-06-22 23:27:02 +00002459 } else {
2460 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002461
Owen Anderson825b72b2009-08-11 20:47:22 +00002462 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002463 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002464 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002465 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002466 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002467 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002468 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002469 RC = (AFI->isThumb1OnlyFunction() ?
2470 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002471 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002472 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002473
2474 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002475 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002476 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002477 }
2478
2479 // If this is an 8 or 16-bit value, it is really passed promoted
2480 // to 32 bits. Insert an assert[sz]ext to capture this, then
2481 // truncate to the right size.
2482 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002483 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002484 case CCValAssign::Full: break;
2485 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002486 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002487 break;
2488 case CCValAssign::SExt:
2489 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2490 DAG.getValueType(VA.getValVT()));
2491 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2492 break;
2493 case CCValAssign::ZExt:
2494 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2495 DAG.getValueType(VA.getValVT()));
2496 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2497 break;
2498 }
2499
Dan Gohman98ca4f22009-08-05 01:29:28 +00002500 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002501
2502 } else { // VA.isRegLoc()
2503
2504 // sanity check
2505 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002506 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002507
Stuart Hastingsf222e592011-02-28 17:17:53 +00002508 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002509
Stuart Hastingsf222e592011-02-28 17:17:53 +00002510 // Some Ins[] entries become multiple ArgLoc[] entries.
2511 // Process them only once.
2512 if (index != lastInsIndex)
2513 {
2514 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2515 // FIXME: For now, all byval parameter objects are marked mutable. This can be
2516 // changed with more analysis.
2517 // In case of tail call optimization mark all arguments mutable. Since they
2518 // could be overwritten by lowering of arguments in case of a tail call.
2519 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002520 unsigned VARegSize, VARegSaveSize;
2521 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2522 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2523 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002524 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002525 int FI = MFI->CreateFixedObject(Bytes,
2526 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002527 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2528 } else {
2529 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2530 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002531
Stuart Hastingsf222e592011-02-28 17:17:53 +00002532 // Create load nodes to retrieve arguments from the stack.
2533 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2534 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2535 MachinePointerInfo::getFixedStack(FI),
2536 false, false, 0));
2537 }
2538 lastInsIndex = index;
2539 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002540 }
2541 }
2542
2543 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002544 if (isVarArg)
2545 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002546
Dan Gohman98ca4f22009-08-05 01:29:28 +00002547 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002548}
2549
2550/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002551static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002552 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002553 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002554 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002555 // Maybe this has already been legalized into the constant pool?
2556 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002557 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002558 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002559 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002560 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002561 }
2562 }
2563 return false;
2564}
2565
Evan Chenga8e29892007-01-19 07:51:42 +00002566/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2567/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002568SDValue
2569ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002570 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002571 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002572 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002573 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002574 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002575 // Constant does not fit, try adjusting it by one?
2576 switch (CC) {
2577 default: break;
2578 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002579 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002580 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002581 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002582 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002583 }
2584 break;
2585 case ISD::SETULT:
2586 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002587 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002588 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002589 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002590 }
2591 break;
2592 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002593 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002594 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002595 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002596 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002597 }
2598 break;
2599 case ISD::SETULE:
2600 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002601 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002602 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002603 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002604 }
2605 break;
2606 }
2607 }
2608 }
2609
2610 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002611 ARMISD::NodeType CompareType;
2612 switch (CondCode) {
2613 default:
2614 CompareType = ARMISD::CMP;
2615 break;
2616 case ARMCC::EQ:
2617 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002618 // Uses only Z Flag
2619 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002620 break;
2621 }
Evan Cheng218977b2010-07-13 19:27:42 +00002622 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002623 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002624}
2625
2626/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002627SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002628ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002629 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002630 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002631 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002632 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002633 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002634 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2635 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002636}
2637
Bob Wilson79f56c92011-03-08 01:17:20 +00002638/// duplicateCmp - Glue values can have only one use, so this function
2639/// duplicates a comparison node.
2640SDValue
2641ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2642 unsigned Opc = Cmp.getOpcode();
2643 DebugLoc DL = Cmp.getDebugLoc();
2644 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2645 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2646
2647 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2648 Cmp = Cmp.getOperand(0);
2649 Opc = Cmp.getOpcode();
2650 if (Opc == ARMISD::CMPFP)
2651 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2652 else {
2653 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2654 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2655 }
2656 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2657}
2658
Bill Wendlingde2b1512010-08-11 08:43:16 +00002659SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2660 SDValue Cond = Op.getOperand(0);
2661 SDValue SelectTrue = Op.getOperand(1);
2662 SDValue SelectFalse = Op.getOperand(2);
2663 DebugLoc dl = Op.getDebugLoc();
2664
2665 // Convert:
2666 //
2667 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2668 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2669 //
2670 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2671 const ConstantSDNode *CMOVTrue =
2672 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2673 const ConstantSDNode *CMOVFalse =
2674 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2675
2676 if (CMOVTrue && CMOVFalse) {
2677 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2678 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2679
2680 SDValue True;
2681 SDValue False;
2682 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2683 True = SelectTrue;
2684 False = SelectFalse;
2685 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2686 True = SelectFalse;
2687 False = SelectTrue;
2688 }
2689
2690 if (True.getNode() && False.getNode()) {
2691 EVT VT = Cond.getValueType();
2692 SDValue ARMcc = Cond.getOperand(2);
2693 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002694 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002695 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2696 }
2697 }
2698 }
2699
2700 return DAG.getSelectCC(dl, Cond,
2701 DAG.getConstant(0, Cond.getValueType()),
2702 SelectTrue, SelectFalse, ISD::SETNE);
2703}
2704
Dan Gohmand858e902010-04-17 15:26:15 +00002705SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002706 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002707 SDValue LHS = Op.getOperand(0);
2708 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002709 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002710 SDValue TrueVal = Op.getOperand(2);
2711 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002712 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002713
Owen Anderson825b72b2009-08-11 20:47:22 +00002714 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002715 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002716 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002717 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2718 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002719 }
2720
2721 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002722 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002723
Evan Cheng218977b2010-07-13 19:27:42 +00002724 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2725 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002726 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002727 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002728 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002729 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002730 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002731 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002732 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002733 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002734 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002735 }
2736 return Result;
2737}
2738
Evan Cheng218977b2010-07-13 19:27:42 +00002739/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2740/// to morph to an integer compare sequence.
2741static bool canChangeToInt(SDValue Op, bool &SeenZero,
2742 const ARMSubtarget *Subtarget) {
2743 SDNode *N = Op.getNode();
2744 if (!N->hasOneUse())
2745 // Otherwise it requires moving the value from fp to integer registers.
2746 return false;
2747 if (!N->getNumValues())
2748 return false;
2749 EVT VT = Op.getValueType();
2750 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2751 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2752 // vmrs are very slow, e.g. cortex-a8.
2753 return false;
2754
2755 if (isFloatingPointZero(Op)) {
2756 SeenZero = true;
2757 return true;
2758 }
2759 return ISD::isNormalLoad(N);
2760}
2761
2762static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2763 if (isFloatingPointZero(Op))
2764 return DAG.getConstant(0, MVT::i32);
2765
2766 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2767 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002768 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002769 Ld->isVolatile(), Ld->isNonTemporal(),
2770 Ld->getAlignment());
2771
2772 llvm_unreachable("Unknown VFP cmp argument!");
2773}
2774
2775static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2776 SDValue &RetVal1, SDValue &RetVal2) {
2777 if (isFloatingPointZero(Op)) {
2778 RetVal1 = DAG.getConstant(0, MVT::i32);
2779 RetVal2 = DAG.getConstant(0, MVT::i32);
2780 return;
2781 }
2782
2783 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2784 SDValue Ptr = Ld->getBasePtr();
2785 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2786 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002787 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002788 Ld->isVolatile(), Ld->isNonTemporal(),
2789 Ld->getAlignment());
2790
2791 EVT PtrType = Ptr.getValueType();
2792 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2793 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2794 PtrType, Ptr, DAG.getConstant(4, PtrType));
2795 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2796 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002797 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002798 Ld->isVolatile(), Ld->isNonTemporal(),
2799 NewAlign);
2800 return;
2801 }
2802
2803 llvm_unreachable("Unknown VFP cmp argument!");
2804}
2805
2806/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2807/// f32 and even f64 comparisons to integer ones.
2808SDValue
2809ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2810 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002811 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002812 SDValue LHS = Op.getOperand(2);
2813 SDValue RHS = Op.getOperand(3);
2814 SDValue Dest = Op.getOperand(4);
2815 DebugLoc dl = Op.getDebugLoc();
2816
2817 bool SeenZero = false;
2818 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2819 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002820 // If one of the operand is zero, it's safe to ignore the NaN case since
2821 // we only care about equality comparisons.
2822 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002823 // If unsafe fp math optimization is enabled and there are no other uses of
2824 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002825 // to an integer comparison.
2826 if (CC == ISD::SETOEQ)
2827 CC = ISD::SETEQ;
2828 else if (CC == ISD::SETUNE)
2829 CC = ISD::SETNE;
2830
2831 SDValue ARMcc;
2832 if (LHS.getValueType() == MVT::f32) {
2833 LHS = bitcastf32Toi32(LHS, DAG);
2834 RHS = bitcastf32Toi32(RHS, DAG);
2835 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2836 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2837 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2838 Chain, Dest, ARMcc, CCR, Cmp);
2839 }
2840
2841 SDValue LHS1, LHS2;
2842 SDValue RHS1, RHS2;
2843 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2844 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2845 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2846 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002847 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002848 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2849 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2850 }
2851
2852 return SDValue();
2853}
2854
2855SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2856 SDValue Chain = Op.getOperand(0);
2857 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2858 SDValue LHS = Op.getOperand(2);
2859 SDValue RHS = Op.getOperand(3);
2860 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002861 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002862
Owen Anderson825b72b2009-08-11 20:47:22 +00002863 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002864 SDValue ARMcc;
2865 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002866 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002867 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002868 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002869 }
2870
Owen Anderson825b72b2009-08-11 20:47:22 +00002871 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002872
2873 if (UnsafeFPMath &&
2874 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2875 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2876 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2877 if (Result.getNode())
2878 return Result;
2879 }
2880
Evan Chenga8e29892007-01-19 07:51:42 +00002881 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002882 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002883
Evan Cheng218977b2010-07-13 19:27:42 +00002884 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2885 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002886 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002887 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002888 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002889 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002890 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002891 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2892 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002893 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002894 }
2895 return Res;
2896}
2897
Dan Gohmand858e902010-04-17 15:26:15 +00002898SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002899 SDValue Chain = Op.getOperand(0);
2900 SDValue Table = Op.getOperand(1);
2901 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002902 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002903
Owen Andersone50ed302009-08-10 22:56:29 +00002904 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002905 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2906 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002907 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002908 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002909 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002910 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2911 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002912 if (Subtarget->isThumb2()) {
2913 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2914 // which does another jump to the destination. This also makes it easier
2915 // to translate it to TBB / TBH later.
2916 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002917 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002918 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002919 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002920 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002921 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002922 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002923 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002924 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002925 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002926 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002927 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002928 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002929 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002930 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002932 }
Evan Chenga8e29892007-01-19 07:51:42 +00002933}
2934
Bob Wilson76a312b2010-03-19 22:51:32 +00002935static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2936 DebugLoc dl = Op.getDebugLoc();
2937 unsigned Opc;
2938
2939 switch (Op.getOpcode()) {
2940 default:
2941 assert(0 && "Invalid opcode!");
2942 case ISD::FP_TO_SINT:
2943 Opc = ARMISD::FTOSI;
2944 break;
2945 case ISD::FP_TO_UINT:
2946 Opc = ARMISD::FTOUI;
2947 break;
2948 }
2949 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002950 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002951}
2952
Cameron Zwarich3007d332011-03-29 21:41:55 +00002953static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2954 EVT VT = Op.getValueType();
2955 DebugLoc dl = Op.getDebugLoc();
2956
2957 EVT OperandVT = Op.getOperand(0).getValueType();
2958 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2959 if (VT != MVT::v4f32)
2960 return DAG.UnrollVectorOp(Op.getNode());
2961
2962 unsigned CastOpc;
2963 unsigned Opc;
2964 switch (Op.getOpcode()) {
2965 default:
2966 assert(0 && "Invalid opcode!");
2967 case ISD::SINT_TO_FP:
2968 CastOpc = ISD::SIGN_EXTEND;
2969 Opc = ISD::SINT_TO_FP;
2970 break;
2971 case ISD::UINT_TO_FP:
2972 CastOpc = ISD::ZERO_EXTEND;
2973 Opc = ISD::UINT_TO_FP;
2974 break;
2975 }
2976
2977 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2978 return DAG.getNode(Opc, dl, VT, Op);
2979}
2980
Bob Wilson76a312b2010-03-19 22:51:32 +00002981static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2982 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002983 if (VT.isVector())
2984 return LowerVectorINT_TO_FP(Op, DAG);
2985
Bob Wilson76a312b2010-03-19 22:51:32 +00002986 DebugLoc dl = Op.getDebugLoc();
2987 unsigned Opc;
2988
2989 switch (Op.getOpcode()) {
2990 default:
2991 assert(0 && "Invalid opcode!");
2992 case ISD::SINT_TO_FP:
2993 Opc = ARMISD::SITOF;
2994 break;
2995 case ISD::UINT_TO_FP:
2996 Opc = ARMISD::UITOF;
2997 break;
2998 }
2999
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003000 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003001 return DAG.getNode(Opc, dl, VT, Op);
3002}
3003
Evan Cheng515fe3a2010-07-08 02:08:50 +00003004SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003005 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003006 SDValue Tmp0 = Op.getOperand(0);
3007 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003008 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003009 EVT VT = Op.getValueType();
3010 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003011 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3012 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3013 bool UseNEON = !InGPR && Subtarget->hasNEON();
3014
3015 if (UseNEON) {
3016 // Use VBSL to copy the sign bit.
3017 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3018 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3019 DAG.getTargetConstant(EncodedVal, MVT::i32));
3020 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3021 if (VT == MVT::f64)
3022 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3023 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3024 DAG.getConstant(32, MVT::i32));
3025 else /*if (VT == MVT::f32)*/
3026 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3027 if (SrcVT == MVT::f32) {
3028 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3029 if (VT == MVT::f64)
3030 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3031 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3032 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003033 } else if (VT == MVT::f32)
3034 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3035 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3036 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003037 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3038 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3039
3040 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3041 MVT::i32);
3042 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3043 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3044 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003045
Evan Chenge573fb32011-02-23 02:24:55 +00003046 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3047 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3048 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003049 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003050 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3051 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3052 DAG.getConstant(0, MVT::i32));
3053 } else {
3054 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3055 }
3056
3057 return Res;
3058 }
Evan Chengc143dd42011-02-11 02:28:55 +00003059
3060 // Bitcast operand 1 to i32.
3061 if (SrcVT == MVT::f64)
3062 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3063 &Tmp1, 1).getValue(1);
3064 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3065
Evan Chenge573fb32011-02-23 02:24:55 +00003066 // Or in the signbit with integer operations.
3067 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3068 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3069 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3070 if (VT == MVT::f32) {
3071 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3072 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3073 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3074 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003075 }
3076
Evan Chenge573fb32011-02-23 02:24:55 +00003077 // f64: Or the high part with signbit and then combine two parts.
3078 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3079 &Tmp0, 1);
3080 SDValue Lo = Tmp0.getValue(0);
3081 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3082 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3083 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003084}
3085
Evan Cheng2457f2c2010-05-22 01:47:14 +00003086SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3087 MachineFunction &MF = DAG.getMachineFunction();
3088 MachineFrameInfo *MFI = MF.getFrameInfo();
3089 MFI->setReturnAddressIsTaken(true);
3090
3091 EVT VT = Op.getValueType();
3092 DebugLoc dl = Op.getDebugLoc();
3093 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3094 if (Depth) {
3095 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3096 SDValue Offset = DAG.getConstant(4, MVT::i32);
3097 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3098 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003099 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003100 }
3101
3102 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003103 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003104 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3105}
3106
Dan Gohmand858e902010-04-17 15:26:15 +00003107SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003108 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3109 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003110
Owen Andersone50ed302009-08-10 22:56:29 +00003111 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003112 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3113 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003114 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003115 ? ARM::R7 : ARM::R11;
3116 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3117 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003118 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3119 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003120 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003121 return FrameAddr;
3122}
3123
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003124/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003125/// expand a bit convert where either the source or destination type is i64 to
3126/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3127/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3128/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003129static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003130 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3131 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003132 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003133
Bob Wilson9f3f0612010-04-17 05:30:19 +00003134 // This function is only supposed to be called for i64 types, either as the
3135 // source or destination of the bit convert.
3136 EVT SrcVT = Op.getValueType();
3137 EVT DstVT = N->getValueType(0);
3138 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003139 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003140
Bob Wilson9f3f0612010-04-17 05:30:19 +00003141 // Turn i64->f64 into VMOVDRR.
3142 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003143 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3144 DAG.getConstant(0, MVT::i32));
3145 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3146 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003147 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003148 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003149 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003150
Jim Grosbache5165492009-11-09 00:11:35 +00003151 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003152 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3153 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3154 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3155 // Merge the pieces into a single i64 value.
3156 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3157 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003158
Bob Wilson9f3f0612010-04-17 05:30:19 +00003159 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003160}
3161
Bob Wilson5bafff32009-06-22 23:27:02 +00003162/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003163/// Zero vectors are used to represent vector negation and in those cases
3164/// will be implemented with the NEON VNEG instruction. However, VNEG does
3165/// not support i64 elements, so sometimes the zero vectors will need to be
3166/// explicitly constructed. Regardless, use a canonical VMOV to create the
3167/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003168static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003169 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003170 // The canonical modified immediate encoding of a zero vector is....0!
3171 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3172 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3173 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003174 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003175}
3176
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003177/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3178/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003179SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3180 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003181 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3182 EVT VT = Op.getValueType();
3183 unsigned VTBits = VT.getSizeInBits();
3184 DebugLoc dl = Op.getDebugLoc();
3185 SDValue ShOpLo = Op.getOperand(0);
3186 SDValue ShOpHi = Op.getOperand(1);
3187 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003188 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003189 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003190
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003191 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3192
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003193 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3194 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3195 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3196 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3197 DAG.getConstant(VTBits, MVT::i32));
3198 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3199 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003200 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003201
3202 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3203 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003204 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003205 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003206 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003207 CCR, Cmp);
3208
3209 SDValue Ops[2] = { Lo, Hi };
3210 return DAG.getMergeValues(Ops, 2, dl);
3211}
3212
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003213/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3214/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003215SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3216 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003217 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3218 EVT VT = Op.getValueType();
3219 unsigned VTBits = VT.getSizeInBits();
3220 DebugLoc dl = Op.getDebugLoc();
3221 SDValue ShOpLo = Op.getOperand(0);
3222 SDValue ShOpHi = Op.getOperand(1);
3223 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003224 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003225
3226 assert(Op.getOpcode() == ISD::SHL_PARTS);
3227 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3228 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3229 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3230 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3231 DAG.getConstant(VTBits, MVT::i32));
3232 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3233 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3234
3235 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3236 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3237 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003238 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003239 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003240 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003241 CCR, Cmp);
3242
3243 SDValue Ops[2] = { Lo, Hi };
3244 return DAG.getMergeValues(Ops, 2, dl);
3245}
3246
Jim Grosbach4725ca72010-09-08 03:54:02 +00003247SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003248 SelectionDAG &DAG) const {
3249 // The rounding mode is in bits 23:22 of the FPSCR.
3250 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3251 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3252 // so that the shift + and get folded into a bitfield extract.
3253 DebugLoc dl = Op.getDebugLoc();
3254 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3255 DAG.getConstant(Intrinsic::arm_get_fpscr,
3256 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003257 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003258 DAG.getConstant(1U << 22, MVT::i32));
3259 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3260 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003261 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003262 DAG.getConstant(3, MVT::i32));
3263}
3264
Jim Grosbach3482c802010-01-18 19:58:49 +00003265static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3266 const ARMSubtarget *ST) {
3267 EVT VT = N->getValueType(0);
3268 DebugLoc dl = N->getDebugLoc();
3269
3270 if (!ST->hasV6T2Ops())
3271 return SDValue();
3272
3273 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3274 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3275}
3276
Bob Wilson5bafff32009-06-22 23:27:02 +00003277static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3278 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003279 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003280 DebugLoc dl = N->getDebugLoc();
3281
Bob Wilsond5448bb2010-11-18 21:16:28 +00003282 if (!VT.isVector())
3283 return SDValue();
3284
Bob Wilson5bafff32009-06-22 23:27:02 +00003285 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003286 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003287
Bob Wilsond5448bb2010-11-18 21:16:28 +00003288 // Left shifts translate directly to the vshiftu intrinsic.
3289 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003290 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003291 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3292 N->getOperand(0), N->getOperand(1));
3293
3294 assert((N->getOpcode() == ISD::SRA ||
3295 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3296
3297 // NEON uses the same intrinsics for both left and right shifts. For
3298 // right shifts, the shift amounts are negative, so negate the vector of
3299 // shift amounts.
3300 EVT ShiftVT = N->getOperand(1).getValueType();
3301 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3302 getZeroVector(ShiftVT, DAG, dl),
3303 N->getOperand(1));
3304 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3305 Intrinsic::arm_neon_vshifts :
3306 Intrinsic::arm_neon_vshiftu);
3307 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3308 DAG.getConstant(vshiftInt, MVT::i32),
3309 N->getOperand(0), NegatedCount);
3310}
3311
3312static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3313 const ARMSubtarget *ST) {
3314 EVT VT = N->getValueType(0);
3315 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003316
Eli Friedmance392eb2009-08-22 03:13:10 +00003317 // We can get here for a node like i32 = ISD::SHL i32, i64
3318 if (VT != MVT::i64)
3319 return SDValue();
3320
3321 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003322 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003323
Chris Lattner27a6c732007-11-24 07:07:01 +00003324 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3325 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003326 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003327 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003328
Chris Lattner27a6c732007-11-24 07:07:01 +00003329 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003330 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003331
Chris Lattner27a6c732007-11-24 07:07:01 +00003332 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003333 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003334 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003336 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003337
Chris Lattner27a6c732007-11-24 07:07:01 +00003338 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3339 // captures the result into a carry flag.
3340 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003341 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003342
Chris Lattner27a6c732007-11-24 07:07:01 +00003343 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003345
Chris Lattner27a6c732007-11-24 07:07:01 +00003346 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003348}
3349
Bob Wilson5bafff32009-06-22 23:27:02 +00003350static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3351 SDValue TmpOp0, TmpOp1;
3352 bool Invert = false;
3353 bool Swap = false;
3354 unsigned Opc = 0;
3355
3356 SDValue Op0 = Op.getOperand(0);
3357 SDValue Op1 = Op.getOperand(1);
3358 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003359 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003360 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3361 DebugLoc dl = Op.getDebugLoc();
3362
3363 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3364 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003365 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003366 case ISD::SETUNE:
3367 case ISD::SETNE: Invert = true; // Fallthrough
3368 case ISD::SETOEQ:
3369 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3370 case ISD::SETOLT:
3371 case ISD::SETLT: Swap = true; // Fallthrough
3372 case ISD::SETOGT:
3373 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3374 case ISD::SETOLE:
3375 case ISD::SETLE: Swap = true; // Fallthrough
3376 case ISD::SETOGE:
3377 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3378 case ISD::SETUGE: Swap = true; // Fallthrough
3379 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3380 case ISD::SETUGT: Swap = true; // Fallthrough
3381 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3382 case ISD::SETUEQ: Invert = true; // Fallthrough
3383 case ISD::SETONE:
3384 // Expand this to (OLT | OGT).
3385 TmpOp0 = Op0;
3386 TmpOp1 = Op1;
3387 Opc = ISD::OR;
3388 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3389 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3390 break;
3391 case ISD::SETUO: Invert = true; // Fallthrough
3392 case ISD::SETO:
3393 // Expand this to (OLT | OGE).
3394 TmpOp0 = Op0;
3395 TmpOp1 = Op1;
3396 Opc = ISD::OR;
3397 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3398 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3399 break;
3400 }
3401 } else {
3402 // Integer comparisons.
3403 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003404 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003405 case ISD::SETNE: Invert = true;
3406 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3407 case ISD::SETLT: Swap = true;
3408 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3409 case ISD::SETLE: Swap = true;
3410 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3411 case ISD::SETULT: Swap = true;
3412 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3413 case ISD::SETULE: Swap = true;
3414 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3415 }
3416
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003417 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003418 if (Opc == ARMISD::VCEQ) {
3419
3420 SDValue AndOp;
3421 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3422 AndOp = Op0;
3423 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3424 AndOp = Op1;
3425
3426 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003427 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003428 AndOp = AndOp.getOperand(0);
3429
3430 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3431 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003432 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3433 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003434 Invert = !Invert;
3435 }
3436 }
3437 }
3438
3439 if (Swap)
3440 std::swap(Op0, Op1);
3441
Owen Andersonc24cb352010-11-08 23:21:22 +00003442 // If one of the operands is a constant vector zero, attempt to fold the
3443 // comparison to a specialized compare-against-zero form.
3444 SDValue SingleOp;
3445 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3446 SingleOp = Op0;
3447 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3448 if (Opc == ARMISD::VCGE)
3449 Opc = ARMISD::VCLEZ;
3450 else if (Opc == ARMISD::VCGT)
3451 Opc = ARMISD::VCLTZ;
3452 SingleOp = Op1;
3453 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003454
Owen Andersonc24cb352010-11-08 23:21:22 +00003455 SDValue Result;
3456 if (SingleOp.getNode()) {
3457 switch (Opc) {
3458 case ARMISD::VCEQ:
3459 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3460 case ARMISD::VCGE:
3461 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3462 case ARMISD::VCLEZ:
3463 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3464 case ARMISD::VCGT:
3465 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3466 case ARMISD::VCLTZ:
3467 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3468 default:
3469 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3470 }
3471 } else {
3472 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3473 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003474
3475 if (Invert)
3476 Result = DAG.getNOT(dl, Result, VT);
3477
3478 return Result;
3479}
3480
Bob Wilsond3c42842010-06-14 22:19:57 +00003481/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3482/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003483/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003484static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3485 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003486 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003487 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003488
Bob Wilson827b2102010-06-15 19:05:35 +00003489 // SplatBitSize is set to the smallest size that splats the vector, so a
3490 // zero vector will always have SplatBitSize == 8. However, NEON modified
3491 // immediate instructions others than VMOV do not support the 8-bit encoding
3492 // of a zero vector, and the default encoding of zero is supposed to be the
3493 // 32-bit version.
3494 if (SplatBits == 0)
3495 SplatBitSize = 32;
3496
Bob Wilson5bafff32009-06-22 23:27:02 +00003497 switch (SplatBitSize) {
3498 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003499 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003500 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003501 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003502 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003503 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003504 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003505 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003506 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003507
3508 case 16:
3509 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003510 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003511 if ((SplatBits & ~0xff) == 0) {
3512 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003513 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003514 Imm = SplatBits;
3515 break;
3516 }
3517 if ((SplatBits & ~0xff00) == 0) {
3518 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003519 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003520 Imm = SplatBits >> 8;
3521 break;
3522 }
3523 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003524
3525 case 32:
3526 // NEON's 32-bit VMOV supports splat values where:
3527 // * only one byte is nonzero, or
3528 // * the least significant byte is 0xff and the second byte is nonzero, or
3529 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003530 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003531 if ((SplatBits & ~0xff) == 0) {
3532 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003533 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003534 Imm = SplatBits;
3535 break;
3536 }
3537 if ((SplatBits & ~0xff00) == 0) {
3538 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003539 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003540 Imm = SplatBits >> 8;
3541 break;
3542 }
3543 if ((SplatBits & ~0xff0000) == 0) {
3544 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003545 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003546 Imm = SplatBits >> 16;
3547 break;
3548 }
3549 if ((SplatBits & ~0xff000000) == 0) {
3550 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003551 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003552 Imm = SplatBits >> 24;
3553 break;
3554 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003555
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003556 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3557 if (type == OtherModImm) return SDValue();
3558
Bob Wilson5bafff32009-06-22 23:27:02 +00003559 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003560 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3561 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003562 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003563 Imm = SplatBits >> 8;
3564 SplatBits |= 0xff;
3565 break;
3566 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003567
3568 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003569 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3570 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003571 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003572 Imm = SplatBits >> 16;
3573 SplatBits |= 0xffff;
3574 break;
3575 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003576
3577 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3578 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3579 // VMOV.I32. A (very) minor optimization would be to replicate the value
3580 // and fall through here to test for a valid 64-bit splat. But, then the
3581 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003582 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003583
3584 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003585 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003586 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003587 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003588 uint64_t BitMask = 0xff;
3589 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003590 unsigned ImmMask = 1;
3591 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003592 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003593 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003594 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003595 Imm |= ImmMask;
3596 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003597 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003598 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003599 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003600 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003601 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003602 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003603 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003604 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003605 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003606 break;
3607 }
3608
Bob Wilson1a913ed2010-06-11 21:34:50 +00003609 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003610 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003611 return SDValue();
3612 }
3613
Bob Wilsoncba270d2010-07-13 21:16:48 +00003614 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3615 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003616}
3617
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003618static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3619 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003620 unsigned NumElts = VT.getVectorNumElements();
3621 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003622
3623 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3624 if (M[0] < 0)
3625 return false;
3626
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003627 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003628
3629 // If this is a VEXT shuffle, the immediate value is the index of the first
3630 // element. The other shuffle indices must be the successive elements after
3631 // the first one.
3632 unsigned ExpectedElt = Imm;
3633 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003634 // Increment the expected index. If it wraps around, it may still be
3635 // a VEXT but the source vectors must be swapped.
3636 ExpectedElt += 1;
3637 if (ExpectedElt == NumElts * 2) {
3638 ExpectedElt = 0;
3639 ReverseVEXT = true;
3640 }
3641
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003642 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003643 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003644 return false;
3645 }
3646
3647 // Adjust the index value if the source operands will be swapped.
3648 if (ReverseVEXT)
3649 Imm -= NumElts;
3650
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003651 return true;
3652}
3653
Bob Wilson8bb9e482009-07-26 00:39:34 +00003654/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3655/// instruction with the specified blocksize. (The order of the elements
3656/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003657static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3658 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003659 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3660 "Only possible block sizes for VREV are: 16, 32, 64");
3661
Bob Wilson8bb9e482009-07-26 00:39:34 +00003662 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003663 if (EltSz == 64)
3664 return false;
3665
3666 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003667 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003668 // If the first shuffle index is UNDEF, be optimistic.
3669 if (M[0] < 0)
3670 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003671
3672 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3673 return false;
3674
3675 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003676 if (M[i] < 0) continue; // ignore UNDEF indices
3677 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003678 return false;
3679 }
3680
3681 return true;
3682}
3683
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003684static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3685 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3686 // range, then 0 is placed into the resulting vector. So pretty much any mask
3687 // of 8 elements can work here.
3688 return VT == MVT::v8i8 && M.size() == 8;
3689}
3690
Bob Wilsonc692cb72009-08-21 20:54:19 +00003691static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3692 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003693 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3694 if (EltSz == 64)
3695 return false;
3696
Bob Wilsonc692cb72009-08-21 20:54:19 +00003697 unsigned NumElts = VT.getVectorNumElements();
3698 WhichResult = (M[0] == 0 ? 0 : 1);
3699 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003700 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3701 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003702 return false;
3703 }
3704 return true;
3705}
3706
Bob Wilson324f4f12009-12-03 06:40:55 +00003707/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3708/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3709/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3710static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3711 unsigned &WhichResult) {
3712 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3713 if (EltSz == 64)
3714 return false;
3715
3716 unsigned NumElts = VT.getVectorNumElements();
3717 WhichResult = (M[0] == 0 ? 0 : 1);
3718 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003719 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3720 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003721 return false;
3722 }
3723 return true;
3724}
3725
Bob Wilsonc692cb72009-08-21 20:54:19 +00003726static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3727 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003728 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3729 if (EltSz == 64)
3730 return false;
3731
Bob Wilsonc692cb72009-08-21 20:54:19 +00003732 unsigned NumElts = VT.getVectorNumElements();
3733 WhichResult = (M[0] == 0 ? 0 : 1);
3734 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003735 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003736 if ((unsigned) M[i] != 2 * i + WhichResult)
3737 return false;
3738 }
3739
3740 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003741 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003742 return false;
3743
3744 return true;
3745}
3746
Bob Wilson324f4f12009-12-03 06:40:55 +00003747/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3748/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3749/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3750static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3751 unsigned &WhichResult) {
3752 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3753 if (EltSz == 64)
3754 return false;
3755
3756 unsigned Half = VT.getVectorNumElements() / 2;
3757 WhichResult = (M[0] == 0 ? 0 : 1);
3758 for (unsigned j = 0; j != 2; ++j) {
3759 unsigned Idx = WhichResult;
3760 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003761 int MIdx = M[i + j * Half];
3762 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003763 return false;
3764 Idx += 2;
3765 }
3766 }
3767
3768 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3769 if (VT.is64BitVector() && EltSz == 32)
3770 return false;
3771
3772 return true;
3773}
3774
Bob Wilsonc692cb72009-08-21 20:54:19 +00003775static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3776 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003777 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3778 if (EltSz == 64)
3779 return false;
3780
Bob Wilsonc692cb72009-08-21 20:54:19 +00003781 unsigned NumElts = VT.getVectorNumElements();
3782 WhichResult = (M[0] == 0 ? 0 : 1);
3783 unsigned Idx = WhichResult * NumElts / 2;
3784 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003785 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3786 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003787 return false;
3788 Idx += 1;
3789 }
3790
3791 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003792 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003793 return false;
3794
3795 return true;
3796}
3797
Bob Wilson324f4f12009-12-03 06:40:55 +00003798/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3799/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3800/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3801static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3802 unsigned &WhichResult) {
3803 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3804 if (EltSz == 64)
3805 return false;
3806
3807 unsigned NumElts = VT.getVectorNumElements();
3808 WhichResult = (M[0] == 0 ? 0 : 1);
3809 unsigned Idx = WhichResult * NumElts / 2;
3810 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003811 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3812 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003813 return false;
3814 Idx += 1;
3815 }
3816
3817 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3818 if (VT.is64BitVector() && EltSz == 32)
3819 return false;
3820
3821 return true;
3822}
3823
Dale Johannesenf630c712010-07-29 20:10:08 +00003824// If N is an integer constant that can be moved into a register in one
3825// instruction, return an SDValue of such a constant (will become a MOV
3826// instruction). Otherwise return null.
3827static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3828 const ARMSubtarget *ST, DebugLoc dl) {
3829 uint64_t Val;
3830 if (!isa<ConstantSDNode>(N))
3831 return SDValue();
3832 Val = cast<ConstantSDNode>(N)->getZExtValue();
3833
3834 if (ST->isThumb1Only()) {
3835 if (Val <= 255 || ~Val <= 255)
3836 return DAG.getConstant(Val, MVT::i32);
3837 } else {
3838 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3839 return DAG.getConstant(Val, MVT::i32);
3840 }
3841 return SDValue();
3842}
3843
Bob Wilson5bafff32009-06-22 23:27:02 +00003844// If this is a case we can't handle, return null and let the default
3845// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003846SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3847 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003848 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003849 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003850 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003851
3852 APInt SplatBits, SplatUndef;
3853 unsigned SplatBitSize;
3854 bool HasAnyUndefs;
3855 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003856 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003857 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003858 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003859 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003860 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003861 DAG, VmovVT, VT.is128BitVector(),
3862 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003863 if (Val.getNode()) {
3864 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003865 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003866 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003867
3868 // Try an immediate VMVN.
3869 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3870 ((1LL << SplatBitSize) - 1));
3871 Val = isNEONModifiedImm(NegatedImm,
3872 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003873 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003874 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003875 if (Val.getNode()) {
3876 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003877 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003878 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003879 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003880 }
3881
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003882 // Scan through the operands to see if only one value is used.
3883 unsigned NumElts = VT.getVectorNumElements();
3884 bool isOnlyLowElement = true;
3885 bool usesOnlyOneValue = true;
3886 bool isConstant = true;
3887 SDValue Value;
3888 for (unsigned i = 0; i < NumElts; ++i) {
3889 SDValue V = Op.getOperand(i);
3890 if (V.getOpcode() == ISD::UNDEF)
3891 continue;
3892 if (i > 0)
3893 isOnlyLowElement = false;
3894 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3895 isConstant = false;
3896
3897 if (!Value.getNode())
3898 Value = V;
3899 else if (V != Value)
3900 usesOnlyOneValue = false;
3901 }
3902
3903 if (!Value.getNode())
3904 return DAG.getUNDEF(VT);
3905
3906 if (isOnlyLowElement)
3907 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3908
Dale Johannesenf630c712010-07-29 20:10:08 +00003909 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3910
Dale Johannesen575cd142010-10-19 20:00:17 +00003911 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3912 // i32 and try again.
3913 if (usesOnlyOneValue && EltSize <= 32) {
3914 if (!isConstant)
3915 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3916 if (VT.getVectorElementType().isFloatingPoint()) {
3917 SmallVector<SDValue, 8> Ops;
3918 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003919 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003920 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003921 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3922 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003923 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3924 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003925 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003926 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003927 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3928 if (Val.getNode())
3929 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003930 }
3931
3932 // If all elements are constants and the case above didn't get hit, fall back
3933 // to the default expansion, which will generate a load from the constant
3934 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003935 if (isConstant)
3936 return SDValue();
3937
Bob Wilson11a1dff2011-01-07 21:37:30 +00003938 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3939 if (NumElts >= 4) {
3940 SDValue shuffle = ReconstructShuffle(Op, DAG);
3941 if (shuffle != SDValue())
3942 return shuffle;
3943 }
3944
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003945 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003946 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3947 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003948 if (EltSize >= 32) {
3949 // Do the expansion with floating-point types, since that is what the VFP
3950 // registers are defined to use, and since i64 is not legal.
3951 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3952 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003953 SmallVector<SDValue, 8> Ops;
3954 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003955 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003956 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003957 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003958 }
3959
3960 return SDValue();
3961}
3962
Bob Wilson11a1dff2011-01-07 21:37:30 +00003963// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003964// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003965SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3966 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003967 DebugLoc dl = Op.getDebugLoc();
3968 EVT VT = Op.getValueType();
3969 unsigned NumElts = VT.getVectorNumElements();
3970
3971 SmallVector<SDValue, 2> SourceVecs;
3972 SmallVector<unsigned, 2> MinElts;
3973 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003974
Bob Wilson11a1dff2011-01-07 21:37:30 +00003975 for (unsigned i = 0; i < NumElts; ++i) {
3976 SDValue V = Op.getOperand(i);
3977 if (V.getOpcode() == ISD::UNDEF)
3978 continue;
3979 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3980 // A shuffle can only come from building a vector from various
3981 // elements of other vectors.
3982 return SDValue();
3983 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003984
Bob Wilson11a1dff2011-01-07 21:37:30 +00003985 // Record this extraction against the appropriate vector if possible...
3986 SDValue SourceVec = V.getOperand(0);
3987 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3988 bool FoundSource = false;
3989 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3990 if (SourceVecs[j] == SourceVec) {
3991 if (MinElts[j] > EltNo)
3992 MinElts[j] = EltNo;
3993 if (MaxElts[j] < EltNo)
3994 MaxElts[j] = EltNo;
3995 FoundSource = true;
3996 break;
3997 }
3998 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003999
Bob Wilson11a1dff2011-01-07 21:37:30 +00004000 // Or record a new source if not...
4001 if (!FoundSource) {
4002 SourceVecs.push_back(SourceVec);
4003 MinElts.push_back(EltNo);
4004 MaxElts.push_back(EltNo);
4005 }
4006 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004007
Bob Wilson11a1dff2011-01-07 21:37:30 +00004008 // Currently only do something sane when at most two source vectors
4009 // involved.
4010 if (SourceVecs.size() > 2)
4011 return SDValue();
4012
4013 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4014 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004015
Bob Wilson11a1dff2011-01-07 21:37:30 +00004016 // This loop extracts the usage patterns of the source vectors
4017 // and prepares appropriate SDValues for a shuffle if possible.
4018 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4019 if (SourceVecs[i].getValueType() == VT) {
4020 // No VEXT necessary
4021 ShuffleSrcs[i] = SourceVecs[i];
4022 VEXTOffsets[i] = 0;
4023 continue;
4024 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4025 // It probably isn't worth padding out a smaller vector just to
4026 // break it down again in a shuffle.
4027 return SDValue();
4028 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004029
Bob Wilson11a1dff2011-01-07 21:37:30 +00004030 // Since only 64-bit and 128-bit vectors are legal on ARM and
4031 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004032 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4033 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004034
Bob Wilson11a1dff2011-01-07 21:37:30 +00004035 if (MaxElts[i] - MinElts[i] >= NumElts) {
4036 // Span too large for a VEXT to cope
4037 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004038 }
4039
Bob Wilson11a1dff2011-01-07 21:37:30 +00004040 if (MinElts[i] >= NumElts) {
4041 // The extraction can just take the second half
4042 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004043 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4044 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004045 DAG.getIntPtrConstant(NumElts));
4046 } else if (MaxElts[i] < NumElts) {
4047 // The extraction can just take the first half
4048 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004049 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4050 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004051 DAG.getIntPtrConstant(0));
4052 } else {
4053 // An actual VEXT is needed
4054 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004055 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4056 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004057 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004058 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4059 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004060 DAG.getIntPtrConstant(NumElts));
4061 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4062 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4063 }
4064 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004065
Bob Wilson11a1dff2011-01-07 21:37:30 +00004066 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004067
Bob Wilson11a1dff2011-01-07 21:37:30 +00004068 for (unsigned i = 0; i < NumElts; ++i) {
4069 SDValue Entry = Op.getOperand(i);
4070 if (Entry.getOpcode() == ISD::UNDEF) {
4071 Mask.push_back(-1);
4072 continue;
4073 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004074
Bob Wilson11a1dff2011-01-07 21:37:30 +00004075 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004076 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4077 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004078 if (ExtractVec == SourceVecs[0]) {
4079 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4080 } else {
4081 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4082 }
4083 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004084
Bob Wilson11a1dff2011-01-07 21:37:30 +00004085 // Final check before we try to produce nonsense...
4086 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004087 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4088 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004089
Bob Wilson11a1dff2011-01-07 21:37:30 +00004090 return SDValue();
4091}
4092
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004093/// isShuffleMaskLegal - Targets can use this to indicate that they only
4094/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4095/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4096/// are assumed to be legal.
4097bool
4098ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4099 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004100 if (VT.getVectorNumElements() == 4 &&
4101 (VT.is128BitVector() || VT.is64BitVector())) {
4102 unsigned PFIndexes[4];
4103 for (unsigned i = 0; i != 4; ++i) {
4104 if (M[i] < 0)
4105 PFIndexes[i] = 8;
4106 else
4107 PFIndexes[i] = M[i];
4108 }
4109
4110 // Compute the index in the perfect shuffle table.
4111 unsigned PFTableIndex =
4112 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4113 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4114 unsigned Cost = (PFEntry >> 30);
4115
4116 if (Cost <= 4)
4117 return true;
4118 }
4119
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004120 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004121 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004122
Bob Wilson53dd2452010-06-07 23:53:38 +00004123 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4124 return (EltSize >= 32 ||
4125 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004126 isVREVMask(M, VT, 64) ||
4127 isVREVMask(M, VT, 32) ||
4128 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004129 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004130 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004131 isVTRNMask(M, VT, WhichResult) ||
4132 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004133 isVZIPMask(M, VT, WhichResult) ||
4134 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4135 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4136 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004137}
4138
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004139/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4140/// the specified operations to build the shuffle.
4141static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4142 SDValue RHS, SelectionDAG &DAG,
4143 DebugLoc dl) {
4144 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4145 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4146 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4147
4148 enum {
4149 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4150 OP_VREV,
4151 OP_VDUP0,
4152 OP_VDUP1,
4153 OP_VDUP2,
4154 OP_VDUP3,
4155 OP_VEXT1,
4156 OP_VEXT2,
4157 OP_VEXT3,
4158 OP_VUZPL, // VUZP, left result
4159 OP_VUZPR, // VUZP, right result
4160 OP_VZIPL, // VZIP, left result
4161 OP_VZIPR, // VZIP, right result
4162 OP_VTRNL, // VTRN, left result
4163 OP_VTRNR // VTRN, right result
4164 };
4165
4166 if (OpNum == OP_COPY) {
4167 if (LHSID == (1*9+2)*9+3) return LHS;
4168 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4169 return RHS;
4170 }
4171
4172 SDValue OpLHS, OpRHS;
4173 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4174 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4175 EVT VT = OpLHS.getValueType();
4176
4177 switch (OpNum) {
4178 default: llvm_unreachable("Unknown shuffle opcode!");
4179 case OP_VREV:
4180 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4181 case OP_VDUP0:
4182 case OP_VDUP1:
4183 case OP_VDUP2:
4184 case OP_VDUP3:
4185 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004186 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004187 case OP_VEXT1:
4188 case OP_VEXT2:
4189 case OP_VEXT3:
4190 return DAG.getNode(ARMISD::VEXT, dl, VT,
4191 OpLHS, OpRHS,
4192 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4193 case OP_VUZPL:
4194 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004195 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004196 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4197 case OP_VZIPL:
4198 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004199 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004200 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4201 case OP_VTRNL:
4202 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004203 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4204 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004205 }
4206}
4207
Bill Wendling69a05a72011-03-14 23:02:38 +00004208static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4209 SmallVectorImpl<int> &ShuffleMask,
4210 SelectionDAG &DAG) {
4211 // Check to see if we can use the VTBL instruction.
4212 SDValue V1 = Op.getOperand(0);
4213 SDValue V2 = Op.getOperand(1);
4214 DebugLoc DL = Op.getDebugLoc();
4215
4216 SmallVector<SDValue, 8> VTBLMask;
4217 for (SmallVectorImpl<int>::iterator
4218 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4219 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4220
4221 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4222 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4223 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4224 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004225
Owen Anderson76706012011-04-05 21:48:57 +00004226 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004227 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4228 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004229}
4230
Bob Wilson5bafff32009-06-22 23:27:02 +00004231static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004232 SDValue V1 = Op.getOperand(0);
4233 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004234 DebugLoc dl = Op.getDebugLoc();
4235 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004236 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004237 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004238
Bob Wilson28865062009-08-13 02:13:04 +00004239 // Convert shuffles that are directly supported on NEON to target-specific
4240 // DAG nodes, instead of keeping them as shuffles and matching them again
4241 // during code selection. This is more efficient and avoids the possibility
4242 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004243 // FIXME: floating-point vectors should be canonicalized to integer vectors
4244 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004245 SVN->getMask(ShuffleMask);
4246
Bob Wilson53dd2452010-06-07 23:53:38 +00004247 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4248 if (EltSize <= 32) {
4249 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4250 int Lane = SVN->getSplatIndex();
4251 // If this is undef splat, generate it via "just" vdup, if possible.
4252 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004253
Bob Wilson53dd2452010-06-07 23:53:38 +00004254 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4255 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4256 }
4257 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4258 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004259 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004260
4261 bool ReverseVEXT;
4262 unsigned Imm;
4263 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4264 if (ReverseVEXT)
4265 std::swap(V1, V2);
4266 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4267 DAG.getConstant(Imm, MVT::i32));
4268 }
4269
4270 if (isVREVMask(ShuffleMask, VT, 64))
4271 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4272 if (isVREVMask(ShuffleMask, VT, 32))
4273 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4274 if (isVREVMask(ShuffleMask, VT, 16))
4275 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4276
4277 // Check for Neon shuffles that modify both input vectors in place.
4278 // If both results are used, i.e., if there are two shuffles with the same
4279 // source operands and with masks corresponding to both results of one of
4280 // these operations, DAG memoization will ensure that a single node is
4281 // used for both shuffles.
4282 unsigned WhichResult;
4283 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4284 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4285 V1, V2).getValue(WhichResult);
4286 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4287 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4288 V1, V2).getValue(WhichResult);
4289 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4290 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4291 V1, V2).getValue(WhichResult);
4292
4293 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4294 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4295 V1, V1).getValue(WhichResult);
4296 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4297 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4298 V1, V1).getValue(WhichResult);
4299 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4300 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4301 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004302 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004303
Bob Wilsonc692cb72009-08-21 20:54:19 +00004304 // If the shuffle is not directly supported and it has 4 elements, use
4305 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004306 unsigned NumElts = VT.getVectorNumElements();
4307 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004308 unsigned PFIndexes[4];
4309 for (unsigned i = 0; i != 4; ++i) {
4310 if (ShuffleMask[i] < 0)
4311 PFIndexes[i] = 8;
4312 else
4313 PFIndexes[i] = ShuffleMask[i];
4314 }
4315
4316 // Compute the index in the perfect shuffle table.
4317 unsigned PFTableIndex =
4318 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004319 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4320 unsigned Cost = (PFEntry >> 30);
4321
4322 if (Cost <= 4)
4323 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4324 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004325
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004326 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004327 if (EltSize >= 32) {
4328 // Do the expansion with floating-point types, since that is what the VFP
4329 // registers are defined to use, and since i64 is not legal.
4330 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4331 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004332 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4333 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004334 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004335 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004336 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004337 Ops.push_back(DAG.getUNDEF(EltVT));
4338 else
4339 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4340 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4341 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4342 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004343 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004344 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004345 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004346 }
4347
Bill Wendling69a05a72011-03-14 23:02:38 +00004348 if (VT == MVT::v8i8) {
4349 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4350 if (NewOp.getNode())
4351 return NewOp;
4352 }
4353
Bob Wilson22cac0d2009-08-14 05:16:33 +00004354 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004355}
4356
Bob Wilson5bafff32009-06-22 23:27:02 +00004357static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004358 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004359 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004360 if (!isa<ConstantSDNode>(Lane))
4361 return SDValue();
4362
4363 SDValue Vec = Op.getOperand(0);
4364 if (Op.getValueType() == MVT::i32 &&
4365 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4366 DebugLoc dl = Op.getDebugLoc();
4367 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4368 }
4369
4370 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004371}
4372
Bob Wilsona6d65862009-08-03 20:36:38 +00004373static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4374 // The only time a CONCAT_VECTORS operation can have legal types is when
4375 // two 64-bit vectors are concatenated to a 128-bit vector.
4376 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4377 "unexpected CONCAT_VECTORS");
4378 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004379 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004380 SDValue Op0 = Op.getOperand(0);
4381 SDValue Op1 = Op.getOperand(1);
4382 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004383 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004384 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004385 DAG.getIntPtrConstant(0));
4386 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004387 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004388 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004389 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004390 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004391}
4392
Bob Wilson626613d2010-11-23 19:38:38 +00004393/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4394/// element has been zero/sign-extended, depending on the isSigned parameter,
4395/// from an integer type half its size.
4396static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4397 bool isSigned) {
4398 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4399 EVT VT = N->getValueType(0);
4400 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4401 SDNode *BVN = N->getOperand(0).getNode();
4402 if (BVN->getValueType(0) != MVT::v4i32 ||
4403 BVN->getOpcode() != ISD::BUILD_VECTOR)
4404 return false;
4405 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4406 unsigned HiElt = 1 - LoElt;
4407 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4408 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4409 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4410 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4411 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4412 return false;
4413 if (isSigned) {
4414 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4415 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4416 return true;
4417 } else {
4418 if (Hi0->isNullValue() && Hi1->isNullValue())
4419 return true;
4420 }
4421 return false;
4422 }
4423
4424 if (N->getOpcode() != ISD::BUILD_VECTOR)
4425 return false;
4426
4427 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4428 SDNode *Elt = N->getOperand(i).getNode();
4429 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4430 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4431 unsigned HalfSize = EltSize / 2;
4432 if (isSigned) {
4433 int64_t SExtVal = C->getSExtValue();
4434 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4435 return false;
4436 } else {
4437 if ((C->getZExtValue() >> HalfSize) != 0)
4438 return false;
4439 }
4440 continue;
4441 }
4442 return false;
4443 }
4444
4445 return true;
4446}
4447
4448/// isSignExtended - Check if a node is a vector value that is sign-extended
4449/// or a constant BUILD_VECTOR with sign-extended elements.
4450static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4451 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4452 return true;
4453 if (isExtendedBUILD_VECTOR(N, DAG, true))
4454 return true;
4455 return false;
4456}
4457
4458/// isZeroExtended - Check if a node is a vector value that is zero-extended
4459/// or a constant BUILD_VECTOR with zero-extended elements.
4460static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4461 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4462 return true;
4463 if (isExtendedBUILD_VECTOR(N, DAG, false))
4464 return true;
4465 return false;
4466}
4467
4468/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4469/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004470static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4471 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4472 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004473 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4474 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4475 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4476 LD->isNonTemporal(), LD->getAlignment());
4477 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4478 // have been legalized as a BITCAST from v4i32.
4479 if (N->getOpcode() == ISD::BITCAST) {
4480 SDNode *BVN = N->getOperand(0).getNode();
4481 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4482 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4483 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4484 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4485 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4486 }
4487 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4488 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4489 EVT VT = N->getValueType(0);
4490 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4491 unsigned NumElts = VT.getVectorNumElements();
4492 MVT TruncVT = MVT::getIntegerVT(EltSize);
4493 SmallVector<SDValue, 8> Ops;
4494 for (unsigned i = 0; i != NumElts; ++i) {
4495 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4496 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004497 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004498 }
4499 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4500 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004501}
4502
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004503static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4504 unsigned Opcode = N->getOpcode();
4505 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4506 SDNode *N0 = N->getOperand(0).getNode();
4507 SDNode *N1 = N->getOperand(1).getNode();
4508 return N0->hasOneUse() && N1->hasOneUse() &&
4509 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4510 }
4511 return false;
4512}
4513
4514static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4515 unsigned Opcode = N->getOpcode();
4516 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4517 SDNode *N0 = N->getOperand(0).getNode();
4518 SDNode *N1 = N->getOperand(1).getNode();
4519 return N0->hasOneUse() && N1->hasOneUse() &&
4520 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4521 }
4522 return false;
4523}
4524
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004525static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4526 // Multiplications are only custom-lowered for 128-bit vectors so that
4527 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4528 EVT VT = Op.getValueType();
4529 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4530 SDNode *N0 = Op.getOperand(0).getNode();
4531 SDNode *N1 = Op.getOperand(1).getNode();
4532 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004533 bool isMLA = false;
4534 bool isN0SExt = isSignExtended(N0, DAG);
4535 bool isN1SExt = isSignExtended(N1, DAG);
4536 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004537 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004538 else {
4539 bool isN0ZExt = isZeroExtended(N0, DAG);
4540 bool isN1ZExt = isZeroExtended(N1, DAG);
4541 if (isN0ZExt && isN1ZExt)
4542 NewOpc = ARMISD::VMULLu;
4543 else if (isN1SExt || isN1ZExt) {
4544 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4545 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4546 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4547 NewOpc = ARMISD::VMULLs;
4548 isMLA = true;
4549 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4550 NewOpc = ARMISD::VMULLu;
4551 isMLA = true;
4552 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4553 std::swap(N0, N1);
4554 NewOpc = ARMISD::VMULLu;
4555 isMLA = true;
4556 }
4557 }
4558
4559 if (!NewOpc) {
4560 if (VT == MVT::v2i64)
4561 // Fall through to expand this. It is not legal.
4562 return SDValue();
4563 else
4564 // Other vector multiplications are legal.
4565 return Op;
4566 }
4567 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004568
4569 // Legalize to a VMULL instruction.
4570 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004571 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004572 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004573 if (!isMLA) {
4574 Op0 = SkipExtension(N0, DAG);
4575 assert(Op0.getValueType().is64BitVector() &&
4576 Op1.getValueType().is64BitVector() &&
4577 "unexpected types for extended operands to VMULL");
4578 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4579 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004580
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004581 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4582 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4583 // vmull q0, d4, d6
4584 // vmlal q0, d5, d6
4585 // is faster than
4586 // vaddl q0, d4, d5
4587 // vmovl q1, d6
4588 // vmul q0, q0, q1
4589 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4590 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4591 EVT Op1VT = Op1.getValueType();
4592 return DAG.getNode(N0->getOpcode(), DL, VT,
4593 DAG.getNode(NewOpc, DL, VT,
4594 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4595 DAG.getNode(NewOpc, DL, VT,
4596 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004597}
4598
Owen Anderson76706012011-04-05 21:48:57 +00004599static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004600LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4601 // Convert to float
4602 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4603 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4604 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4605 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4606 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4607 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4608 // Get reciprocal estimate.
4609 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004610 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004611 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4612 // Because char has a smaller range than uchar, we can actually get away
4613 // without any newton steps. This requires that we use a weird bias
4614 // of 0xb000, however (again, this has been exhaustively tested).
4615 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4616 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4617 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4618 Y = DAG.getConstant(0xb000, MVT::i32);
4619 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4620 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4621 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4622 // Convert back to short.
4623 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4624 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4625 return X;
4626}
4627
Owen Anderson76706012011-04-05 21:48:57 +00004628static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004629LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4630 SDValue N2;
4631 // Convert to float.
4632 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4633 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4634 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4635 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4636 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4637 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004638
Nate Begeman7973f352011-02-11 20:53:29 +00004639 // Use reciprocal estimate and one refinement step.
4640 // float4 recip = vrecpeq_f32(yf);
4641 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004642 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004643 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004644 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004645 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4646 N1, N2);
4647 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4648 // Because short has a smaller range than ushort, we can actually get away
4649 // with only a single newton step. This requires that we use a weird bias
4650 // of 89, however (again, this has been exhaustively tested).
4651 // float4 result = as_float4(as_int4(xf*recip) + 89);
4652 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4653 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4654 N1 = DAG.getConstant(89, MVT::i32);
4655 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4656 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4657 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4658 // Convert back to integer and return.
4659 // return vmovn_s32(vcvt_s32_f32(result));
4660 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4661 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4662 return N0;
4663}
4664
4665static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4666 EVT VT = Op.getValueType();
4667 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4668 "unexpected type for custom-lowering ISD::SDIV");
4669
4670 DebugLoc dl = Op.getDebugLoc();
4671 SDValue N0 = Op.getOperand(0);
4672 SDValue N1 = Op.getOperand(1);
4673 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004674
Nate Begeman7973f352011-02-11 20:53:29 +00004675 if (VT == MVT::v8i8) {
4676 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4677 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004678
Nate Begeman7973f352011-02-11 20:53:29 +00004679 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4680 DAG.getIntPtrConstant(4));
4681 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004682 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004683 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4684 DAG.getIntPtrConstant(0));
4685 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4686 DAG.getIntPtrConstant(0));
4687
4688 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4689 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4690
4691 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4692 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004693
Nate Begeman7973f352011-02-11 20:53:29 +00004694 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4695 return N0;
4696 }
4697 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4698}
4699
4700static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4701 EVT VT = Op.getValueType();
4702 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4703 "unexpected type for custom-lowering ISD::UDIV");
4704
4705 DebugLoc dl = Op.getDebugLoc();
4706 SDValue N0 = Op.getOperand(0);
4707 SDValue N1 = Op.getOperand(1);
4708 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004709
Nate Begeman7973f352011-02-11 20:53:29 +00004710 if (VT == MVT::v8i8) {
4711 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4712 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004713
Nate Begeman7973f352011-02-11 20:53:29 +00004714 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4715 DAG.getIntPtrConstant(4));
4716 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004717 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004718 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4719 DAG.getIntPtrConstant(0));
4720 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4721 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004722
Nate Begeman7973f352011-02-11 20:53:29 +00004723 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4724 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004725
Nate Begeman7973f352011-02-11 20:53:29 +00004726 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4727 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004728
4729 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004730 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4731 N0);
4732 return N0;
4733 }
Owen Anderson76706012011-04-05 21:48:57 +00004734
Nate Begeman7973f352011-02-11 20:53:29 +00004735 // v4i16 sdiv ... Convert to float.
4736 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4737 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4738 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4739 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4740 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4741 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4742
4743 // Use reciprocal estimate and two refinement steps.
4744 // float4 recip = vrecpeq_f32(yf);
4745 // recip *= vrecpsq_f32(yf, recip);
4746 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004747 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004748 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004749 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004750 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4751 N1, N2);
4752 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004753 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004754 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4755 N1, N2);
4756 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4757 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4758 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4759 // and that it will never cause us to return an answer too large).
4760 // float4 result = as_float4(as_int4(xf*recip) + 89);
4761 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4762 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4763 N1 = DAG.getConstant(2, MVT::i32);
4764 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4765 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4766 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4767 // Convert back to integer and return.
4768 // return vmovn_u32(vcvt_s32_f32(result));
4769 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4770 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4771 return N0;
4772}
4773
Dan Gohmand858e902010-04-17 15:26:15 +00004774SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004775 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004776 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004777 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004778 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004779 case ISD::GlobalAddress:
4780 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4781 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004782 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004783 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004784 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4785 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004786 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004787 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004788 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004789 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004790 case ISD::SINT_TO_FP:
4791 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4792 case ISD::FP_TO_SINT:
4793 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004794 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004795 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004796 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004797 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004798 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004799 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004800 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004801 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4802 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004803 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004804 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004805 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004806 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004807 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004808 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004809 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004810 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004811 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004812 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004813 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004814 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004815 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004816 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004817 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004818 case ISD::SDIV: return LowerSDIV(Op, DAG);
4819 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004820 }
Dan Gohman475871a2008-07-27 21:46:04 +00004821 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004822}
4823
Duncan Sands1607f052008-12-01 11:39:25 +00004824/// ReplaceNodeResults - Replace the results of node with an illegal result
4825/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004826void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4827 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004828 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004829 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004830 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004831 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004832 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004833 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004834 case ISD::BITCAST:
4835 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004836 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004837 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004838 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004839 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004840 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004841 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004842 if (Res.getNode())
4843 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004844}
Chris Lattner27a6c732007-11-24 07:07:01 +00004845
Evan Chenga8e29892007-01-19 07:51:42 +00004846//===----------------------------------------------------------------------===//
4847// ARM Scheduler Hooks
4848//===----------------------------------------------------------------------===//
4849
4850MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004851ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4852 MachineBasicBlock *BB,
4853 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004854 unsigned dest = MI->getOperand(0).getReg();
4855 unsigned ptr = MI->getOperand(1).getReg();
4856 unsigned oldval = MI->getOperand(2).getReg();
4857 unsigned newval = MI->getOperand(3).getReg();
4858 unsigned scratch = BB->getParent()->getRegInfo()
4859 .createVirtualRegister(ARM::GPRRegisterClass);
4860 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4861 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004862 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004863
4864 unsigned ldrOpc, strOpc;
4865 switch (Size) {
4866 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004867 case 1:
4868 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004869 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004870 break;
4871 case 2:
4872 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4873 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4874 break;
4875 case 4:
4876 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4877 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4878 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004879 }
4880
4881 MachineFunction *MF = BB->getParent();
4882 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4883 MachineFunction::iterator It = BB;
4884 ++It; // insert the new blocks after the current block
4885
4886 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4887 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4888 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4889 MF->insert(It, loop1MBB);
4890 MF->insert(It, loop2MBB);
4891 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004892
4893 // Transfer the remainder of BB and its successor edges to exitMBB.
4894 exitMBB->splice(exitMBB->begin(), BB,
4895 llvm::next(MachineBasicBlock::iterator(MI)),
4896 BB->end());
4897 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004898
4899 // thisMBB:
4900 // ...
4901 // fallthrough --> loop1MBB
4902 BB->addSuccessor(loop1MBB);
4903
4904 // loop1MBB:
4905 // ldrex dest, [ptr]
4906 // cmp dest, oldval
4907 // bne exitMBB
4908 BB = loop1MBB;
4909 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004910 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004911 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004912 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4913 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004914 BB->addSuccessor(loop2MBB);
4915 BB->addSuccessor(exitMBB);
4916
4917 // loop2MBB:
4918 // strex scratch, newval, [ptr]
4919 // cmp scratch, #0
4920 // bne loop1MBB
4921 BB = loop2MBB;
4922 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4923 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004924 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004925 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004926 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4927 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004928 BB->addSuccessor(loop1MBB);
4929 BB->addSuccessor(exitMBB);
4930
4931 // exitMBB:
4932 // ...
4933 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004934
Dan Gohman14152b42010-07-06 20:24:04 +00004935 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004936
Jim Grosbach5278eb82009-12-11 01:42:04 +00004937 return BB;
4938}
4939
4940MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004941ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4942 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004943 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4944 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4945
4946 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004947 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004948 MachineFunction::iterator It = BB;
4949 ++It;
4950
4951 unsigned dest = MI->getOperand(0).getReg();
4952 unsigned ptr = MI->getOperand(1).getReg();
4953 unsigned incr = MI->getOperand(2).getReg();
4954 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004955
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004956 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004957 unsigned ldrOpc, strOpc;
4958 switch (Size) {
4959 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004960 case 1:
4961 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004962 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004963 break;
4964 case 2:
4965 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4966 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4967 break;
4968 case 4:
4969 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4970 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4971 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004972 }
4973
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004974 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4975 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4976 MF->insert(It, loopMBB);
4977 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004978
4979 // Transfer the remainder of BB and its successor edges to exitMBB.
4980 exitMBB->splice(exitMBB->begin(), BB,
4981 llvm::next(MachineBasicBlock::iterator(MI)),
4982 BB->end());
4983 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004984
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004985 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004986 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4987 unsigned scratch2 = (!BinOpcode) ? incr :
4988 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4989
4990 // thisMBB:
4991 // ...
4992 // fallthrough --> loopMBB
4993 BB->addSuccessor(loopMBB);
4994
4995 // loopMBB:
4996 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004997 // <binop> scratch2, dest, incr
4998 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004999 // cmp scratch, #0
5000 // bne- loopMBB
5001 // fallthrough --> exitMBB
5002 BB = loopMBB;
5003 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005004 if (BinOpcode) {
5005 // operand order needs to go the other way for NAND
5006 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5007 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5008 addReg(incr).addReg(dest)).addReg(0);
5009 else
5010 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5011 addReg(dest).addReg(incr)).addReg(0);
5012 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005013
5014 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5015 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005016 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005017 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005018 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5019 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005020
5021 BB->addSuccessor(loopMBB);
5022 BB->addSuccessor(exitMBB);
5023
5024 // exitMBB:
5025 // ...
5026 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005027
Dan Gohman14152b42010-07-06 20:24:04 +00005028 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005029
Jim Grosbachc3c23542009-12-14 04:22:04 +00005030 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005031}
5032
Evan Cheng218977b2010-07-13 19:27:42 +00005033static
5034MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5035 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5036 E = MBB->succ_end(); I != E; ++I)
5037 if (*I != Succ)
5038 return *I;
5039 llvm_unreachable("Expecting a BB with two successors!");
5040}
5041
Jim Grosbache801dc42009-12-12 01:40:06 +00005042MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005043ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005044 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005045 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005046 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005047 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005048 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00005049 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00005050 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005051 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00005052
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005053 case ARM::ATOMIC_LOAD_ADD_I8:
5054 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5055 case ARM::ATOMIC_LOAD_ADD_I16:
5056 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5057 case ARM::ATOMIC_LOAD_ADD_I32:
5058 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005059
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005060 case ARM::ATOMIC_LOAD_AND_I8:
5061 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5062 case ARM::ATOMIC_LOAD_AND_I16:
5063 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5064 case ARM::ATOMIC_LOAD_AND_I32:
5065 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005066
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005067 case ARM::ATOMIC_LOAD_OR_I8:
5068 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5069 case ARM::ATOMIC_LOAD_OR_I16:
5070 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5071 case ARM::ATOMIC_LOAD_OR_I32:
5072 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005073
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005074 case ARM::ATOMIC_LOAD_XOR_I8:
5075 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5076 case ARM::ATOMIC_LOAD_XOR_I16:
5077 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5078 case ARM::ATOMIC_LOAD_XOR_I32:
5079 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005080
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005081 case ARM::ATOMIC_LOAD_NAND_I8:
5082 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5083 case ARM::ATOMIC_LOAD_NAND_I16:
5084 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5085 case ARM::ATOMIC_LOAD_NAND_I32:
5086 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005087
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005088 case ARM::ATOMIC_LOAD_SUB_I8:
5089 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5090 case ARM::ATOMIC_LOAD_SUB_I16:
5091 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5092 case ARM::ATOMIC_LOAD_SUB_I32:
5093 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005094
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005095 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5096 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5097 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005098
5099 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5100 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5101 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005102
Owen Anderson76706012011-04-05 21:48:57 +00005103 case ARM::ADCSSri:
5104 case ARM::ADCSSrr:
5105 case ARM::ADCSSrs:
5106 case ARM::SBCSSri:
5107 case ARM::SBCSSrr:
Owen Andersonb48c7912011-04-05 23:55:28 +00005108 case ARM::SBCSSrs:
5109 case ARM::RSBSri:
5110 case ARM::RSBSrr:
5111 case ARM::RSBSrs:
5112 case ARM::RSCSri:
5113 case ARM::RSCSrs: {
Owen Anderson76706012011-04-05 21:48:57 +00005114 unsigned OldOpc = MI->getOpcode();
5115 unsigned Opc = 0;
5116 switch (OldOpc) {
5117 case ARM::ADCSSrr:
5118 Opc = ARM::ADCrr;
5119 break;
5120 case ARM::ADCSSri:
5121 Opc = ARM::ADCri;
5122 break;
5123 case ARM::ADCSSrs:
5124 Opc = ARM::ADCrs;
5125 break;
5126 case ARM::SBCSSrr:
5127 Opc = ARM::SBCrr;
5128 break;
5129 case ARM::SBCSSri:
5130 Opc = ARM::SBCri;
5131 break;
5132 case ARM::SBCSSrs:
5133 Opc = ARM::SBCrs;
5134 break;
Owen Andersonb48c7912011-04-05 23:55:28 +00005135 case ARM::RSBSri:
5136 Opc = ARM::RSBri;
5137 break;
5138 case ARM::RSBSrr:
5139 Opc = ARM::RSBrr;
5140 break;
5141 case ARM::RSBSrs:
5142 Opc = ARM::RSBrs;
5143 break;
5144 case ARM::RSCSri:
5145 Opc = ARM::RSCri;
5146 break;
5147 case ARM::RSCSrs:
5148 Opc = ARM::RSCrs;
5149 break;
Owen Anderson76706012011-04-05 21:48:57 +00005150 default:
5151 llvm_unreachable("Unknown opcode?");
5152 }
5153
5154 MachineInstrBuilder MIB =
5155 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(Opc));
5156 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5157 MIB.addOperand(MI->getOperand(i));
5158 AddDefaultPred(MIB);
5159 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5160 MI->eraseFromParent();
5161 return BB;
5162 }
5163
5164
Evan Cheng007ea272009-08-12 05:17:19 +00005165 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005166 // To "insert" a SELECT_CC instruction, we actually have to insert the
5167 // diamond control-flow pattern. The incoming instruction knows the
5168 // destination vreg to set, the condition code register to branch on, the
5169 // true/false values to select between, and a branch opcode to use.
5170 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005171 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005172 ++It;
5173
5174 // thisMBB:
5175 // ...
5176 // TrueVal = ...
5177 // cmpTY ccX, r1, r2
5178 // bCC copy1MBB
5179 // fallthrough --> copy0MBB
5180 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005181 MachineFunction *F = BB->getParent();
5182 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5183 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005184 F->insert(It, copy0MBB);
5185 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005186
5187 // Transfer the remainder of BB and its successor edges to sinkMBB.
5188 sinkMBB->splice(sinkMBB->begin(), BB,
5189 llvm::next(MachineBasicBlock::iterator(MI)),
5190 BB->end());
5191 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5192
Dan Gohman258c58c2010-07-06 15:49:48 +00005193 BB->addSuccessor(copy0MBB);
5194 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005195
Dan Gohman14152b42010-07-06 20:24:04 +00005196 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5197 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5198
Evan Chenga8e29892007-01-19 07:51:42 +00005199 // copy0MBB:
5200 // %FalseValue = ...
5201 // # fallthrough to sinkMBB
5202 BB = copy0MBB;
5203
5204 // Update machine-CFG edges
5205 BB->addSuccessor(sinkMBB);
5206
5207 // sinkMBB:
5208 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5209 // ...
5210 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005211 BuildMI(*BB, BB->begin(), dl,
5212 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005213 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5214 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5215
Dan Gohman14152b42010-07-06 20:24:04 +00005216 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005217 return BB;
5218 }
Evan Cheng86198642009-08-07 00:34:42 +00005219
Evan Cheng218977b2010-07-13 19:27:42 +00005220 case ARM::BCCi64:
5221 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005222 // If there is an unconditional branch to the other successor, remove it.
5223 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005224
Evan Cheng218977b2010-07-13 19:27:42 +00005225 // Compare both parts that make up the double comparison separately for
5226 // equality.
5227 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5228
5229 unsigned LHS1 = MI->getOperand(1).getReg();
5230 unsigned LHS2 = MI->getOperand(2).getReg();
5231 if (RHSisZero) {
5232 AddDefaultPred(BuildMI(BB, dl,
5233 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5234 .addReg(LHS1).addImm(0));
5235 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5236 .addReg(LHS2).addImm(0)
5237 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5238 } else {
5239 unsigned RHS1 = MI->getOperand(3).getReg();
5240 unsigned RHS2 = MI->getOperand(4).getReg();
5241 AddDefaultPred(BuildMI(BB, dl,
5242 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5243 .addReg(LHS1).addReg(RHS1));
5244 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5245 .addReg(LHS2).addReg(RHS2)
5246 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5247 }
5248
5249 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5250 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5251 if (MI->getOperand(0).getImm() == ARMCC::NE)
5252 std::swap(destMBB, exitMBB);
5253
5254 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5255 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5256 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5257 .addMBB(exitMBB);
5258
5259 MI->eraseFromParent(); // The pseudo instruction is gone now.
5260 return BB;
5261 }
Evan Chenga8e29892007-01-19 07:51:42 +00005262 }
5263}
5264
5265//===----------------------------------------------------------------------===//
5266// ARM Optimization Hooks
5267//===----------------------------------------------------------------------===//
5268
Chris Lattnerd1980a52009-03-12 06:52:53 +00005269static
5270SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5271 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005272 SelectionDAG &DAG = DCI.DAG;
5273 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005274 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005275 unsigned Opc = N->getOpcode();
5276 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5277 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5278 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5279 ISD::CondCode CC = ISD::SETCC_INVALID;
5280
5281 if (isSlctCC) {
5282 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5283 } else {
5284 SDValue CCOp = Slct.getOperand(0);
5285 if (CCOp.getOpcode() == ISD::SETCC)
5286 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5287 }
5288
5289 bool DoXform = false;
5290 bool InvCC = false;
5291 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5292 "Bad input!");
5293
5294 if (LHS.getOpcode() == ISD::Constant &&
5295 cast<ConstantSDNode>(LHS)->isNullValue()) {
5296 DoXform = true;
5297 } else if (CC != ISD::SETCC_INVALID &&
5298 RHS.getOpcode() == ISD::Constant &&
5299 cast<ConstantSDNode>(RHS)->isNullValue()) {
5300 std::swap(LHS, RHS);
5301 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005302 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005303 Op0.getOperand(0).getValueType();
5304 bool isInt = OpVT.isInteger();
5305 CC = ISD::getSetCCInverse(CC, isInt);
5306
5307 if (!TLI.isCondCodeLegal(CC, OpVT))
5308 return SDValue(); // Inverse operator isn't legal.
5309
5310 DoXform = true;
5311 InvCC = true;
5312 }
5313
5314 if (DoXform) {
5315 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5316 if (isSlctCC)
5317 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5318 Slct.getOperand(0), Slct.getOperand(1), CC);
5319 SDValue CCOp = Slct.getOperand(0);
5320 if (InvCC)
5321 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5322 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5323 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5324 CCOp, OtherOp, Result);
5325 }
5326 return SDValue();
5327}
5328
Bob Wilson3d5792a2010-07-29 20:34:14 +00005329/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5330/// operands N0 and N1. This is a helper for PerformADDCombine that is
5331/// called with the default operands, and if that fails, with commuted
5332/// operands.
5333static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5334 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005335 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5336 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5337 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5338 if (Result.getNode()) return Result;
5339 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005340 return SDValue();
5341}
5342
Bob Wilson3d5792a2010-07-29 20:34:14 +00005343/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5344///
5345static SDValue PerformADDCombine(SDNode *N,
5346 TargetLowering::DAGCombinerInfo &DCI) {
5347 SDValue N0 = N->getOperand(0);
5348 SDValue N1 = N->getOperand(1);
5349
5350 // First try with the default operand order.
5351 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5352 if (Result.getNode())
5353 return Result;
5354
5355 // If that didn't work, try again with the operands commuted.
5356 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5357}
5358
Chris Lattnerd1980a52009-03-12 06:52:53 +00005359/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005360///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005361static SDValue PerformSUBCombine(SDNode *N,
5362 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005363 SDValue N0 = N->getOperand(0);
5364 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005365
Chris Lattnerd1980a52009-03-12 06:52:53 +00005366 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5367 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5368 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5369 if (Result.getNode()) return Result;
5370 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005371
Chris Lattnerd1980a52009-03-12 06:52:53 +00005372 return SDValue();
5373}
5374
Evan Cheng463d3582011-03-31 19:38:48 +00005375/// PerformVMULCombine
5376/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5377/// special multiplier accumulator forwarding.
5378/// vmul d3, d0, d2
5379/// vmla d3, d1, d2
5380/// is faster than
5381/// vadd d3, d0, d1
5382/// vmul d3, d3, d2
5383static SDValue PerformVMULCombine(SDNode *N,
5384 TargetLowering::DAGCombinerInfo &DCI,
5385 const ARMSubtarget *Subtarget) {
5386 if (!Subtarget->hasVMLxForwarding())
5387 return SDValue();
5388
5389 SelectionDAG &DAG = DCI.DAG;
5390 SDValue N0 = N->getOperand(0);
5391 SDValue N1 = N->getOperand(1);
5392 unsigned Opcode = N0.getOpcode();
5393 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5394 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5395 Opcode = N0.getOpcode();
5396 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5397 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5398 return SDValue();
5399 std::swap(N0, N1);
5400 }
5401
5402 EVT VT = N->getValueType(0);
5403 DebugLoc DL = N->getDebugLoc();
5404 SDValue N00 = N0->getOperand(0);
5405 SDValue N01 = N0->getOperand(1);
5406 return DAG.getNode(Opcode, DL, VT,
5407 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5408 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5409}
5410
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005411static SDValue PerformMULCombine(SDNode *N,
5412 TargetLowering::DAGCombinerInfo &DCI,
5413 const ARMSubtarget *Subtarget) {
5414 SelectionDAG &DAG = DCI.DAG;
5415
5416 if (Subtarget->isThumb1Only())
5417 return SDValue();
5418
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005419 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5420 return SDValue();
5421
5422 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005423 if (VT.is64BitVector() || VT.is128BitVector())
5424 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005425 if (VT != MVT::i32)
5426 return SDValue();
5427
5428 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5429 if (!C)
5430 return SDValue();
5431
5432 uint64_t MulAmt = C->getZExtValue();
5433 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5434 ShiftAmt = ShiftAmt & (32 - 1);
5435 SDValue V = N->getOperand(0);
5436 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005437
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005438 SDValue Res;
5439 MulAmt >>= ShiftAmt;
5440 if (isPowerOf2_32(MulAmt - 1)) {
5441 // (mul x, 2^N + 1) => (add (shl x, N), x)
5442 Res = DAG.getNode(ISD::ADD, DL, VT,
5443 V, DAG.getNode(ISD::SHL, DL, VT,
5444 V, DAG.getConstant(Log2_32(MulAmt-1),
5445 MVT::i32)));
5446 } else if (isPowerOf2_32(MulAmt + 1)) {
5447 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5448 Res = DAG.getNode(ISD::SUB, DL, VT,
5449 DAG.getNode(ISD::SHL, DL, VT,
5450 V, DAG.getConstant(Log2_32(MulAmt+1),
5451 MVT::i32)),
5452 V);
5453 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005454 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005455
5456 if (ShiftAmt != 0)
5457 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5458 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005459
5460 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005461 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005462 return SDValue();
5463}
5464
Owen Anderson080c0922010-11-05 19:27:46 +00005465static SDValue PerformANDCombine(SDNode *N,
5466 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005467
Owen Anderson080c0922010-11-05 19:27:46 +00005468 // Attempt to use immediate-form VBIC
5469 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5470 DebugLoc dl = N->getDebugLoc();
5471 EVT VT = N->getValueType(0);
5472 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005473
Tanya Lattner0433b212011-04-07 15:24:20 +00005474 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5475 return SDValue();
5476
Owen Anderson080c0922010-11-05 19:27:46 +00005477 APInt SplatBits, SplatUndef;
5478 unsigned SplatBitSize;
5479 bool HasAnyUndefs;
5480 if (BVN &&
5481 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5482 if (SplatBitSize <= 64) {
5483 EVT VbicVT;
5484 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5485 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005486 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005487 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005488 if (Val.getNode()) {
5489 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005490 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005491 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005492 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005493 }
5494 }
5495 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005496
Owen Anderson080c0922010-11-05 19:27:46 +00005497 return SDValue();
5498}
5499
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005500/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5501static SDValue PerformORCombine(SDNode *N,
5502 TargetLowering::DAGCombinerInfo &DCI,
5503 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005504 // Attempt to use immediate-form VORR
5505 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5506 DebugLoc dl = N->getDebugLoc();
5507 EVT VT = N->getValueType(0);
5508 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005509
Tanya Lattner0433b212011-04-07 15:24:20 +00005510 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5511 return SDValue();
5512
Owen Anderson60f48702010-11-03 23:15:26 +00005513 APInt SplatBits, SplatUndef;
5514 unsigned SplatBitSize;
5515 bool HasAnyUndefs;
5516 if (BVN && Subtarget->hasNEON() &&
5517 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5518 if (SplatBitSize <= 64) {
5519 EVT VorrVT;
5520 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5521 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005522 DAG, VorrVT, VT.is128BitVector(),
5523 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005524 if (Val.getNode()) {
5525 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005526 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005527 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005528 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005529 }
5530 }
5531 }
5532
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005533 SDValue N0 = N->getOperand(0);
5534 if (N0.getOpcode() != ISD::AND)
5535 return SDValue();
5536 SDValue N1 = N->getOperand(1);
5537
5538 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5539 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5540 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5541 APInt SplatUndef;
5542 unsigned SplatBitSize;
5543 bool HasAnyUndefs;
5544
5545 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5546 APInt SplatBits0;
5547 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5548 HasAnyUndefs) && !HasAnyUndefs) {
5549 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5550 APInt SplatBits1;
5551 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5552 HasAnyUndefs) && !HasAnyUndefs &&
5553 SplatBits0 == ~SplatBits1) {
5554 // Canonicalize the vector type to make instruction selection simpler.
5555 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5556 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5557 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005558 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005559 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5560 }
5561 }
5562 }
5563
Jim Grosbach54238562010-07-17 03:30:54 +00005564 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5565 // reasonable.
5566
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005567 // BFI is only available on V6T2+
5568 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5569 return SDValue();
5570
Jim Grosbach54238562010-07-17 03:30:54 +00005571 DebugLoc DL = N->getDebugLoc();
5572 // 1) or (and A, mask), val => ARMbfi A, val, mask
5573 // iff (val & mask) == val
5574 //
5575 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5576 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005577 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005578 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005579 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005580 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005581
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005582 if (VT != MVT::i32)
5583 return SDValue();
5584
Evan Cheng30fb13f2010-12-13 20:32:54 +00005585 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005586
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005587 // The value and the mask need to be constants so we can verify this is
5588 // actually a bitfield set. If the mask is 0xffff, we can do better
5589 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005590 SDValue MaskOp = N0.getOperand(1);
5591 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5592 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005593 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005594 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005595 if (Mask == 0xffff)
5596 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005597 SDValue Res;
5598 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005599 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5600 if (N1C) {
5601 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005602 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005603 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005604
Evan Chenga9688c42010-12-11 04:11:38 +00005605 if (ARM::isBitFieldInvertedMask(Mask)) {
5606 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005607
Evan Cheng30fb13f2010-12-13 20:32:54 +00005608 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005609 DAG.getConstant(Val, MVT::i32),
5610 DAG.getConstant(Mask, MVT::i32));
5611
5612 // Do not add new nodes to DAG combiner worklist.
5613 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005614 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005615 }
Jim Grosbach54238562010-07-17 03:30:54 +00005616 } else if (N1.getOpcode() == ISD::AND) {
5617 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005618 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5619 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005620 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005621 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005622
Eric Christopher29aeed12011-03-26 01:21:03 +00005623 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5624 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005625 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005626 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005627 // The pack halfword instruction works better for masks that fit it,
5628 // so use that when it's available.
5629 if (Subtarget->hasT2ExtractPack() &&
5630 (Mask == 0xffff || Mask == 0xffff0000))
5631 return SDValue();
5632 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005633 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005634 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005635 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005636 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005637 DAG.getConstant(Mask, MVT::i32));
5638 // Do not add new nodes to DAG combiner worklist.
5639 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005640 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005641 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005642 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005643 // The pack halfword instruction works better for masks that fit it,
5644 // so use that when it's available.
5645 if (Subtarget->hasT2ExtractPack() &&
5646 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5647 return SDValue();
5648 // 2b
5649 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005650 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005651 DAG.getConstant(lsb, MVT::i32));
5652 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005653 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005654 // Do not add new nodes to DAG combiner worklist.
5655 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005656 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005657 }
5658 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005659
Evan Cheng30fb13f2010-12-13 20:32:54 +00005660 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5661 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5662 ARM::isBitFieldInvertedMask(~Mask)) {
5663 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5664 // where lsb(mask) == #shamt and masked bits of B are known zero.
5665 SDValue ShAmt = N00.getOperand(1);
5666 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5667 unsigned LSB = CountTrailingZeros_32(Mask);
5668 if (ShAmtC != LSB)
5669 return SDValue();
5670
5671 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5672 DAG.getConstant(~Mask, MVT::i32));
5673
5674 // Do not add new nodes to DAG combiner worklist.
5675 DCI.CombineTo(N, Res, false);
5676 }
5677
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005678 return SDValue();
5679}
5680
Evan Cheng0c1aec12010-12-14 03:22:07 +00005681/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5682/// C1 & C2 == C1.
5683static SDValue PerformBFICombine(SDNode *N,
5684 TargetLowering::DAGCombinerInfo &DCI) {
5685 SDValue N1 = N->getOperand(1);
5686 if (N1.getOpcode() == ISD::AND) {
5687 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5688 if (!N11C)
5689 return SDValue();
5690 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5691 unsigned Mask2 = N11C->getZExtValue();
5692 if ((Mask & Mask2) == Mask2)
5693 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5694 N->getOperand(0), N1.getOperand(0),
5695 N->getOperand(2));
5696 }
5697 return SDValue();
5698}
5699
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005700/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5701/// ARMISD::VMOVRRD.
5702static SDValue PerformVMOVRRDCombine(SDNode *N,
5703 TargetLowering::DAGCombinerInfo &DCI) {
5704 // vmovrrd(vmovdrr x, y) -> x,y
5705 SDValue InDouble = N->getOperand(0);
5706 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5707 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00005708
5709 // vmovrrd(load f64) -> (load i32), (load i32)
5710 SDNode *InNode = InDouble.getNode();
5711 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
5712 InNode->getValueType(0) == MVT::f64 &&
5713 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
5714 !cast<LoadSDNode>(InNode)->isVolatile()) {
5715 // TODO: Should this be done for non-FrameIndex operands?
5716 LoadSDNode *LD = cast<LoadSDNode>(InNode);
5717
5718 SelectionDAG &DAG = DCI.DAG;
5719 DebugLoc DL = LD->getDebugLoc();
5720 SDValue BasePtr = LD->getBasePtr();
5721 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
5722 LD->getPointerInfo(), LD->isVolatile(),
5723 LD->isNonTemporal(), LD->getAlignment());
5724
5725 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5726 DAG.getConstant(4, MVT::i32));
5727 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
5728 LD->getPointerInfo(), LD->isVolatile(),
5729 LD->isNonTemporal(),
5730 std::min(4U, LD->getAlignment() / 2));
5731
5732 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
5733 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
5734 DCI.RemoveFromWorklist(LD);
5735 DAG.DeleteNode(LD);
5736 return Result;
5737 }
5738
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005739 return SDValue();
5740}
5741
5742/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5743/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5744static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5745 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5746 SDValue Op0 = N->getOperand(0);
5747 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005748 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005749 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005750 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005751 Op1 = Op1.getOperand(0);
5752 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5753 Op0.getNode() == Op1.getNode() &&
5754 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005755 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005756 N->getValueType(0), Op0.getOperand(0));
5757 return SDValue();
5758}
5759
Bob Wilson31600902010-12-21 06:43:19 +00005760/// PerformSTORECombine - Target-specific dag combine xforms for
5761/// ISD::STORE.
5762static SDValue PerformSTORECombine(SDNode *N,
5763 TargetLowering::DAGCombinerInfo &DCI) {
5764 // Bitcast an i64 store extracted from a vector to f64.
5765 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5766 StoreSDNode *St = cast<StoreSDNode>(N);
5767 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00005768 if (!ISD::isNormalStore(St) || St->isVolatile())
5769 return SDValue();
5770
5771 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
5772 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
5773 SelectionDAG &DAG = DCI.DAG;
5774 DebugLoc DL = St->getDebugLoc();
5775 SDValue BasePtr = St->getBasePtr();
5776 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
5777 StVal.getNode()->getOperand(0), BasePtr,
5778 St->getPointerInfo(), St->isVolatile(),
5779 St->isNonTemporal(), St->getAlignment());
5780
5781 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5782 DAG.getConstant(4, MVT::i32));
5783 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
5784 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
5785 St->isNonTemporal(),
5786 std::min(4U, St->getAlignment() / 2));
5787 }
5788
5789 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00005790 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5791 return SDValue();
5792
5793 SelectionDAG &DAG = DCI.DAG;
5794 DebugLoc dl = StVal.getDebugLoc();
5795 SDValue IntVec = StVal.getOperand(0);
5796 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5797 IntVec.getValueType().getVectorNumElements());
5798 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5799 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5800 Vec, StVal.getOperand(1));
5801 dl = N->getDebugLoc();
5802 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5803 // Make the DAGCombiner fold the bitcasts.
5804 DCI.AddToWorklist(Vec.getNode());
5805 DCI.AddToWorklist(ExtElt.getNode());
5806 DCI.AddToWorklist(V.getNode());
5807 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5808 St->getPointerInfo(), St->isVolatile(),
5809 St->isNonTemporal(), St->getAlignment(),
5810 St->getTBAAInfo());
5811}
5812
5813/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5814/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5815/// i64 vector to have f64 elements, since the value can then be loaded
5816/// directly into a VFP register.
5817static bool hasNormalLoadOperand(SDNode *N) {
5818 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5819 for (unsigned i = 0; i < NumElts; ++i) {
5820 SDNode *Elt = N->getOperand(i).getNode();
5821 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5822 return true;
5823 }
5824 return false;
5825}
5826
Bob Wilson75f02882010-09-17 22:59:05 +00005827/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5828/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005829static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5830 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005831 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5832 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5833 // into a pair of GPRs, which is fine when the value is used as a scalar,
5834 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005835 SelectionDAG &DAG = DCI.DAG;
5836 if (N->getNumOperands() == 2) {
5837 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5838 if (RV.getNode())
5839 return RV;
5840 }
Bob Wilson75f02882010-09-17 22:59:05 +00005841
Bob Wilson31600902010-12-21 06:43:19 +00005842 // Load i64 elements as f64 values so that type legalization does not split
5843 // them up into i32 values.
5844 EVT VT = N->getValueType(0);
5845 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5846 return SDValue();
5847 DebugLoc dl = N->getDebugLoc();
5848 SmallVector<SDValue, 8> Ops;
5849 unsigned NumElts = VT.getVectorNumElements();
5850 for (unsigned i = 0; i < NumElts; ++i) {
5851 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5852 Ops.push_back(V);
5853 // Make the DAGCombiner fold the bitcast.
5854 DCI.AddToWorklist(V.getNode());
5855 }
5856 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5857 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5858 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5859}
5860
5861/// PerformInsertEltCombine - Target-specific dag combine xforms for
5862/// ISD::INSERT_VECTOR_ELT.
5863static SDValue PerformInsertEltCombine(SDNode *N,
5864 TargetLowering::DAGCombinerInfo &DCI) {
5865 // Bitcast an i64 load inserted into a vector to f64.
5866 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5867 EVT VT = N->getValueType(0);
5868 SDNode *Elt = N->getOperand(1).getNode();
5869 if (VT.getVectorElementType() != MVT::i64 ||
5870 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5871 return SDValue();
5872
5873 SelectionDAG &DAG = DCI.DAG;
5874 DebugLoc dl = N->getDebugLoc();
5875 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5876 VT.getVectorNumElements());
5877 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5878 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5879 // Make the DAGCombiner fold the bitcasts.
5880 DCI.AddToWorklist(Vec.getNode());
5881 DCI.AddToWorklist(V.getNode());
5882 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5883 Vec, V, N->getOperand(2));
5884 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005885}
5886
Bob Wilsonf20700c2010-10-27 20:38:28 +00005887/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5888/// ISD::VECTOR_SHUFFLE.
5889static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5890 // The LLVM shufflevector instruction does not require the shuffle mask
5891 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5892 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5893 // operands do not match the mask length, they are extended by concatenating
5894 // them with undef vectors. That is probably the right thing for other
5895 // targets, but for NEON it is better to concatenate two double-register
5896 // size vector operands into a single quad-register size vector. Do that
5897 // transformation here:
5898 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5899 // shuffle(concat(v1, v2), undef)
5900 SDValue Op0 = N->getOperand(0);
5901 SDValue Op1 = N->getOperand(1);
5902 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5903 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5904 Op0.getNumOperands() != 2 ||
5905 Op1.getNumOperands() != 2)
5906 return SDValue();
5907 SDValue Concat0Op1 = Op0.getOperand(1);
5908 SDValue Concat1Op1 = Op1.getOperand(1);
5909 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5910 Concat1Op1.getOpcode() != ISD::UNDEF)
5911 return SDValue();
5912 // Skip the transformation if any of the types are illegal.
5913 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5914 EVT VT = N->getValueType(0);
5915 if (!TLI.isTypeLegal(VT) ||
5916 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5917 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5918 return SDValue();
5919
5920 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5921 Op0.getOperand(0), Op1.getOperand(0));
5922 // Translate the shuffle mask.
5923 SmallVector<int, 16> NewMask;
5924 unsigned NumElts = VT.getVectorNumElements();
5925 unsigned HalfElts = NumElts/2;
5926 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5927 for (unsigned n = 0; n < NumElts; ++n) {
5928 int MaskElt = SVN->getMaskElt(n);
5929 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005930 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005931 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005932 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005933 NewElt = HalfElts + MaskElt - NumElts;
5934 NewMask.push_back(NewElt);
5935 }
5936 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5937 DAG.getUNDEF(VT), NewMask.data());
5938}
5939
Bob Wilson1c3ef902011-02-07 17:43:21 +00005940/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5941/// NEON load/store intrinsics to merge base address updates.
5942static SDValue CombineBaseUpdate(SDNode *N,
5943 TargetLowering::DAGCombinerInfo &DCI) {
5944 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5945 return SDValue();
5946
5947 SelectionDAG &DAG = DCI.DAG;
5948 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5949 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5950 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5951 SDValue Addr = N->getOperand(AddrOpIdx);
5952
5953 // Search for a use of the address operand that is an increment.
5954 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5955 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5956 SDNode *User = *UI;
5957 if (User->getOpcode() != ISD::ADD ||
5958 UI.getUse().getResNo() != Addr.getResNo())
5959 continue;
5960
5961 // Check that the add is independent of the load/store. Otherwise, folding
5962 // it would create a cycle.
5963 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5964 continue;
5965
5966 // Find the new opcode for the updating load/store.
5967 bool isLoad = true;
5968 bool isLaneOp = false;
5969 unsigned NewOpc = 0;
5970 unsigned NumVecs = 0;
5971 if (isIntrinsic) {
5972 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5973 switch (IntNo) {
5974 default: assert(0 && "unexpected intrinsic for Neon base update");
5975 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5976 NumVecs = 1; break;
5977 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5978 NumVecs = 2; break;
5979 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5980 NumVecs = 3; break;
5981 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5982 NumVecs = 4; break;
5983 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5984 NumVecs = 2; isLaneOp = true; break;
5985 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5986 NumVecs = 3; isLaneOp = true; break;
5987 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5988 NumVecs = 4; isLaneOp = true; break;
5989 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5990 NumVecs = 1; isLoad = false; break;
5991 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
5992 NumVecs = 2; isLoad = false; break;
5993 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
5994 NumVecs = 3; isLoad = false; break;
5995 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
5996 NumVecs = 4; isLoad = false; break;
5997 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
5998 NumVecs = 2; isLoad = false; isLaneOp = true; break;
5999 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6000 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6001 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6002 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6003 }
6004 } else {
6005 isLaneOp = true;
6006 switch (N->getOpcode()) {
6007 default: assert(0 && "unexpected opcode for Neon base update");
6008 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6009 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6010 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6011 }
6012 }
6013
6014 // Find the size of memory referenced by the load/store.
6015 EVT VecTy;
6016 if (isLoad)
6017 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006018 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006019 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6020 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6021 if (isLaneOp)
6022 NumBytes /= VecTy.getVectorNumElements();
6023
6024 // If the increment is a constant, it must match the memory ref size.
6025 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6026 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6027 uint64_t IncVal = CInc->getZExtValue();
6028 if (IncVal != NumBytes)
6029 continue;
6030 } else if (NumBytes >= 3 * 16) {
6031 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6032 // separate instructions that make it harder to use a non-constant update.
6033 continue;
6034 }
6035
6036 // Create the new updating load/store node.
6037 EVT Tys[6];
6038 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6039 unsigned n;
6040 for (n = 0; n < NumResultVecs; ++n)
6041 Tys[n] = VecTy;
6042 Tys[n++] = MVT::i32;
6043 Tys[n] = MVT::Other;
6044 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6045 SmallVector<SDValue, 8> Ops;
6046 Ops.push_back(N->getOperand(0)); // incoming chain
6047 Ops.push_back(N->getOperand(AddrOpIdx));
6048 Ops.push_back(Inc);
6049 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6050 Ops.push_back(N->getOperand(i));
6051 }
6052 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6053 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6054 Ops.data(), Ops.size(),
6055 MemInt->getMemoryVT(),
6056 MemInt->getMemOperand());
6057
6058 // Update the uses.
6059 std::vector<SDValue> NewResults;
6060 for (unsigned i = 0; i < NumResultVecs; ++i) {
6061 NewResults.push_back(SDValue(UpdN.getNode(), i));
6062 }
6063 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6064 DCI.CombineTo(N, NewResults);
6065 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6066
6067 break;
Owen Anderson76706012011-04-05 21:48:57 +00006068 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006069 return SDValue();
6070}
6071
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006072/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6073/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6074/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6075/// return true.
6076static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6077 SelectionDAG &DAG = DCI.DAG;
6078 EVT VT = N->getValueType(0);
6079 // vldN-dup instructions only support 64-bit vectors for N > 1.
6080 if (!VT.is64BitVector())
6081 return false;
6082
6083 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6084 SDNode *VLD = N->getOperand(0).getNode();
6085 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6086 return false;
6087 unsigned NumVecs = 0;
6088 unsigned NewOpc = 0;
6089 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6090 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6091 NumVecs = 2;
6092 NewOpc = ARMISD::VLD2DUP;
6093 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6094 NumVecs = 3;
6095 NewOpc = ARMISD::VLD3DUP;
6096 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6097 NumVecs = 4;
6098 NewOpc = ARMISD::VLD4DUP;
6099 } else {
6100 return false;
6101 }
6102
6103 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6104 // numbers match the load.
6105 unsigned VLDLaneNo =
6106 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6107 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6108 UI != UE; ++UI) {
6109 // Ignore uses of the chain result.
6110 if (UI.getUse().getResNo() == NumVecs)
6111 continue;
6112 SDNode *User = *UI;
6113 if (User->getOpcode() != ARMISD::VDUPLANE ||
6114 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6115 return false;
6116 }
6117
6118 // Create the vldN-dup node.
6119 EVT Tys[5];
6120 unsigned n;
6121 for (n = 0; n < NumVecs; ++n)
6122 Tys[n] = VT;
6123 Tys[n] = MVT::Other;
6124 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6125 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6126 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6127 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6128 Ops, 2, VLDMemInt->getMemoryVT(),
6129 VLDMemInt->getMemOperand());
6130
6131 // Update the uses.
6132 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6133 UI != UE; ++UI) {
6134 unsigned ResNo = UI.getUse().getResNo();
6135 // Ignore uses of the chain result.
6136 if (ResNo == NumVecs)
6137 continue;
6138 SDNode *User = *UI;
6139 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6140 }
6141
6142 // Now the vldN-lane intrinsic is dead except for its chain result.
6143 // Update uses of the chain.
6144 std::vector<SDValue> VLDDupResults;
6145 for (unsigned n = 0; n < NumVecs; ++n)
6146 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6147 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6148 DCI.CombineTo(VLD, VLDDupResults);
6149
6150 return true;
6151}
6152
Bob Wilson9e82bf12010-07-14 01:22:12 +00006153/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6154/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006155static SDValue PerformVDUPLANECombine(SDNode *N,
6156 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006157 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006158
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006159 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6160 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6161 if (CombineVLDDUP(N, DCI))
6162 return SDValue(N, 0);
6163
6164 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6165 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006166 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006167 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006168 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006169 return SDValue();
6170
6171 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6172 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6173 // The canonical VMOV for a zero vector uses a 32-bit element size.
6174 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6175 unsigned EltBits;
6176 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6177 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006178 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006179 if (EltSize > VT.getVectorElementType().getSizeInBits())
6180 return SDValue();
6181
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006182 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006183}
6184
Bob Wilson5bafff32009-06-22 23:27:02 +00006185/// getVShiftImm - Check if this is a valid build_vector for the immediate
6186/// operand of a vector shift operation, where all the elements of the
6187/// build_vector must have the same constant integer value.
6188static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6189 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006190 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006191 Op = Op.getOperand(0);
6192 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6193 APInt SplatBits, SplatUndef;
6194 unsigned SplatBitSize;
6195 bool HasAnyUndefs;
6196 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6197 HasAnyUndefs, ElementBits) ||
6198 SplatBitSize > ElementBits)
6199 return false;
6200 Cnt = SplatBits.getSExtValue();
6201 return true;
6202}
6203
6204/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6205/// operand of a vector shift left operation. That value must be in the range:
6206/// 0 <= Value < ElementBits for a left shift; or
6207/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006208static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006209 assert(VT.isVector() && "vector shift count is not a vector type");
6210 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6211 if (! getVShiftImm(Op, ElementBits, Cnt))
6212 return false;
6213 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6214}
6215
6216/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6217/// operand of a vector shift right operation. For a shift opcode, the value
6218/// is positive, but for an intrinsic the value count must be negative. The
6219/// absolute value must be in the range:
6220/// 1 <= |Value| <= ElementBits for a right shift; or
6221/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006222static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006223 int64_t &Cnt) {
6224 assert(VT.isVector() && "vector shift count is not a vector type");
6225 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6226 if (! getVShiftImm(Op, ElementBits, Cnt))
6227 return false;
6228 if (isIntrinsic)
6229 Cnt = -Cnt;
6230 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6231}
6232
6233/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6234static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6235 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6236 switch (IntNo) {
6237 default:
6238 // Don't do anything for most intrinsics.
6239 break;
6240
6241 // Vector shifts: check for immediate versions and lower them.
6242 // Note: This is done during DAG combining instead of DAG legalizing because
6243 // the build_vectors for 64-bit vector element shift counts are generally
6244 // not legal, and it is hard to see their values after they get legalized to
6245 // loads from a constant pool.
6246 case Intrinsic::arm_neon_vshifts:
6247 case Intrinsic::arm_neon_vshiftu:
6248 case Intrinsic::arm_neon_vshiftls:
6249 case Intrinsic::arm_neon_vshiftlu:
6250 case Intrinsic::arm_neon_vshiftn:
6251 case Intrinsic::arm_neon_vrshifts:
6252 case Intrinsic::arm_neon_vrshiftu:
6253 case Intrinsic::arm_neon_vrshiftn:
6254 case Intrinsic::arm_neon_vqshifts:
6255 case Intrinsic::arm_neon_vqshiftu:
6256 case Intrinsic::arm_neon_vqshiftsu:
6257 case Intrinsic::arm_neon_vqshiftns:
6258 case Intrinsic::arm_neon_vqshiftnu:
6259 case Intrinsic::arm_neon_vqshiftnsu:
6260 case Intrinsic::arm_neon_vqrshiftns:
6261 case Intrinsic::arm_neon_vqrshiftnu:
6262 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006263 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006264 int64_t Cnt;
6265 unsigned VShiftOpc = 0;
6266
6267 switch (IntNo) {
6268 case Intrinsic::arm_neon_vshifts:
6269 case Intrinsic::arm_neon_vshiftu:
6270 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6271 VShiftOpc = ARMISD::VSHL;
6272 break;
6273 }
6274 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6275 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6276 ARMISD::VSHRs : ARMISD::VSHRu);
6277 break;
6278 }
6279 return SDValue();
6280
6281 case Intrinsic::arm_neon_vshiftls:
6282 case Intrinsic::arm_neon_vshiftlu:
6283 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6284 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006285 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006286
6287 case Intrinsic::arm_neon_vrshifts:
6288 case Intrinsic::arm_neon_vrshiftu:
6289 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6290 break;
6291 return SDValue();
6292
6293 case Intrinsic::arm_neon_vqshifts:
6294 case Intrinsic::arm_neon_vqshiftu:
6295 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6296 break;
6297 return SDValue();
6298
6299 case Intrinsic::arm_neon_vqshiftsu:
6300 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6301 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006302 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006303
6304 case Intrinsic::arm_neon_vshiftn:
6305 case Intrinsic::arm_neon_vrshiftn:
6306 case Intrinsic::arm_neon_vqshiftns:
6307 case Intrinsic::arm_neon_vqshiftnu:
6308 case Intrinsic::arm_neon_vqshiftnsu:
6309 case Intrinsic::arm_neon_vqrshiftns:
6310 case Intrinsic::arm_neon_vqrshiftnu:
6311 case Intrinsic::arm_neon_vqrshiftnsu:
6312 // Narrowing shifts require an immediate right shift.
6313 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6314 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006315 llvm_unreachable("invalid shift count for narrowing vector shift "
6316 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006317
6318 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006319 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006320 }
6321
6322 switch (IntNo) {
6323 case Intrinsic::arm_neon_vshifts:
6324 case Intrinsic::arm_neon_vshiftu:
6325 // Opcode already set above.
6326 break;
6327 case Intrinsic::arm_neon_vshiftls:
6328 case Intrinsic::arm_neon_vshiftlu:
6329 if (Cnt == VT.getVectorElementType().getSizeInBits())
6330 VShiftOpc = ARMISD::VSHLLi;
6331 else
6332 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6333 ARMISD::VSHLLs : ARMISD::VSHLLu);
6334 break;
6335 case Intrinsic::arm_neon_vshiftn:
6336 VShiftOpc = ARMISD::VSHRN; break;
6337 case Intrinsic::arm_neon_vrshifts:
6338 VShiftOpc = ARMISD::VRSHRs; break;
6339 case Intrinsic::arm_neon_vrshiftu:
6340 VShiftOpc = ARMISD::VRSHRu; break;
6341 case Intrinsic::arm_neon_vrshiftn:
6342 VShiftOpc = ARMISD::VRSHRN; break;
6343 case Intrinsic::arm_neon_vqshifts:
6344 VShiftOpc = ARMISD::VQSHLs; break;
6345 case Intrinsic::arm_neon_vqshiftu:
6346 VShiftOpc = ARMISD::VQSHLu; break;
6347 case Intrinsic::arm_neon_vqshiftsu:
6348 VShiftOpc = ARMISD::VQSHLsu; break;
6349 case Intrinsic::arm_neon_vqshiftns:
6350 VShiftOpc = ARMISD::VQSHRNs; break;
6351 case Intrinsic::arm_neon_vqshiftnu:
6352 VShiftOpc = ARMISD::VQSHRNu; break;
6353 case Intrinsic::arm_neon_vqshiftnsu:
6354 VShiftOpc = ARMISD::VQSHRNsu; break;
6355 case Intrinsic::arm_neon_vqrshiftns:
6356 VShiftOpc = ARMISD::VQRSHRNs; break;
6357 case Intrinsic::arm_neon_vqrshiftnu:
6358 VShiftOpc = ARMISD::VQRSHRNu; break;
6359 case Intrinsic::arm_neon_vqrshiftnsu:
6360 VShiftOpc = ARMISD::VQRSHRNsu; break;
6361 }
6362
6363 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006364 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006365 }
6366
6367 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006368 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006369 int64_t Cnt;
6370 unsigned VShiftOpc = 0;
6371
6372 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6373 VShiftOpc = ARMISD::VSLI;
6374 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6375 VShiftOpc = ARMISD::VSRI;
6376 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006377 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006378 }
6379
6380 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6381 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006382 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006383 }
6384
6385 case Intrinsic::arm_neon_vqrshifts:
6386 case Intrinsic::arm_neon_vqrshiftu:
6387 // No immediate versions of these to check for.
6388 break;
6389 }
6390
6391 return SDValue();
6392}
6393
6394/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6395/// lowers them. As with the vector shift intrinsics, this is done during DAG
6396/// combining instead of DAG legalizing because the build_vectors for 64-bit
6397/// vector element shift counts are generally not legal, and it is hard to see
6398/// their values after they get legalized to loads from a constant pool.
6399static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6400 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006401 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006402
6403 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006404 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6405 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006406 return SDValue();
6407
6408 assert(ST->hasNEON() && "unexpected vector shift");
6409 int64_t Cnt;
6410
6411 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006412 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006413
6414 case ISD::SHL:
6415 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6416 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006417 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006418 break;
6419
6420 case ISD::SRA:
6421 case ISD::SRL:
6422 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6423 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6424 ARMISD::VSHRs : ARMISD::VSHRu);
6425 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006426 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006427 }
6428 }
6429 return SDValue();
6430}
6431
6432/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6433/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6434static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6435 const ARMSubtarget *ST) {
6436 SDValue N0 = N->getOperand(0);
6437
6438 // Check for sign- and zero-extensions of vector extract operations of 8-
6439 // and 16-bit vector elements. NEON supports these directly. They are
6440 // handled during DAG combining because type legalization will promote them
6441 // to 32-bit types and it is messy to recognize the operations after that.
6442 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6443 SDValue Vec = N0.getOperand(0);
6444 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006445 EVT VT = N->getValueType(0);
6446 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006447 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6448
Owen Anderson825b72b2009-08-11 20:47:22 +00006449 if (VT == MVT::i32 &&
6450 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006451 TLI.isTypeLegal(Vec.getValueType()) &&
6452 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006453
6454 unsigned Opc = 0;
6455 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006456 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006457 case ISD::SIGN_EXTEND:
6458 Opc = ARMISD::VGETLANEs;
6459 break;
6460 case ISD::ZERO_EXTEND:
6461 case ISD::ANY_EXTEND:
6462 Opc = ARMISD::VGETLANEu;
6463 break;
6464 }
6465 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6466 }
6467 }
6468
6469 return SDValue();
6470}
6471
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006472/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6473/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6474static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6475 const ARMSubtarget *ST) {
6476 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006477 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006478 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6479 // a NaN; only do the transformation when it matches that behavior.
6480
6481 // For now only do this when using NEON for FP operations; if using VFP, it
6482 // is not obvious that the benefit outweighs the cost of switching to the
6483 // NEON pipeline.
6484 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6485 N->getValueType(0) != MVT::f32)
6486 return SDValue();
6487
6488 SDValue CondLHS = N->getOperand(0);
6489 SDValue CondRHS = N->getOperand(1);
6490 SDValue LHS = N->getOperand(2);
6491 SDValue RHS = N->getOperand(3);
6492 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6493
6494 unsigned Opcode = 0;
6495 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006496 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006497 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006498 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006499 IsReversed = true ; // x CC y ? y : x
6500 } else {
6501 return SDValue();
6502 }
6503
Bob Wilsone742bb52010-02-24 22:15:53 +00006504 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006505 switch (CC) {
6506 default: break;
6507 case ISD::SETOLT:
6508 case ISD::SETOLE:
6509 case ISD::SETLT:
6510 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006511 case ISD::SETULT:
6512 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006513 // If LHS is NaN, an ordered comparison will be false and the result will
6514 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6515 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6516 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6517 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6518 break;
6519 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6520 // will return -0, so vmin can only be used for unsafe math or if one of
6521 // the operands is known to be nonzero.
6522 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6523 !UnsafeFPMath &&
6524 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6525 break;
6526 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006527 break;
6528
6529 case ISD::SETOGT:
6530 case ISD::SETOGE:
6531 case ISD::SETGT:
6532 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006533 case ISD::SETUGT:
6534 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006535 // If LHS is NaN, an ordered comparison will be false and the result will
6536 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6537 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6538 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6539 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6540 break;
6541 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6542 // will return +0, so vmax can only be used for unsafe math or if one of
6543 // the operands is known to be nonzero.
6544 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6545 !UnsafeFPMath &&
6546 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6547 break;
6548 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006549 break;
6550 }
6551
6552 if (!Opcode)
6553 return SDValue();
6554 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6555}
6556
Dan Gohman475871a2008-07-27 21:46:04 +00006557SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006558 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006559 switch (N->getOpcode()) {
6560 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006561 case ISD::ADD: return PerformADDCombine(N, DCI);
6562 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006563 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006564 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006565 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006566 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006567 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006568 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006569 case ISD::STORE: return PerformSTORECombine(N, DCI);
6570 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6571 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006572 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006573 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006574 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006575 case ISD::SHL:
6576 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006577 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006578 case ISD::SIGN_EXTEND:
6579 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006580 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6581 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006582 case ARMISD::VLD2DUP:
6583 case ARMISD::VLD3DUP:
6584 case ARMISD::VLD4DUP:
6585 return CombineBaseUpdate(N, DCI);
6586 case ISD::INTRINSIC_VOID:
6587 case ISD::INTRINSIC_W_CHAIN:
6588 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6589 case Intrinsic::arm_neon_vld1:
6590 case Intrinsic::arm_neon_vld2:
6591 case Intrinsic::arm_neon_vld3:
6592 case Intrinsic::arm_neon_vld4:
6593 case Intrinsic::arm_neon_vld2lane:
6594 case Intrinsic::arm_neon_vld3lane:
6595 case Intrinsic::arm_neon_vld4lane:
6596 case Intrinsic::arm_neon_vst1:
6597 case Intrinsic::arm_neon_vst2:
6598 case Intrinsic::arm_neon_vst3:
6599 case Intrinsic::arm_neon_vst4:
6600 case Intrinsic::arm_neon_vst2lane:
6601 case Intrinsic::arm_neon_vst3lane:
6602 case Intrinsic::arm_neon_vst4lane:
6603 return CombineBaseUpdate(N, DCI);
6604 default: break;
6605 }
6606 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006607 }
Dan Gohman475871a2008-07-27 21:46:04 +00006608 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006609}
6610
Evan Cheng31959b12011-02-02 01:06:55 +00006611bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6612 EVT VT) const {
6613 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6614}
6615
Bill Wendlingaf566342009-08-15 21:21:19 +00006616bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006617 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006618 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006619
6620 switch (VT.getSimpleVT().SimpleTy) {
6621 default:
6622 return false;
6623 case MVT::i8:
6624 case MVT::i16:
6625 case MVT::i32:
6626 return true;
6627 // FIXME: VLD1 etc with standard alignment is legal.
6628 }
6629}
6630
Evan Chenge6c835f2009-08-14 20:09:37 +00006631static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6632 if (V < 0)
6633 return false;
6634
6635 unsigned Scale = 1;
6636 switch (VT.getSimpleVT().SimpleTy) {
6637 default: return false;
6638 case MVT::i1:
6639 case MVT::i8:
6640 // Scale == 1;
6641 break;
6642 case MVT::i16:
6643 // Scale == 2;
6644 Scale = 2;
6645 break;
6646 case MVT::i32:
6647 // Scale == 4;
6648 Scale = 4;
6649 break;
6650 }
6651
6652 if ((V & (Scale - 1)) != 0)
6653 return false;
6654 V /= Scale;
6655 return V == (V & ((1LL << 5) - 1));
6656}
6657
6658static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6659 const ARMSubtarget *Subtarget) {
6660 bool isNeg = false;
6661 if (V < 0) {
6662 isNeg = true;
6663 V = - V;
6664 }
6665
6666 switch (VT.getSimpleVT().SimpleTy) {
6667 default: return false;
6668 case MVT::i1:
6669 case MVT::i8:
6670 case MVT::i16:
6671 case MVT::i32:
6672 // + imm12 or - imm8
6673 if (isNeg)
6674 return V == (V & ((1LL << 8) - 1));
6675 return V == (V & ((1LL << 12) - 1));
6676 case MVT::f32:
6677 case MVT::f64:
6678 // Same as ARM mode. FIXME: NEON?
6679 if (!Subtarget->hasVFP2())
6680 return false;
6681 if ((V & 3) != 0)
6682 return false;
6683 V >>= 2;
6684 return V == (V & ((1LL << 8) - 1));
6685 }
6686}
6687
Evan Chengb01fad62007-03-12 23:30:29 +00006688/// isLegalAddressImmediate - Return true if the integer value can be used
6689/// as the offset of the target addressing mode for load / store of the
6690/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006691static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006692 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006693 if (V == 0)
6694 return true;
6695
Evan Cheng65011532009-03-09 19:15:00 +00006696 if (!VT.isSimple())
6697 return false;
6698
Evan Chenge6c835f2009-08-14 20:09:37 +00006699 if (Subtarget->isThumb1Only())
6700 return isLegalT1AddressImmediate(V, VT);
6701 else if (Subtarget->isThumb2())
6702 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006703
Evan Chenge6c835f2009-08-14 20:09:37 +00006704 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006705 if (V < 0)
6706 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006707 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006708 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006709 case MVT::i1:
6710 case MVT::i8:
6711 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006712 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006713 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006714 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006715 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006716 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006717 case MVT::f32:
6718 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006719 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006720 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006721 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006722 return false;
6723 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006724 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006725 }
Evan Chenga8e29892007-01-19 07:51:42 +00006726}
6727
Evan Chenge6c835f2009-08-14 20:09:37 +00006728bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6729 EVT VT) const {
6730 int Scale = AM.Scale;
6731 if (Scale < 0)
6732 return false;
6733
6734 switch (VT.getSimpleVT().SimpleTy) {
6735 default: return false;
6736 case MVT::i1:
6737 case MVT::i8:
6738 case MVT::i16:
6739 case MVT::i32:
6740 if (Scale == 1)
6741 return true;
6742 // r + r << imm
6743 Scale = Scale & ~1;
6744 return Scale == 2 || Scale == 4 || Scale == 8;
6745 case MVT::i64:
6746 // r + r
6747 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6748 return true;
6749 return false;
6750 case MVT::isVoid:
6751 // Note, we allow "void" uses (basically, uses that aren't loads or
6752 // stores), because arm allows folding a scale into many arithmetic
6753 // operations. This should be made more precise and revisited later.
6754
6755 // Allow r << imm, but the imm has to be a multiple of two.
6756 if (Scale & 1) return false;
6757 return isPowerOf2_32(Scale);
6758 }
6759}
6760
Chris Lattner37caf8c2007-04-09 23:33:39 +00006761/// isLegalAddressingMode - Return true if the addressing mode represented
6762/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006763bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006764 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006765 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006766 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006767 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006768
Chris Lattner37caf8c2007-04-09 23:33:39 +00006769 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006770 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006771 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006772
Chris Lattner37caf8c2007-04-09 23:33:39 +00006773 switch (AM.Scale) {
6774 case 0: // no scale reg, must be "r+i" or "r", or "i".
6775 break;
6776 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006777 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006778 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006779 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006780 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006781 // ARM doesn't support any R+R*scale+imm addr modes.
6782 if (AM.BaseOffs)
6783 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006784
Bob Wilson2c7dab12009-04-08 17:55:28 +00006785 if (!VT.isSimple())
6786 return false;
6787
Evan Chenge6c835f2009-08-14 20:09:37 +00006788 if (Subtarget->isThumb2())
6789 return isLegalT2ScaledAddressingMode(AM, VT);
6790
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006791 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006792 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006793 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006794 case MVT::i1:
6795 case MVT::i8:
6796 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006797 if (Scale < 0) Scale = -Scale;
6798 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006799 return true;
6800 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006801 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006802 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006803 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006804 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006805 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006806 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006807 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006808
Owen Anderson825b72b2009-08-11 20:47:22 +00006809 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006810 // Note, we allow "void" uses (basically, uses that aren't loads or
6811 // stores), because arm allows folding a scale into many arithmetic
6812 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006813
Chris Lattner37caf8c2007-04-09 23:33:39 +00006814 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006815 if (Scale & 1) return false;
6816 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006817 }
6818 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006819 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006820 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006821}
6822
Evan Cheng77e47512009-11-11 19:05:52 +00006823/// isLegalICmpImmediate - Return true if the specified immediate is legal
6824/// icmp immediate, that is the target has icmp instructions which can compare
6825/// a register against the immediate without having to materialize the
6826/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006827bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006828 if (!Subtarget->isThumb())
6829 return ARM_AM::getSOImmVal(Imm) != -1;
6830 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006831 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006832 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006833}
6834
Owen Andersone50ed302009-08-10 22:56:29 +00006835static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006836 bool isSEXTLoad, SDValue &Base,
6837 SDValue &Offset, bool &isInc,
6838 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006839 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6840 return false;
6841
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006843 // AddressingMode 3
6844 Base = Ptr->getOperand(0);
6845 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006846 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006847 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006848 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006849 isInc = false;
6850 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6851 return true;
6852 }
6853 }
6854 isInc = (Ptr->getOpcode() == ISD::ADD);
6855 Offset = Ptr->getOperand(1);
6856 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006858 // AddressingMode 2
6859 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006860 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006861 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006862 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006863 isInc = false;
6864 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6865 Base = Ptr->getOperand(0);
6866 return true;
6867 }
6868 }
6869
6870 if (Ptr->getOpcode() == ISD::ADD) {
6871 isInc = true;
6872 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6873 if (ShOpcVal != ARM_AM::no_shift) {
6874 Base = Ptr->getOperand(1);
6875 Offset = Ptr->getOperand(0);
6876 } else {
6877 Base = Ptr->getOperand(0);
6878 Offset = Ptr->getOperand(1);
6879 }
6880 return true;
6881 }
6882
6883 isInc = (Ptr->getOpcode() == ISD::ADD);
6884 Base = Ptr->getOperand(0);
6885 Offset = Ptr->getOperand(1);
6886 return true;
6887 }
6888
Jim Grosbache5165492009-11-09 00:11:35 +00006889 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006890 return false;
6891}
6892
Owen Andersone50ed302009-08-10 22:56:29 +00006893static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006894 bool isSEXTLoad, SDValue &Base,
6895 SDValue &Offset, bool &isInc,
6896 SelectionDAG &DAG) {
6897 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6898 return false;
6899
6900 Base = Ptr->getOperand(0);
6901 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6902 int RHSC = (int)RHS->getZExtValue();
6903 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6904 assert(Ptr->getOpcode() == ISD::ADD);
6905 isInc = false;
6906 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6907 return true;
6908 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6909 isInc = Ptr->getOpcode() == ISD::ADD;
6910 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6911 return true;
6912 }
6913 }
6914
6915 return false;
6916}
6917
Evan Chenga8e29892007-01-19 07:51:42 +00006918/// getPreIndexedAddressParts - returns true by value, base pointer and
6919/// offset pointer and addressing mode by reference if the node's address
6920/// can be legally represented as pre-indexed load / store address.
6921bool
Dan Gohman475871a2008-07-27 21:46:04 +00006922ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6923 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006924 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006925 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006926 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006927 return false;
6928
Owen Andersone50ed302009-08-10 22:56:29 +00006929 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006930 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006931 bool isSEXTLoad = false;
6932 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6933 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006934 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006935 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6936 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6937 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006938 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006939 } else
6940 return false;
6941
6942 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006943 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006944 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006945 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6946 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006947 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006948 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006949 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006950 if (!isLegal)
6951 return false;
6952
6953 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6954 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006955}
6956
6957/// getPostIndexedAddressParts - returns true by value, base pointer and
6958/// offset pointer and addressing mode by reference if this node can be
6959/// combined with a load / store to form a post-indexed load / store.
6960bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006961 SDValue &Base,
6962 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006963 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006964 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006965 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006966 return false;
6967
Owen Andersone50ed302009-08-10 22:56:29 +00006968 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006969 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006970 bool isSEXTLoad = false;
6971 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006972 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006973 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006974 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6975 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006976 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006977 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006978 } else
6979 return false;
6980
6981 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006982 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006983 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006984 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006985 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006986 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006987 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6988 isInc, DAG);
6989 if (!isLegal)
6990 return false;
6991
Evan Cheng28dad2a2010-05-18 21:31:17 +00006992 if (Ptr != Base) {
6993 // Swap base ptr and offset to catch more post-index load / store when
6994 // it's legal. In Thumb2 mode, offset must be an immediate.
6995 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6996 !Subtarget->isThumb2())
6997 std::swap(Base, Offset);
6998
6999 // Post-indexed load / store update the base pointer.
7000 if (Ptr != Base)
7001 return false;
7002 }
7003
Evan Chenge88d5ce2009-07-02 07:28:31 +00007004 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7005 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007006}
7007
Dan Gohman475871a2008-07-27 21:46:04 +00007008void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007009 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007010 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007011 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007012 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007013 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007014 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007015 switch (Op.getOpcode()) {
7016 default: break;
7017 case ARMISD::CMOV: {
7018 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007019 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007020 if (KnownZero == 0 && KnownOne == 0) return;
7021
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007022 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007023 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7024 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007025 KnownZero &= KnownZeroRHS;
7026 KnownOne &= KnownOneRHS;
7027 return;
7028 }
7029 }
7030}
7031
7032//===----------------------------------------------------------------------===//
7033// ARM Inline Assembly Support
7034//===----------------------------------------------------------------------===//
7035
Evan Cheng55d42002011-01-08 01:24:27 +00007036bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7037 // Looking for "rev" which is V6+.
7038 if (!Subtarget->hasV6Ops())
7039 return false;
7040
7041 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7042 std::string AsmStr = IA->getAsmString();
7043 SmallVector<StringRef, 4> AsmPieces;
7044 SplitString(AsmStr, AsmPieces, ";\n");
7045
7046 switch (AsmPieces.size()) {
7047 default: return false;
7048 case 1:
7049 AsmStr = AsmPieces[0];
7050 AsmPieces.clear();
7051 SplitString(AsmStr, AsmPieces, " \t,");
7052
7053 // rev $0, $1
7054 if (AsmPieces.size() == 3 &&
7055 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7056 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7057 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7058 if (Ty && Ty->getBitWidth() == 32)
7059 return IntrinsicLowering::LowerToByteSwap(CI);
7060 }
7061 break;
7062 }
7063
7064 return false;
7065}
7066
Evan Chenga8e29892007-01-19 07:51:42 +00007067/// getConstraintType - Given a constraint letter, return the type of
7068/// constraint it is for this target.
7069ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007070ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7071 if (Constraint.size() == 1) {
7072 switch (Constraint[0]) {
7073 default: break;
7074 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007075 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00007076 }
Evan Chenga8e29892007-01-19 07:51:42 +00007077 }
Chris Lattner4234f572007-03-25 02:14:49 +00007078 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007079}
7080
John Thompson44ab89e2010-10-29 17:29:13 +00007081/// Examine constraint type and operand type and determine a weight value.
7082/// This object must already have been set up with the operand type
7083/// and the current alternative constraint selected.
7084TargetLowering::ConstraintWeight
7085ARMTargetLowering::getSingleConstraintMatchWeight(
7086 AsmOperandInfo &info, const char *constraint) const {
7087 ConstraintWeight weight = CW_Invalid;
7088 Value *CallOperandVal = info.CallOperandVal;
7089 // If we don't have a value, we can't do a match,
7090 // but allow it at the lowest weight.
7091 if (CallOperandVal == NULL)
7092 return CW_Default;
7093 const Type *type = CallOperandVal->getType();
7094 // Look at the constraint type.
7095 switch (*constraint) {
7096 default:
7097 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7098 break;
7099 case 'l':
7100 if (type->isIntegerTy()) {
7101 if (Subtarget->isThumb())
7102 weight = CW_SpecificReg;
7103 else
7104 weight = CW_Register;
7105 }
7106 break;
7107 case 'w':
7108 if (type->isFloatingPointTy())
7109 weight = CW_Register;
7110 break;
7111 }
7112 return weight;
7113}
7114
Bob Wilson2dc4f542009-03-20 22:42:55 +00007115std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007116ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007117 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007118 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007119 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007120 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007121 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007122 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007123 return std::make_pair(0U, ARM::tGPRRegisterClass);
7124 else
7125 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007126 case 'r':
7127 return std::make_pair(0U, ARM::GPRRegisterClass);
7128 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007129 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007130 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007131 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007132 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007133 if (VT.getSizeInBits() == 128)
7134 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007135 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007136 }
7137 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007138 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007139 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007140
Evan Chenga8e29892007-01-19 07:51:42 +00007141 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7142}
7143
7144std::vector<unsigned> ARMTargetLowering::
7145getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007146 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007147 if (Constraint.size() != 1)
7148 return std::vector<unsigned>();
7149
7150 switch (Constraint[0]) { // GCC ARM Constraint Letters
7151 default: break;
7152 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007153 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7154 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7155 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007156 case 'r':
7157 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7158 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7159 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7160 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007161 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007162 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007163 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7164 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7165 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7166 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7167 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7168 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7169 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7170 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007171 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007172 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7173 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7174 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7175 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007176 if (VT.getSizeInBits() == 128)
7177 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7178 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007179 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007180 }
7181
7182 return std::vector<unsigned>();
7183}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007184
7185/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7186/// vector. If it is invalid, don't add anything to Ops.
7187void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
7188 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007189 std::vector<SDValue>&Ops,
7190 SelectionDAG &DAG) const {
7191 SDValue Result(0, 0);
7192
7193 switch (Constraint) {
7194 default: break;
7195 case 'I': case 'J': case 'K': case 'L':
7196 case 'M': case 'N': case 'O':
7197 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7198 if (!C)
7199 return;
7200
7201 int64_t CVal64 = C->getSExtValue();
7202 int CVal = (int) CVal64;
7203 // None of these constraints allow values larger than 32 bits. Check
7204 // that the value fits in an int.
7205 if (CVal != CVal64)
7206 return;
7207
7208 switch (Constraint) {
7209 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007210 if (Subtarget->isThumb1Only()) {
7211 // This must be a constant between 0 and 255, for ADD
7212 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007213 if (CVal >= 0 && CVal <= 255)
7214 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007215 } else if (Subtarget->isThumb2()) {
7216 // A constant that can be used as an immediate value in a
7217 // data-processing instruction.
7218 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7219 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007220 } else {
7221 // A constant that can be used as an immediate value in a
7222 // data-processing instruction.
7223 if (ARM_AM::getSOImmVal(CVal) != -1)
7224 break;
7225 }
7226 return;
7227
7228 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007229 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007230 // This must be a constant between -255 and -1, for negated ADD
7231 // immediates. This can be used in GCC with an "n" modifier that
7232 // prints the negated value, for use with SUB instructions. It is
7233 // not useful otherwise but is implemented for compatibility.
7234 if (CVal >= -255 && CVal <= -1)
7235 break;
7236 } else {
7237 // This must be a constant between -4095 and 4095. It is not clear
7238 // what this constraint is intended for. Implemented for
7239 // compatibility with GCC.
7240 if (CVal >= -4095 && CVal <= 4095)
7241 break;
7242 }
7243 return;
7244
7245 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007246 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007247 // A 32-bit value where only one byte has a nonzero value. Exclude
7248 // zero to match GCC. This constraint is used by GCC internally for
7249 // constants that can be loaded with a move/shift combination.
7250 // It is not useful otherwise but is implemented for compatibility.
7251 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7252 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007253 } else if (Subtarget->isThumb2()) {
7254 // A constant whose bitwise inverse can be used as an immediate
7255 // value in a data-processing instruction. This can be used in GCC
7256 // with a "B" modifier that prints the inverted value, for use with
7257 // BIC and MVN instructions. It is not useful otherwise but is
7258 // implemented for compatibility.
7259 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7260 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007261 } else {
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::getSOImmVal(~CVal) != -1)
7268 break;
7269 }
7270 return;
7271
7272 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007273 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007274 // This must be a constant between -7 and 7,
7275 // for 3-operand ADD/SUB immediate instructions.
7276 if (CVal >= -7 && CVal < 7)
7277 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007278 } else if (Subtarget->isThumb2()) {
7279 // A constant whose negation can be used as an immediate value in a
7280 // data-processing instruction. This can be used in GCC with an "n"
7281 // modifier that prints the negated value, for use with SUB
7282 // instructions. It is not useful otherwise but is implemented for
7283 // compatibility.
7284 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7285 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007286 } else {
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::getSOImmVal(-CVal) != -1)
7293 break;
7294 }
7295 return;
7296
7297 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007298 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007299 // This must be a multiple of 4 between 0 and 1020, for
7300 // ADD sp + immediate.
7301 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7302 break;
7303 } else {
7304 // A power of two or a constant between 0 and 32. This is used in
7305 // GCC for the shift amount on shifted register operands, but it is
7306 // useful in general for any shift amounts.
7307 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7308 break;
7309 }
7310 return;
7311
7312 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007313 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007314 // This must be a constant between 0 and 31, for shift amounts.
7315 if (CVal >= 0 && CVal <= 31)
7316 break;
7317 }
7318 return;
7319
7320 case 'O':
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 multiple of 4 between -508 and 508, for
7323 // ADD/SUB sp = sp + immediate.
7324 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7325 break;
7326 }
7327 return;
7328 }
7329 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7330 break;
7331 }
7332
7333 if (Result.getNode()) {
7334 Ops.push_back(Result);
7335 return;
7336 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007337 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007338}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007339
7340bool
7341ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7342 // The ARM target isn't yet aware of offsets.
7343 return false;
7344}
Evan Cheng39382422009-10-28 01:44:26 +00007345
7346int ARM::getVFPf32Imm(const APFloat &FPImm) {
7347 APInt Imm = FPImm.bitcastToAPInt();
7348 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7349 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7350 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7351
7352 // We can handle 4 bits of mantissa.
7353 // mantissa = (16+UInt(e:f:g:h))/16.
7354 if (Mantissa & 0x7ffff)
7355 return -1;
7356 Mantissa >>= 19;
7357 if ((Mantissa & 0xf) != Mantissa)
7358 return -1;
7359
7360 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7361 if (Exp < -3 || Exp > 4)
7362 return -1;
7363 Exp = ((Exp+3) & 0x7) ^ 4;
7364
7365 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7366}
7367
7368int ARM::getVFPf64Imm(const APFloat &FPImm) {
7369 APInt Imm = FPImm.bitcastToAPInt();
7370 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7371 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7372 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7373
7374 // We can handle 4 bits of mantissa.
7375 // mantissa = (16+UInt(e:f:g:h))/16.
7376 if (Mantissa & 0xffffffffffffLL)
7377 return -1;
7378 Mantissa >>= 48;
7379 if ((Mantissa & 0xf) != Mantissa)
7380 return -1;
7381
7382 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7383 if (Exp < -3 || Exp > 4)
7384 return -1;
7385 Exp = ((Exp+3) & 0x7) ^ 4;
7386
7387 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7388}
7389
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007390bool ARM::isBitFieldInvertedMask(unsigned v) {
7391 if (v == 0xffffffff)
7392 return 0;
7393 // there can be 1's on either or both "outsides", all the "inside"
7394 // bits must be 0's
7395 unsigned int lsb = 0, msb = 31;
7396 while (v & (1 << msb)) --msb;
7397 while (v & (1 << lsb)) ++lsb;
7398 for (unsigned int i = lsb; i <= msb; ++i) {
7399 if (v & (1 << i))
7400 return 0;
7401 }
7402 return 1;
7403}
7404
Evan Cheng39382422009-10-28 01:44:26 +00007405/// isFPImmLegal - Returns true if the target can instruction select the
7406/// specified FP immediate natively. If false, the legalizer will
7407/// materialize the FP immediate as a load from a constant pool.
7408bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7409 if (!Subtarget->hasVFP3())
7410 return false;
7411 if (VT == MVT::f32)
7412 return ARM::getVFPf32Imm(Imm) != -1;
7413 if (VT == MVT::f64)
7414 return ARM::getVFPf64Imm(Imm) != -1;
7415 return false;
7416}
Bob Wilson65ffec42010-09-21 17:56:22 +00007417
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007418/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007419/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7420/// specified in the intrinsic calls.
7421bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7422 const CallInst &I,
7423 unsigned Intrinsic) const {
7424 switch (Intrinsic) {
7425 case Intrinsic::arm_neon_vld1:
7426 case Intrinsic::arm_neon_vld2:
7427 case Intrinsic::arm_neon_vld3:
7428 case Intrinsic::arm_neon_vld4:
7429 case Intrinsic::arm_neon_vld2lane:
7430 case Intrinsic::arm_neon_vld3lane:
7431 case Intrinsic::arm_neon_vld4lane: {
7432 Info.opc = ISD::INTRINSIC_W_CHAIN;
7433 // Conservatively set memVT to the entire set of vectors loaded.
7434 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7435 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7436 Info.ptrVal = I.getArgOperand(0);
7437 Info.offset = 0;
7438 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7439 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7440 Info.vol = false; // volatile loads with NEON intrinsics not supported
7441 Info.readMem = true;
7442 Info.writeMem = false;
7443 return true;
7444 }
7445 case Intrinsic::arm_neon_vst1:
7446 case Intrinsic::arm_neon_vst2:
7447 case Intrinsic::arm_neon_vst3:
7448 case Intrinsic::arm_neon_vst4:
7449 case Intrinsic::arm_neon_vst2lane:
7450 case Intrinsic::arm_neon_vst3lane:
7451 case Intrinsic::arm_neon_vst4lane: {
7452 Info.opc = ISD::INTRINSIC_VOID;
7453 // Conservatively set memVT to the entire set of vectors stored.
7454 unsigned NumElts = 0;
7455 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7456 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7457 if (!ArgTy->isVectorTy())
7458 break;
7459 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7460 }
7461 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7462 Info.ptrVal = I.getArgOperand(0);
7463 Info.offset = 0;
7464 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7465 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7466 Info.vol = false; // volatile stores with NEON intrinsics not supported
7467 Info.readMem = false;
7468 Info.writeMem = true;
7469 return true;
7470 }
7471 default:
7472 break;
7473 }
7474
7475 return false;
7476}