blob: 5c1cdedd84af4caa4641c060107129bd56df783b [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);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000399
400 // Memory operations
401 // RTABI chapter 4.3.4
402 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
403 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
404 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000405 }
406
David Goodwinf1daf7d2009-07-08 23:10:31 +0000407 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000409 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000411 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000413 if (!Subtarget->isFPOnlySP())
414 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000417 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000418
419 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 addDRTypeForNEON(MVT::v2f32);
421 addDRTypeForNEON(MVT::v8i8);
422 addDRTypeForNEON(MVT::v4i16);
423 addDRTypeForNEON(MVT::v2i32);
424 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 addQRTypeForNEON(MVT::v4f32);
427 addQRTypeForNEON(MVT::v2f64);
428 addQRTypeForNEON(MVT::v16i8);
429 addQRTypeForNEON(MVT::v8i16);
430 addQRTypeForNEON(MVT::v4i32);
431 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000432
Bob Wilson74dc72e2009-09-15 23:55:57 +0000433 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
434 // neither Neon nor VFP support any arithmetic operations on it.
435 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
436 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
437 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
438 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
439 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
440 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
441 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
442 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
443 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
444 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
445 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
446 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
447 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
448 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
449 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
450 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
451 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
452 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
453 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
454 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
455 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
456 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
457 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
458 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
459
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000460 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
461
Bob Wilson642b3292009-09-16 00:32:15 +0000462 // Neon does not support some operations on v1i64 and v2i64 types.
463 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000464 // Custom handling for some quad-vector types to detect VMULL.
465 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
466 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
467 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000468 // Custom handling for some vector types to avoid expensive expansions
469 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
470 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
471 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
472 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000473 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
474 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000475 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
476 // a destination type that is wider than the source.
477 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
478 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000479
Bob Wilson1c3ef902011-02-07 17:43:21 +0000480 setTargetDAGCombine(ISD::INTRINSIC_VOID);
481 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000482 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
483 setTargetDAGCombine(ISD::SHL);
484 setTargetDAGCombine(ISD::SRL);
485 setTargetDAGCombine(ISD::SRA);
486 setTargetDAGCombine(ISD::SIGN_EXTEND);
487 setTargetDAGCombine(ISD::ZERO_EXTEND);
488 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000489 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000490 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000491 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000492 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
493 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000494 }
495
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000496 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000497
498 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000500
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000501 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000503
Evan Chenga8e29892007-01-19 07:51:42 +0000504 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000505 if (!Subtarget->isThumb1Only()) {
506 for (unsigned im = (unsigned)ISD::PRE_INC;
507 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setIndexedLoadAction(im, MVT::i1, Legal);
509 setIndexedLoadAction(im, MVT::i8, Legal);
510 setIndexedLoadAction(im, MVT::i16, Legal);
511 setIndexedLoadAction(im, MVT::i32, Legal);
512 setIndexedStoreAction(im, MVT::i1, Legal);
513 setIndexedStoreAction(im, MVT::i8, Legal);
514 setIndexedStoreAction(im, MVT::i16, Legal);
515 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000516 }
Evan Chenga8e29892007-01-19 07:51:42 +0000517 }
518
519 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000520 setOperationAction(ISD::MUL, MVT::i64, Expand);
521 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000522 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
524 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000525 }
Eric Christopher2cc40132011-04-19 18:49:19 +0000526 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops())
527 setOperationAction(ISD::MULHS, MVT::i32, Expand);
528
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000529 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000530 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000531 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::SRL, MVT::i64, Custom);
533 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000534
535 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000537 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000539 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000541
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000542 // Only ARMv6 has BSWAP.
543 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000545
Evan Chenga8e29892007-01-19 07:51:42 +0000546 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000547 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000548 // v7M has a hardware divider
549 setOperationAction(ISD::SDIV, MVT::i32, Expand);
550 setOperationAction(ISD::UDIV, MVT::i32, Expand);
551 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 setOperationAction(ISD::SREM, MVT::i32, Expand);
553 setOperationAction(ISD::UREM, MVT::i32, Expand);
554 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
555 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000556
Owen Anderson825b72b2009-08-11 20:47:22 +0000557 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
558 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
559 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
560 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000561 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000562
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000563 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000564
Evan Chenga8e29892007-01-19 07:51:42 +0000565 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setOperationAction(ISD::VASTART, MVT::Other, Custom);
567 setOperationAction(ISD::VAARG, MVT::Other, Expand);
568 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
569 setOperationAction(ISD::VAEND, MVT::Other, Expand);
570 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
571 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000572 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000573 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
574 setExceptionPointerRegister(ARM::R0);
575 setExceptionSelectorRegister(ARM::R1);
576
Evan Cheng3a1588a2010-04-15 22:20:34 +0000577 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000578 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
579 // the default expansion.
580 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000581 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000582 // membarrier needs custom lowering; the rest are legal and handled
583 // normally.
584 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
585 } else {
586 // Set them all for expansion, which will force libcalls.
587 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
588 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
589 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
590 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000591 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
592 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
593 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000594 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
595 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
596 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
606 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
607 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
608 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
609 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
610 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000612 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i8, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i16, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i8, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i16, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i8, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i16, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
621 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i8, Expand);
622 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i16, Expand);
623 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000624 // Since the libcalls include locking, fold in the fences
625 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000626 }
627 // 64-bit versions are always libcalls (for now)
628 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000629 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000630 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
631 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
632 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
633 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
634 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
635 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000636
Evan Cheng416941d2010-11-04 05:19:35 +0000637 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000638
Eli Friedmana2c6f452010-06-26 04:36:50 +0000639 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
640 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
642 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000643 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000645
Nate Begemand1fb5832010-08-03 21:31:55 +0000646 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000647 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
648 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000649 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000650 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
651 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000652
653 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000655 if (Subtarget->isTargetDarwin()) {
656 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
657 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000658 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000659 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::SETCC, MVT::i32, Expand);
662 setOperationAction(ISD::SETCC, MVT::f32, Expand);
663 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000664 setOperationAction(ISD::SELECT, MVT::i32, Custom);
665 setOperationAction(ISD::SELECT, MVT::f32, Custom);
666 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
668 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
669 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000670
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
672 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
673 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
674 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
675 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000676
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000677 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 setOperationAction(ISD::FSIN, MVT::f64, Expand);
679 setOperationAction(ISD::FSIN, MVT::f32, Expand);
680 setOperationAction(ISD::FCOS, MVT::f32, Expand);
681 setOperationAction(ISD::FCOS, MVT::f64, Expand);
682 setOperationAction(ISD::FREM, MVT::f64, Expand);
683 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000684 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
686 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000687 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::FPOW, MVT::f64, Expand);
689 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000690
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000691 // Various VFP goodness
692 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000693 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
694 if (Subtarget->hasVFP2()) {
695 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
696 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
697 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
698 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
699 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000700 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000701 if (!Subtarget->hasFP16()) {
702 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
703 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000704 }
Evan Cheng110cf482008-04-01 01:50:16 +0000705 }
Evan Chenga8e29892007-01-19 07:51:42 +0000706
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000707 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000708 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000709 setTargetDAGCombine(ISD::ADD);
710 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000711 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000712
Owen Anderson080c0922010-11-05 19:27:46 +0000713 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000714 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000715 if (Subtarget->hasNEON())
716 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000717
Evan Chenga8e29892007-01-19 07:51:42 +0000718 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000719
Evan Chengf7d87ee2010-05-21 00:43:17 +0000720 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
721 setSchedulingPreference(Sched::RegPressure);
722 else
723 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000724
Evan Cheng05219282011-01-06 06:52:41 +0000725 //// temporary - rewrite interface to use type
726 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000727
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000728 // On ARM arguments smaller than 4 bytes are extended, so all arguments
729 // are at least 4 bytes aligned.
730 setMinStackArgumentAlignment(4);
731
Evan Chengfff606d2010-09-24 19:07:23 +0000732 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000733
734 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000735}
736
Andrew Trick32cec0a2011-01-19 02:35:27 +0000737// FIXME: It might make sense to define the representative register class as the
738// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
739// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
740// SPR's representative would be DPR_VFP2. This should work well if register
741// pressure tracking were modified such that a register use would increment the
742// pressure of the register class's representative and all of it's super
743// classes' representatives transitively. We have not implemented this because
744// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000745// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000746// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000747std::pair<const TargetRegisterClass*, uint8_t>
748ARMTargetLowering::findRepresentativeClass(EVT VT) const{
749 const TargetRegisterClass *RRC = 0;
750 uint8_t Cost = 1;
751 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000752 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000753 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000754 // Use DPR as representative register class for all floating point
755 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
756 // the cost is 1 for both f32 and f64.
757 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000758 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000759 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000760 // When NEON is used for SP, only half of the register file is available
761 // because operations that define both SP and DP results will be constrained
762 // to the VFP2 class (D0-D15). We currently model this constraint prior to
763 // coalescing by double-counting the SP regs. See the FIXME above.
764 if (Subtarget->useNEONForSinglePrecisionFP())
765 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000766 break;
767 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
768 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000769 RRC = ARM::DPRRegisterClass;
770 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000771 break;
772 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000773 RRC = ARM::DPRRegisterClass;
774 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000775 break;
776 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000777 RRC = ARM::DPRRegisterClass;
778 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000779 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000780 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000781 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000782}
783
Evan Chenga8e29892007-01-19 07:51:42 +0000784const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
785 switch (Opcode) {
786 default: return 0;
787 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000788 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000789 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000790 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
791 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000792 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000793 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
794 case ARMISD::tCALL: return "ARMISD::tCALL";
795 case ARMISD::BRCOND: return "ARMISD::BRCOND";
796 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000797 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000798 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
799 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
800 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000801 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000802 case ARMISD::CMPFP: return "ARMISD::CMPFP";
803 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000804 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000805 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
806 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000807
Jim Grosbach3482c802010-01-18 19:58:49 +0000808 case ARMISD::RBIT: return "ARMISD::RBIT";
809
Bob Wilson76a312b2010-03-19 22:51:32 +0000810 case ARMISD::FTOSI: return "ARMISD::FTOSI";
811 case ARMISD::FTOUI: return "ARMISD::FTOUI";
812 case ARMISD::SITOF: return "ARMISD::SITOF";
813 case ARMISD::UITOF: return "ARMISD::UITOF";
814
Evan Chenga8e29892007-01-19 07:51:42 +0000815 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
816 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
817 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000818
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000819 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
820 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000821
Evan Chengc5942082009-10-28 06:55:03 +0000822 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
823 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000824 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000825
Dale Johannesen51e28e62010-06-03 21:09:53 +0000826 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000827
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000828 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000829
Evan Cheng86198642009-08-07 00:34:42 +0000830 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
831
Jim Grosbach3728e962009-12-10 00:11:09 +0000832 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000833 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000834
Evan Chengdfed19f2010-11-03 06:34:55 +0000835 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
836
Bob Wilson5bafff32009-06-22 23:27:02 +0000837 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000838 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000839 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000840 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
841 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000842 case ARMISD::VCGEU: return "ARMISD::VCGEU";
843 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000844 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
845 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000846 case ARMISD::VCGTU: return "ARMISD::VCGTU";
847 case ARMISD::VTST: return "ARMISD::VTST";
848
849 case ARMISD::VSHL: return "ARMISD::VSHL";
850 case ARMISD::VSHRs: return "ARMISD::VSHRs";
851 case ARMISD::VSHRu: return "ARMISD::VSHRu";
852 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
853 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
854 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
855 case ARMISD::VSHRN: return "ARMISD::VSHRN";
856 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
857 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
858 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
859 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
860 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
861 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
862 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
863 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
864 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
865 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
866 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
867 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
868 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
869 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000870 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000871 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000872 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000873 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000874 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000875 case ARMISD::VREV64: return "ARMISD::VREV64";
876 case ARMISD::VREV32: return "ARMISD::VREV32";
877 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000878 case ARMISD::VZIP: return "ARMISD::VZIP";
879 case ARMISD::VUZP: return "ARMISD::VUZP";
880 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000881 case ARMISD::VTBL1: return "ARMISD::VTBL1";
882 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000883 case ARMISD::VMULLs: return "ARMISD::VMULLs";
884 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000885 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000886 case ARMISD::FMAX: return "ARMISD::FMAX";
887 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000888 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000889 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
890 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000891 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000892 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
893 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
894 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000895 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
896 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
897 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
898 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
899 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
900 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
901 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
902 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
903 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
904 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
905 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
906 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
907 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
908 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
909 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
910 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
911 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000912 }
913}
914
Evan Cheng06b666c2010-05-15 02:18:07 +0000915/// getRegClassFor - Return the register class that should be used for the
916/// specified value type.
917TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
918 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
919 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
920 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000921 if (Subtarget->hasNEON()) {
922 if (VT == MVT::v4i64)
923 return ARM::QQPRRegisterClass;
924 else if (VT == MVT::v8i64)
925 return ARM::QQQQPRRegisterClass;
926 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000927 return TargetLowering::getRegClassFor(VT);
928}
929
Eric Christopherab695882010-07-21 22:26:11 +0000930// Create a fast isel object.
931FastISel *
932ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
933 return ARM::createFastISel(funcInfo);
934}
935
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000936/// getMaximalGlobalOffset - Returns the maximal possible offset which can
937/// be used for loads / stores from the global.
938unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
939 return (Subtarget->isThumb1Only() ? 127 : 4095);
940}
941
Evan Cheng1cc39842010-05-20 23:26:43 +0000942Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000943 unsigned NumVals = N->getNumValues();
944 if (!NumVals)
945 return Sched::RegPressure;
946
947 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000948 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000949 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000950 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000951 if (VT.isFloatingPoint() || VT.isVector())
952 return Sched::Latency;
953 }
Evan Chengc10f5432010-05-28 23:25:23 +0000954
955 if (!N->isMachineOpcode())
956 return Sched::RegPressure;
957
958 // Load are scheduled for latency even if there instruction itinerary
959 // is not available.
960 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
961 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000962
963 if (TID.getNumDefs() == 0)
964 return Sched::RegPressure;
965 if (!Itins->isEmpty() &&
966 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000967 return Sched::Latency;
968
Evan Cheng1cc39842010-05-20 23:26:43 +0000969 return Sched::RegPressure;
970}
971
Evan Chenga8e29892007-01-19 07:51:42 +0000972//===----------------------------------------------------------------------===//
973// Lowering Code
974//===----------------------------------------------------------------------===//
975
Evan Chenga8e29892007-01-19 07:51:42 +0000976/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
977static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
978 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000979 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000980 case ISD::SETNE: return ARMCC::NE;
981 case ISD::SETEQ: return ARMCC::EQ;
982 case ISD::SETGT: return ARMCC::GT;
983 case ISD::SETGE: return ARMCC::GE;
984 case ISD::SETLT: return ARMCC::LT;
985 case ISD::SETLE: return ARMCC::LE;
986 case ISD::SETUGT: return ARMCC::HI;
987 case ISD::SETUGE: return ARMCC::HS;
988 case ISD::SETULT: return ARMCC::LO;
989 case ISD::SETULE: return ARMCC::LS;
990 }
991}
992
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000993/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
994static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000995 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000996 CondCode2 = ARMCC::AL;
997 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000998 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000999 case ISD::SETEQ:
1000 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1001 case ISD::SETGT:
1002 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1003 case ISD::SETGE:
1004 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1005 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001006 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001007 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1008 case ISD::SETO: CondCode = ARMCC::VC; break;
1009 case ISD::SETUO: CondCode = ARMCC::VS; break;
1010 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1011 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1012 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1013 case ISD::SETLT:
1014 case ISD::SETULT: CondCode = ARMCC::LT; break;
1015 case ISD::SETLE:
1016 case ISD::SETULE: CondCode = ARMCC::LE; break;
1017 case ISD::SETNE:
1018 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1019 }
Evan Chenga8e29892007-01-19 07:51:42 +00001020}
1021
Bob Wilson1f595bb2009-04-17 19:07:39 +00001022//===----------------------------------------------------------------------===//
1023// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001024//===----------------------------------------------------------------------===//
1025
1026#include "ARMGenCallingConv.inc"
1027
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001028/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1029/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001030CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001031 bool Return,
1032 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001033 switch (CC) {
1034 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001035 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001036 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001037 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001038 if (!Subtarget->isAAPCS_ABI())
1039 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1040 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1041 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1042 }
1043 // Fallthrough
1044 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001045 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001046 if (!Subtarget->isAAPCS_ABI())
1047 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1048 else if (Subtarget->hasVFP2() &&
1049 FloatABIType == FloatABI::Hard && !isVarArg)
1050 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1051 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1052 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001053 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001054 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001055 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001056 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001057 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001058 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001059 }
1060}
1061
Dan Gohman98ca4f22009-08-05 01:29:28 +00001062/// LowerCallResult - Lower the result values of a call into the
1063/// appropriate copies out of appropriate physical registers.
1064SDValue
1065ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001066 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001067 const SmallVectorImpl<ISD::InputArg> &Ins,
1068 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001069 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001070
Bob Wilson1f595bb2009-04-17 19:07:39 +00001071 // Assign locations to each value returned by this call.
1072 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001073 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001074 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001075 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001076 CCAssignFnForNode(CallConv, /* Return*/ true,
1077 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001078
1079 // Copy all of the result registers out of their specified physreg.
1080 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1081 CCValAssign VA = RVLocs[i];
1082
Bob Wilson80915242009-04-25 00:33:20 +00001083 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001084 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001085 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001087 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001088 Chain = Lo.getValue(1);
1089 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001090 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001091 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001092 InFlag);
1093 Chain = Hi.getValue(1);
1094 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001095 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001096
Owen Anderson825b72b2009-08-11 20:47:22 +00001097 if (VA.getLocVT() == MVT::v2f64) {
1098 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1099 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1100 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001101
1102 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001103 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001104 Chain = Lo.getValue(1);
1105 InFlag = Lo.getValue(2);
1106 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001108 Chain = Hi.getValue(1);
1109 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001110 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001111 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1112 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001113 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001114 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001115 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1116 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001117 Chain = Val.getValue(1);
1118 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119 }
Bob Wilson80915242009-04-25 00:33:20 +00001120
1121 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001122 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001123 case CCValAssign::Full: break;
1124 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001125 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001126 break;
1127 }
1128
Dan Gohman98ca4f22009-08-05 01:29:28 +00001129 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130 }
1131
Dan Gohman98ca4f22009-08-05 01:29:28 +00001132 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001133}
1134
Bob Wilsondee46d72009-04-17 20:35:10 +00001135/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001137ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1138 SDValue StackPtr, SDValue Arg,
1139 DebugLoc dl, SelectionDAG &DAG,
1140 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001141 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142 unsigned LocMemOffset = VA.getLocMemOffset();
1143 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1144 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001145 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001146 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001147 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001148}
1149
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001151 SDValue Chain, SDValue &Arg,
1152 RegsToPassVector &RegsToPass,
1153 CCValAssign &VA, CCValAssign &NextVA,
1154 SDValue &StackPtr,
1155 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001156 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001157
Jim Grosbache5165492009-11-09 00:11:35 +00001158 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001159 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001160 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1161
1162 if (NextVA.isRegLoc())
1163 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1164 else {
1165 assert(NextVA.isMemLoc());
1166 if (StackPtr.getNode() == 0)
1167 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1168
Dan Gohman98ca4f22009-08-05 01:29:28 +00001169 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1170 dl, DAG, NextVA,
1171 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001172 }
1173}
1174
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001176/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1177/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001178SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001179ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001180 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001181 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001182 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001183 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184 const SmallVectorImpl<ISD::InputArg> &Ins,
1185 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001186 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001187 MachineFunction &MF = DAG.getMachineFunction();
1188 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1189 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001190 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001191 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001192 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001193 if (isTailCall) {
1194 // Check if it's really possible to do a tail call.
1195 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1196 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001197 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001198 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1199 // detected sibcalls.
1200 if (isTailCall) {
1201 ++NumTailCalls;
1202 IsSibCall = true;
1203 }
1204 }
Evan Chenga8e29892007-01-19 07:51:42 +00001205
Bob Wilson1f595bb2009-04-17 19:07:39 +00001206 // Analyze operands of the call, assigning locations to each operand.
1207 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1209 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00001210 CCInfo.setCallOrPrologue(Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001211 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001212 CCAssignFnForNode(CallConv, /* Return*/ false,
1213 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001214
Bob Wilson1f595bb2009-04-17 19:07:39 +00001215 // Get a count of how many bytes are to be pushed on the stack.
1216 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001217
Dale Johannesen51e28e62010-06-03 21:09:53 +00001218 // For tail calls, memory operands are available in our caller's stack.
1219 if (IsSibCall)
1220 NumBytes = 0;
1221
Evan Chenga8e29892007-01-19 07:51:42 +00001222 // Adjust the stack pointer for the new arguments...
1223 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001224 if (!IsSibCall)
1225 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001226
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001227 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001228
Bob Wilson5bafff32009-06-22 23:27:02 +00001229 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001231
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001233 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001234 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1235 i != e;
1236 ++i, ++realArgIdx) {
1237 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001238 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001239 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001240 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001241
Bob Wilson1f595bb2009-04-17 19:07:39 +00001242 // Promote the value if needed.
1243 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001244 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 case CCValAssign::Full: break;
1246 case CCValAssign::SExt:
1247 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1248 break;
1249 case CCValAssign::ZExt:
1250 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1251 break;
1252 case CCValAssign::AExt:
1253 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1254 break;
1255 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001256 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001258 }
1259
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001260 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 if (VA.getLocVT() == MVT::v2f64) {
1263 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1264 DAG.getConstant(0, MVT::i32));
1265 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1266 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001267
Dan Gohman98ca4f22009-08-05 01:29:28 +00001268 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001269 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1270
1271 VA = ArgLocs[++i]; // skip ahead to next loc
1272 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1275 } else {
1276 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001277
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1279 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001280 }
1281 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001282 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001283 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001284 }
1285 } else if (VA.isRegLoc()) {
1286 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001287 } else if (isByVal) {
1288 assert(VA.isMemLoc());
1289 unsigned offset = 0;
1290
1291 // True if this byval aggregate will be split between registers
1292 // and memory.
1293 if (CCInfo.isFirstByValRegValid()) {
1294 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1295 unsigned int i, j;
1296 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1297 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1298 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1299 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1300 MachinePointerInfo(),
1301 false, false, 0);
1302 MemOpChains.push_back(Load.getValue(1));
1303 RegsToPass.push_back(std::make_pair(j, Load));
1304 }
1305 offset = ARM::R4 - CCInfo.getFirstByValReg();
1306 CCInfo.clearFirstByValReg();
1307 }
1308
1309 unsigned LocMemOffset = VA.getLocMemOffset();
1310 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1311 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1312 StkPtrOff);
1313 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1314 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1315 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1316 MVT::i32);
1317 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1318 Flags.getByValAlign(),
1319 /*isVolatile=*/false,
1320 /*AlwaysInline=*/false,
1321 MachinePointerInfo(0),
1322 MachinePointerInfo(0)));
1323
1324 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001325 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001326
Dan Gohman98ca4f22009-08-05 01:29:28 +00001327 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1328 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001329 }
Evan Chenga8e29892007-01-19 07:51:42 +00001330 }
1331
1332 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001334 &MemOpChains[0], MemOpChains.size());
1335
1336 // Build a sequence of copy-to-reg nodes chained together with token chain
1337 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001338 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001339 // Tail call byval lowering might overwrite argument registers so in case of
1340 // tail call optimization the copies to registers are lowered later.
1341 if (!isTailCall)
1342 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1343 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1344 RegsToPass[i].second, InFlag);
1345 InFlag = Chain.getValue(1);
1346 }
Evan Chenga8e29892007-01-19 07:51:42 +00001347
Dale Johannesen51e28e62010-06-03 21:09:53 +00001348 // For tail calls lower the arguments to the 'real' stack slot.
1349 if (isTailCall) {
1350 // Force all the incoming stack arguments to be loaded from the stack
1351 // before any new outgoing arguments are stored to the stack, because the
1352 // outgoing stack slots may alias the incoming argument stack slots, and
1353 // the alias isn't otherwise explicit. This is slightly more conservative
1354 // than necessary, because it means that each store effectively depends
1355 // on every argument instead of just those arguments it would clobber.
1356
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001357 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001358 InFlag = SDValue();
1359 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1360 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1361 RegsToPass[i].second, InFlag);
1362 InFlag = Chain.getValue(1);
1363 }
1364 InFlag =SDValue();
1365 }
1366
Bill Wendling056292f2008-09-16 21:48:12 +00001367 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1368 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1369 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001370 bool isDirect = false;
1371 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001372 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001373 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001374
1375 if (EnableARMLongCalls) {
1376 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1377 && "long-calls with non-static relocation model!");
1378 // Handle a global address or an external symbol. If it's not one of
1379 // those, the target's already in a register, so we don't need to do
1380 // anything extra.
1381 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001382 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001383 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001384 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001385 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1386 ARMPCLabelIndex,
1387 ARMCP::CPValue, 0);
1388 // Get the address of the callee into a register
1389 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1390 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1391 Callee = DAG.getLoad(getPointerTy(), dl,
1392 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001393 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001394 false, false, 0);
1395 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1396 const char *Sym = S->getSymbol();
1397
1398 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001399 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001400 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1401 Sym, ARMPCLabelIndex, 0);
1402 // Get the address of the callee into a register
1403 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1404 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1405 Callee = DAG.getLoad(getPointerTy(), dl,
1406 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001407 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001408 false, false, 0);
1409 }
1410 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001411 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001412 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001413 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001414 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001415 getTargetMachine().getRelocationModel() != Reloc::Static;
1416 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001417 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001418 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001419 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001420 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001421 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001422 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001423 ARMPCLabelIndex,
1424 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001425 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001427 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001428 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001429 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001430 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001431 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001432 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001433 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001434 } else {
1435 // On ELF targets for PIC code, direct calls should go through the PLT
1436 unsigned OpFlags = 0;
1437 if (Subtarget->isTargetELF() &&
1438 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1439 OpFlags = ARMII::MO_PLT;
1440 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1441 }
Bill Wendling056292f2008-09-16 21:48:12 +00001442 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001443 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001444 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001445 getTargetMachine().getRelocationModel() != Reloc::Static;
1446 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001447 // tBX takes a register source operand.
1448 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001449 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001450 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001451 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001452 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001453 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001454 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001455 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001456 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001457 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001458 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001459 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001460 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001461 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001462 } else {
1463 unsigned OpFlags = 0;
1464 // On ELF targets for PIC code, direct calls should go through the PLT
1465 if (Subtarget->isTargetELF() &&
1466 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1467 OpFlags = ARMII::MO_PLT;
1468 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1469 }
Evan Chenga8e29892007-01-19 07:51:42 +00001470 }
1471
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001472 // FIXME: handle tail calls differently.
1473 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001474 if (Subtarget->isThumb()) {
1475 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001476 CallOpc = ARMISD::CALL_NOLINK;
1477 else
1478 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1479 } else {
1480 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001481 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1482 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001483 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001484
Dan Gohman475871a2008-07-27 21:46:04 +00001485 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001486 Ops.push_back(Chain);
1487 Ops.push_back(Callee);
1488
1489 // Add argument registers to the end of the list so that they are known live
1490 // into the call.
1491 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1492 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1493 RegsToPass[i].second.getValueType()));
1494
Gabor Greifba36cb52008-08-28 21:40:38 +00001495 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001496 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001497
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001498 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001499 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001500 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001501
Duncan Sands4bdcb612008-07-02 17:40:58 +00001502 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001503 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001504 InFlag = Chain.getValue(1);
1505
Chris Lattnere563bbc2008-10-11 22:08:30 +00001506 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1507 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001509 InFlag = Chain.getValue(1);
1510
Bob Wilson1f595bb2009-04-17 19:07:39 +00001511 // Handle result values, copying them out of physregs into vregs that we
1512 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001513 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1514 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001515}
1516
Stuart Hastingsf222e592011-02-28 17:17:53 +00001517/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001518/// on the stack. Remember the next parameter register to allocate,
1519/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001520/// this.
1521void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001522llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1523 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1524 assert((State->getCallOrPrologue() == Prologue ||
1525 State->getCallOrPrologue() == Call) &&
1526 "unhandled ParmContext");
1527 if ((!State->isFirstByValRegValid()) &&
1528 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1529 State->setFirstByValReg(reg);
1530 // At a call site, a byval parameter that is split between
1531 // registers and memory needs its size truncated here. In a
1532 // function prologue, such byval parameters are reassembled in
1533 // memory, and are not truncated.
1534 if (State->getCallOrPrologue() == Call) {
1535 unsigned excess = 4 * (ARM::R4 - reg);
1536 assert(size >= excess && "expected larger existing stack allocation");
1537 size -= excess;
1538 }
1539 }
1540 // Confiscate any remaining parameter registers to preclude their
1541 // assignment to subsequent parameters.
1542 while (State->AllocateReg(GPRArgRegs, 4))
1543 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001544}
1545
Dale Johannesen51e28e62010-06-03 21:09:53 +00001546/// MatchingStackOffset - Return true if the given stack call argument is
1547/// already available in the same position (relatively) of the caller's
1548/// incoming argument stack.
1549static
1550bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1551 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1552 const ARMInstrInfo *TII) {
1553 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1554 int FI = INT_MAX;
1555 if (Arg.getOpcode() == ISD::CopyFromReg) {
1556 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001557 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001558 return false;
1559 MachineInstr *Def = MRI->getVRegDef(VR);
1560 if (!Def)
1561 return false;
1562 if (!Flags.isByVal()) {
1563 if (!TII->isLoadFromStackSlot(Def, FI))
1564 return false;
1565 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001566 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001567 }
1568 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1569 if (Flags.isByVal())
1570 // ByVal argument is passed in as a pointer but it's now being
1571 // dereferenced. e.g.
1572 // define @foo(%struct.X* %A) {
1573 // tail call @bar(%struct.X* byval %A)
1574 // }
1575 return false;
1576 SDValue Ptr = Ld->getBasePtr();
1577 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1578 if (!FINode)
1579 return false;
1580 FI = FINode->getIndex();
1581 } else
1582 return false;
1583
1584 assert(FI != INT_MAX);
1585 if (!MFI->isFixedObjectIndex(FI))
1586 return false;
1587 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1588}
1589
1590/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1591/// for tail call optimization. Targets which want to do tail call
1592/// optimization should implement this function.
1593bool
1594ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1595 CallingConv::ID CalleeCC,
1596 bool isVarArg,
1597 bool isCalleeStructRet,
1598 bool isCallerStructRet,
1599 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001600 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001601 const SmallVectorImpl<ISD::InputArg> &Ins,
1602 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001603 const Function *CallerF = DAG.getMachineFunction().getFunction();
1604 CallingConv::ID CallerCC = CallerF->getCallingConv();
1605 bool CCMatch = CallerCC == CalleeCC;
1606
1607 // Look for obvious safe cases to perform tail call optimization that do not
1608 // require ABI changes. This is what gcc calls sibcall.
1609
Jim Grosbach7616b642010-06-16 23:45:49 +00001610 // Do not sibcall optimize vararg calls unless the call site is not passing
1611 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001612 if (isVarArg && !Outs.empty())
1613 return false;
1614
1615 // Also avoid sibcall optimization if either caller or callee uses struct
1616 // return semantics.
1617 if (isCalleeStructRet || isCallerStructRet)
1618 return false;
1619
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001620 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001621 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001622 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1623 // LR. This means if we need to reload LR, it takes an extra instructions,
1624 // which outweighs the value of the tail call; but here we don't know yet
1625 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001626 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001627 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001628
1629 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1630 // but we need to make sure there are enough registers; the only valid
1631 // registers are the 4 used for parameters. We don't currently do this
1632 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001633 if (Subtarget->isThumb1Only())
1634 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001635
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636 // If the calling conventions do not match, then we'd better make sure the
1637 // results are returned in the same way as what the caller expects.
1638 if (!CCMatch) {
1639 SmallVector<CCValAssign, 16> RVLocs1;
1640 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1641 RVLocs1, *DAG.getContext());
1642 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1643
1644 SmallVector<CCValAssign, 16> RVLocs2;
1645 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1646 RVLocs2, *DAG.getContext());
1647 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1648
1649 if (RVLocs1.size() != RVLocs2.size())
1650 return false;
1651 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1652 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1653 return false;
1654 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1655 return false;
1656 if (RVLocs1[i].isRegLoc()) {
1657 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1658 return false;
1659 } else {
1660 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1661 return false;
1662 }
1663 }
1664 }
1665
1666 // If the callee takes no arguments then go on to check the results of the
1667 // call.
1668 if (!Outs.empty()) {
1669 // Check if stack adjustment is needed. For now, do not do this if any
1670 // argument is passed on the stack.
1671 SmallVector<CCValAssign, 16> ArgLocs;
1672 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1673 ArgLocs, *DAG.getContext());
1674 CCInfo.AnalyzeCallOperands(Outs,
1675 CCAssignFnForNode(CalleeCC, false, isVarArg));
1676 if (CCInfo.getNextStackOffset()) {
1677 MachineFunction &MF = DAG.getMachineFunction();
1678
1679 // Check if the arguments are already laid out in the right way as
1680 // the caller's fixed stack objects.
1681 MachineFrameInfo *MFI = MF.getFrameInfo();
1682 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1683 const ARMInstrInfo *TII =
1684 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001685 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1686 i != e;
1687 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001688 CCValAssign &VA = ArgLocs[i];
1689 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001690 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001691 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001692 if (VA.getLocInfo() == CCValAssign::Indirect)
1693 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001694 if (VA.needsCustom()) {
1695 // f64 and vector types are split into multiple registers or
1696 // register/stack-slot combinations. The types will not match
1697 // the registers; give up on memory f64 refs until we figure
1698 // out what to do about this.
1699 if (!VA.isRegLoc())
1700 return false;
1701 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001702 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001703 if (RegVT == MVT::v2f64) {
1704 if (!ArgLocs[++i].isRegLoc())
1705 return false;
1706 if (!ArgLocs[++i].isRegLoc())
1707 return false;
1708 }
1709 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001710 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1711 MFI, MRI, TII))
1712 return false;
1713 }
1714 }
1715 }
1716 }
1717
1718 return true;
1719}
1720
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721SDValue
1722ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001723 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001725 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001726 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001727
Bob Wilsondee46d72009-04-17 20:35:10 +00001728 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001729 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001730
Bob Wilsondee46d72009-04-17 20:35:10 +00001731 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1733 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001734
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001736 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1737 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001738
1739 // If this is the first return lowered for this function, add
1740 // the regs to the liveout set for the function.
1741 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1742 for (unsigned i = 0; i != RVLocs.size(); ++i)
1743 if (RVLocs[i].isRegLoc())
1744 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001745 }
1746
Bob Wilson1f595bb2009-04-17 19:07:39 +00001747 SDValue Flag;
1748
1749 // Copy the result values into the output registers.
1750 for (unsigned i = 0, realRVLocIdx = 0;
1751 i != RVLocs.size();
1752 ++i, ++realRVLocIdx) {
1753 CCValAssign &VA = RVLocs[i];
1754 assert(VA.isRegLoc() && "Can only return in registers!");
1755
Dan Gohmanc9403652010-07-07 15:54:55 +00001756 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757
1758 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001759 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001760 case CCValAssign::Full: break;
1761 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001762 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001763 break;
1764 }
1765
Bob Wilson1f595bb2009-04-17 19:07:39 +00001766 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001767 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001768 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1770 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001771 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001773
1774 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1775 Flag = Chain.getValue(1);
1776 VA = RVLocs[++i]; // skip ahead to next loc
1777 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1778 HalfGPRs.getValue(1), Flag);
1779 Flag = Chain.getValue(1);
1780 VA = RVLocs[++i]; // skip ahead to next loc
1781
1782 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001783 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1784 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001785 }
1786 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1787 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001788 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001789 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001790 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001791 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001792 VA = RVLocs[++i]; // skip ahead to next loc
1793 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1794 Flag);
1795 } else
1796 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1797
Bob Wilsondee46d72009-04-17 20:35:10 +00001798 // Guarantee that all emitted copies are
1799 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001800 Flag = Chain.getValue(1);
1801 }
1802
1803 SDValue result;
1804 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001806 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001807 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001808
1809 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001810}
1811
Evan Cheng3d2125c2010-11-30 23:55:39 +00001812bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1813 if (N->getNumValues() != 1)
1814 return false;
1815 if (!N->hasNUsesOfValue(1, 0))
1816 return false;
1817
1818 unsigned NumCopies = 0;
1819 SDNode* Copies[2];
1820 SDNode *Use = *N->use_begin();
1821 if (Use->getOpcode() == ISD::CopyToReg) {
1822 Copies[NumCopies++] = Use;
1823 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1824 // f64 returned in a pair of GPRs.
1825 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1826 UI != UE; ++UI) {
1827 if (UI->getOpcode() != ISD::CopyToReg)
1828 return false;
1829 Copies[UI.getUse().getResNo()] = *UI;
1830 ++NumCopies;
1831 }
1832 } else if (Use->getOpcode() == ISD::BITCAST) {
1833 // f32 returned in a single GPR.
1834 if (!Use->hasNUsesOfValue(1, 0))
1835 return false;
1836 Use = *Use->use_begin();
1837 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1838 return false;
1839 Copies[NumCopies++] = Use;
1840 } else {
1841 return false;
1842 }
1843
1844 if (NumCopies != 1 && NumCopies != 2)
1845 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001846
1847 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001848 for (unsigned i = 0; i < NumCopies; ++i) {
1849 SDNode *Copy = Copies[i];
1850 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1851 UI != UE; ++UI) {
1852 if (UI->getOpcode() == ISD::CopyToReg) {
1853 SDNode *Use = *UI;
1854 if (Use == Copies[0] || Use == Copies[1])
1855 continue;
1856 return false;
1857 }
1858 if (UI->getOpcode() != ARMISD::RET_FLAG)
1859 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001860 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001861 }
1862 }
1863
Evan Cheng1bf891a2010-12-01 22:59:46 +00001864 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001865}
1866
Evan Cheng485fafc2011-03-21 01:19:09 +00001867bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1868 if (!EnableARMTailCalls)
1869 return false;
1870
1871 if (!CI->isTailCall())
1872 return false;
1873
1874 return !Subtarget->isThumb1Only();
1875}
1876
Bob Wilsonb62d2572009-11-03 00:02:05 +00001877// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1878// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1879// one of the above mentioned nodes. It has to be wrapped because otherwise
1880// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1881// be used to form addressing mode. These wrapped nodes will be selected
1882// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001883static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001884 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001885 // FIXME there is no actual debug info here
1886 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001887 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001888 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001889 if (CP->isMachineConstantPoolEntry())
1890 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1891 CP->getAlignment());
1892 else
1893 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1894 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001896}
1897
Jim Grosbache1102ca2010-07-19 17:20:38 +00001898unsigned ARMTargetLowering::getJumpTableEncoding() const {
1899 return MachineJumpTableInfo::EK_Inline;
1900}
1901
Dan Gohmand858e902010-04-17 15:26:15 +00001902SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1903 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001904 MachineFunction &MF = DAG.getMachineFunction();
1905 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1906 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001907 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001908 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001909 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001910 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1911 SDValue CPAddr;
1912 if (RelocM == Reloc::Static) {
1913 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1914 } else {
1915 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001916 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001917 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1918 ARMCP::CPBlockAddress,
1919 PCAdj);
1920 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1921 }
1922 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1923 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001924 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001925 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001926 if (RelocM == Reloc::Static)
1927 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001928 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001929 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001930}
1931
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001932// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001933SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001934ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001935 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001936 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001937 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001938 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001939 MachineFunction &MF = DAG.getMachineFunction();
1940 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001941 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001942 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001943 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001944 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001945 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001947 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001948 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001949 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001950 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001951
Evan Chenge7e0d622009-11-06 22:24:13 +00001952 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001953 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001954
1955 // call __tls_get_addr.
1956 ArgListTy Args;
1957 ArgListEntry Entry;
1958 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001959 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001960 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001961 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001962 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001963 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1964 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001966 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001967 return CallResult.first;
1968}
1969
1970// Lower ISD::GlobalTLSAddress using the "initial exec" or
1971// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001972SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001973ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001974 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001975 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001976 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001977 SDValue Offset;
1978 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001979 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001980 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001981 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982
Chris Lattner4fb63d02009-07-15 04:12:33 +00001983 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001984 MachineFunction &MF = DAG.getMachineFunction();
1985 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001986 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001987 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001988 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1989 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001990 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001991 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001992 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001994 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001995 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001996 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001997 Chain = Offset.getValue(1);
1998
Evan Chenge7e0d622009-11-06 22:24:13 +00001999 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002000 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002001
Evan Cheng9eda6892009-10-31 03:39:36 +00002002 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002003 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002004 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002005 } else {
2006 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002007 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002008 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002009 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002010 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002011 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002012 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002013 }
2014
2015 // The address of the thread local variable is the add of the thread
2016 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002017 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002018}
2019
Dan Gohman475871a2008-07-27 21:46:04 +00002020SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002021ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002022 // TODO: implement the "local dynamic" model
2023 assert(Subtarget->isTargetELF() &&
2024 "TLS not implemented for non-ELF targets");
2025 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2026 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2027 // otherwise use the "Local Exec" TLS Model
2028 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2029 return LowerToTLSGeneralDynamicModel(GA, DAG);
2030 else
2031 return LowerToTLSExecModels(GA, DAG);
2032}
2033
Dan Gohman475871a2008-07-27 21:46:04 +00002034SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002035 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002036 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002037 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002038 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002039 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2040 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002041 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002042 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002043 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002044 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002046 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002047 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002048 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002049 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002050 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002051 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002052 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002053 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002054 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002055 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002056 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002057 }
2058
2059 // If we have T2 ops, we can materialize the address directly via movt/movw
2060 // pair. This is always cheaper.
2061 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002062 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002063 // FIXME: Once remat is capable of dealing with instructions with register
2064 // operands, expand this into two nodes.
2065 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2066 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002067 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002068 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2069 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2070 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2071 MachinePointerInfo::getConstantPool(),
2072 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002073 }
2074}
2075
Dan Gohman475871a2008-07-27 21:46:04 +00002076SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002077 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002078 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002079 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002080 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002081 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002082 MachineFunction &MF = DAG.getMachineFunction();
2083 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2084
2085 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002086 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002087 // FIXME: Once remat is capable of dealing with instructions with register
2088 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002089 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002090 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2091 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2092
Evan Cheng53519f02011-01-21 18:55:51 +00002093 unsigned Wrapper = (RelocM == Reloc::PIC_)
2094 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2095 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002096 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002097 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2098 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2099 MachinePointerInfo::getGOT(), false, false, 0);
2100 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002101 }
2102
2103 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002104 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002105 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002106 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002107 } else {
2108 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002109 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2110 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002111 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002112 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002113 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002114 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002115
Evan Cheng9eda6892009-10-31 03:39:36 +00002116 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002117 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002118 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002119 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002120
2121 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002122 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002123 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002124 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002125
Evan Cheng63476a82009-09-03 07:04:02 +00002126 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002127 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002128 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002129
2130 return Result;
2131}
2132
Dan Gohman475871a2008-07-27 21:46:04 +00002133SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002134 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002135 assert(Subtarget->isTargetELF() &&
2136 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002137 MachineFunction &MF = DAG.getMachineFunction();
2138 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002139 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002140 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002141 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002142 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002143 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2144 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002145 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002146 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002148 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002149 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002150 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002153}
2154
Jim Grosbach0e0da732009-05-12 23:59:14 +00002155SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002156ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2157 const {
2158 DebugLoc dl = Op.getDebugLoc();
2159 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002160 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002161}
2162
2163SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002164ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2165 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002166 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002167 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2168 Op.getOperand(1), Val);
2169}
2170
2171SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002172ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2173 DebugLoc dl = Op.getDebugLoc();
2174 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2175 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2176}
2177
2178SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002179ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002180 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002181 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002182 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002183 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002184 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002185 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002186 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002187 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2188 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002189 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002190 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002191 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002192 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002193 EVT PtrVT = getPointerTy();
2194 DebugLoc dl = Op.getDebugLoc();
2195 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2196 SDValue CPAddr;
2197 unsigned PCAdj = (RelocM != Reloc::PIC_)
2198 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002199 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002200 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2201 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002202 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002203 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002204 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002205 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002206 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002207 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002208
2209 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002210 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002211 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2212 }
2213 return Result;
2214 }
Evan Cheng92e39162011-03-29 23:06:19 +00002215 case Intrinsic::arm_neon_vmulls:
2216 case Intrinsic::arm_neon_vmullu: {
2217 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2218 ? ARMISD::VMULLs : ARMISD::VMULLu;
2219 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2220 Op.getOperand(1), Op.getOperand(2));
2221 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002222 }
2223}
2224
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002225static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002226 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002227 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002228 if (!Subtarget->hasDataBarrier()) {
2229 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2230 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2231 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002232 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002233 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002234 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002235 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002236 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002237
2238 SDValue Op5 = Op.getOperand(5);
2239 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2240 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2241 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2242 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2243
2244 ARM_MB::MemBOpt DMBOpt;
2245 if (isDeviceBarrier)
2246 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2247 else
2248 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2249 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2250 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002251}
2252
Evan Chengdfed19f2010-11-03 06:34:55 +00002253static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2254 const ARMSubtarget *Subtarget) {
2255 // ARM pre v5TE and Thumb1 does not have preload instructions.
2256 if (!(Subtarget->isThumb2() ||
2257 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2258 // Just preserve the chain.
2259 return Op.getOperand(0);
2260
2261 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002262 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2263 if (!isRead &&
2264 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2265 // ARMv7 with MP extension has PLDW.
2266 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002267
2268 if (Subtarget->isThumb())
2269 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002270 isRead = ~isRead & 1;
2271 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002272
Evan Cheng416941d2010-11-04 05:19:35 +00002273 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002274 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002275 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2276 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002277}
2278
Dan Gohman1e93df62010-04-17 14:41:14 +00002279static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2280 MachineFunction &MF = DAG.getMachineFunction();
2281 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2282
Evan Chenga8e29892007-01-19 07:51:42 +00002283 // vastart just stores the address of the VarArgsFrameIndex slot into the
2284 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002285 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002286 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002287 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002288 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002289 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2290 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002291}
2292
Dan Gohman475871a2008-07-27 21:46:04 +00002293SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002294ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2295 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002296 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002297 MachineFunction &MF = DAG.getMachineFunction();
2298 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2299
2300 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002301 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002302 RC = ARM::tGPRRegisterClass;
2303 else
2304 RC = ARM::GPRRegisterClass;
2305
2306 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002307 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002308 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002309
2310 SDValue ArgValue2;
2311 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002312 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002313 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002314
2315 // Create load node to retrieve arguments from the stack.
2316 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002317 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002318 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002319 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002320 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002321 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002322 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002323 }
2324
Jim Grosbache5165492009-11-09 00:11:35 +00002325 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002326}
2327
Stuart Hastingsc7315872011-04-20 16:47:52 +00002328void
2329ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2330 unsigned &VARegSize, unsigned &VARegSaveSize)
2331 const {
2332 unsigned NumGPRs;
2333 if (CCInfo.isFirstByValRegValid())
2334 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2335 else {
2336 unsigned int firstUnalloced;
2337 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2338 sizeof(GPRArgRegs) /
2339 sizeof(GPRArgRegs[0]));
2340 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2341 }
2342
2343 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2344 VARegSize = NumGPRs * 4;
2345 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2346}
2347
2348// The remaining GPRs hold either the beginning of variable-argument
2349// data, or the beginning of an aggregate passed by value (usuall
2350// byval). Either way, we allocate stack slots adjacent to the data
2351// provided by our caller, and store the unallocated registers there.
2352// If this is a variadic function, the va_list pointer will begin with
2353// these values; otherwise, this reassembles a (byval) structure that
2354// was split between registers and memory.
2355void
2356ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2357 DebugLoc dl, SDValue &Chain,
2358 unsigned ArgOffset) const {
2359 MachineFunction &MF = DAG.getMachineFunction();
2360 MachineFrameInfo *MFI = MF.getFrameInfo();
2361 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2362 unsigned firstRegToSaveIndex;
2363 if (CCInfo.isFirstByValRegValid())
2364 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2365 else {
2366 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2367 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2368 }
2369
2370 unsigned VARegSize, VARegSaveSize;
2371 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2372 if (VARegSaveSize) {
2373 // If this function is vararg, store any remaining integer argument regs
2374 // to their spots on the stack so that they may be loaded by deferencing
2375 // the result of va_next.
2376 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002377 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2378 ArgOffset + VARegSaveSize
2379 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002380 false));
2381 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2382 getPointerTy());
2383
2384 SmallVector<SDValue, 4> MemOps;
2385 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2386 TargetRegisterClass *RC;
2387 if (AFI->isThumb1OnlyFunction())
2388 RC = ARM::tGPRRegisterClass;
2389 else
2390 RC = ARM::GPRRegisterClass;
2391
2392 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2393 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2394 SDValue Store =
2395 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002396 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002397 false, false, 0);
2398 MemOps.push_back(Store);
2399 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2400 DAG.getConstant(4, getPointerTy()));
2401 }
2402 if (!MemOps.empty())
2403 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2404 &MemOps[0], MemOps.size());
2405 } else
2406 // This will point to the next argument passed via stack.
2407 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2408}
2409
Bob Wilson5bafff32009-06-22 23:27:02 +00002410SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002411ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002412 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002413 const SmallVectorImpl<ISD::InputArg>
2414 &Ins,
2415 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002416 SmallVectorImpl<SDValue> &InVals)
2417 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002418 MachineFunction &MF = DAG.getMachineFunction();
2419 MachineFrameInfo *MFI = MF.getFrameInfo();
2420
Bob Wilson1f595bb2009-04-17 19:07:39 +00002421 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2422
2423 // Assign locations to all of the incoming arguments.
2424 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002425 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2426 *DAG.getContext());
Stuart Hastingsc7315872011-04-20 16:47:52 +00002427 CCInfo.setCallOrPrologue(Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002428 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002429 CCAssignFnForNode(CallConv, /* Return*/ false,
2430 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002431
2432 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002433 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002434
Stuart Hastingsf222e592011-02-28 17:17:53 +00002435 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002436 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2437 CCValAssign &VA = ArgLocs[i];
2438
Bob Wilsondee46d72009-04-17 20:35:10 +00002439 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002440 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002441 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002442
Bob Wilson1f595bb2009-04-17 19:07:39 +00002443 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002444 // f64 and vector types are split up into multiple registers or
2445 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002447 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002448 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002449 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002450 SDValue ArgValue2;
2451 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002452 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002453 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2454 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002455 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002456 false, false, 0);
2457 } else {
2458 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2459 Chain, DAG, dl);
2460 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002461 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2462 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002463 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002464 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002465 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2466 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002467 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002468
Bob Wilson5bafff32009-06-22 23:27:02 +00002469 } else {
2470 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002471
Owen Anderson825b72b2009-08-11 20:47:22 +00002472 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002473 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002474 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002475 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002476 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002477 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002478 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002479 RC = (AFI->isThumb1OnlyFunction() ?
2480 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002481 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002482 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002483
2484 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002485 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002486 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002487 }
2488
2489 // If this is an 8 or 16-bit value, it is really passed promoted
2490 // to 32 bits. Insert an assert[sz]ext to capture this, then
2491 // truncate to the right size.
2492 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002493 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002494 case CCValAssign::Full: break;
2495 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002496 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002497 break;
2498 case CCValAssign::SExt:
2499 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2500 DAG.getValueType(VA.getValVT()));
2501 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2502 break;
2503 case CCValAssign::ZExt:
2504 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2505 DAG.getValueType(VA.getValVT()));
2506 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2507 break;
2508 }
2509
Dan Gohman98ca4f22009-08-05 01:29:28 +00002510 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002511
2512 } else { // VA.isRegLoc()
2513
2514 // sanity check
2515 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002516 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002517
Stuart Hastingsf222e592011-02-28 17:17:53 +00002518 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002519
Stuart Hastingsf222e592011-02-28 17:17:53 +00002520 // Some Ins[] entries become multiple ArgLoc[] entries.
2521 // Process them only once.
2522 if (index != lastInsIndex)
2523 {
2524 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher5ac179c2011-04-29 23:12:01 +00002525 // FIXME: For now, all byval parameter objects are marked mutable.
2526 // This can be changed with more analysis.
2527 // In case of tail call optimization mark all arguments mutable.
2528 // Since they could be overwritten by lowering of arguments in case of
2529 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002530 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002531 unsigned VARegSize, VARegSaveSize;
2532 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2533 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2534 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002535 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002536 int FI = MFI->CreateFixedObject(Bytes,
2537 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002538 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2539 } else {
2540 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2541 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002542
Stuart Hastingsf222e592011-02-28 17:17:53 +00002543 // Create load nodes to retrieve arguments from the stack.
2544 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2545 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2546 MachinePointerInfo::getFixedStack(FI),
2547 false, false, 0));
2548 }
2549 lastInsIndex = index;
2550 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002551 }
2552 }
2553
2554 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002555 if (isVarArg)
2556 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002557
Dan Gohman98ca4f22009-08-05 01:29:28 +00002558 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002559}
2560
2561/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002562static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002563 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002564 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002565 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002566 // Maybe this has already been legalized into the constant pool?
2567 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002568 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002569 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002570 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002571 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002572 }
2573 }
2574 return false;
2575}
2576
Evan Chenga8e29892007-01-19 07:51:42 +00002577/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2578/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002579SDValue
2580ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002581 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002582 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002583 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002584 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002585 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002586 // Constant does not fit, try adjusting it by one?
2587 switch (CC) {
2588 default: break;
2589 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002590 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002591 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002592 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002593 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002594 }
2595 break;
2596 case ISD::SETULT:
2597 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002598 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002599 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002600 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002601 }
2602 break;
2603 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002604 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002605 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002606 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002607 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002608 }
2609 break;
2610 case ISD::SETULE:
2611 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002612 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002613 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002614 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002615 }
2616 break;
2617 }
2618 }
2619 }
2620
2621 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002622 ARMISD::NodeType CompareType;
2623 switch (CondCode) {
2624 default:
2625 CompareType = ARMISD::CMP;
2626 break;
2627 case ARMCC::EQ:
2628 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002629 // Uses only Z Flag
2630 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002631 break;
2632 }
Evan Cheng218977b2010-07-13 19:27:42 +00002633 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002634 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002635}
2636
2637/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002638SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002639ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002640 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002641 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002642 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002643 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002644 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002645 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2646 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002647}
2648
Bob Wilson79f56c92011-03-08 01:17:20 +00002649/// duplicateCmp - Glue values can have only one use, so this function
2650/// duplicates a comparison node.
2651SDValue
2652ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2653 unsigned Opc = Cmp.getOpcode();
2654 DebugLoc DL = Cmp.getDebugLoc();
2655 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2656 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2657
2658 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2659 Cmp = Cmp.getOperand(0);
2660 Opc = Cmp.getOpcode();
2661 if (Opc == ARMISD::CMPFP)
2662 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2663 else {
2664 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2665 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2666 }
2667 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2668}
2669
Bill Wendlingde2b1512010-08-11 08:43:16 +00002670SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2671 SDValue Cond = Op.getOperand(0);
2672 SDValue SelectTrue = Op.getOperand(1);
2673 SDValue SelectFalse = Op.getOperand(2);
2674 DebugLoc dl = Op.getDebugLoc();
2675
2676 // Convert:
2677 //
2678 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2679 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2680 //
2681 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2682 const ConstantSDNode *CMOVTrue =
2683 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2684 const ConstantSDNode *CMOVFalse =
2685 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2686
2687 if (CMOVTrue && CMOVFalse) {
2688 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2689 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2690
2691 SDValue True;
2692 SDValue False;
2693 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2694 True = SelectTrue;
2695 False = SelectFalse;
2696 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2697 True = SelectFalse;
2698 False = SelectTrue;
2699 }
2700
2701 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002702 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002703 SDValue ARMcc = Cond.getOperand(2);
2704 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002705 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002706 assert(True.getValueType() == VT);
2707 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002708 }
2709 }
2710 }
2711
2712 return DAG.getSelectCC(dl, Cond,
2713 DAG.getConstant(0, Cond.getValueType()),
2714 SelectTrue, SelectFalse, ISD::SETNE);
2715}
2716
Dan Gohmand858e902010-04-17 15:26:15 +00002717SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002718 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002719 SDValue LHS = Op.getOperand(0);
2720 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002721 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002722 SDValue TrueVal = Op.getOperand(2);
2723 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002724 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002725
Owen Anderson825b72b2009-08-11 20:47:22 +00002726 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002727 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002728 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002729 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2730 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002731 }
2732
2733 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002734 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002735
Evan Cheng218977b2010-07-13 19:27:42 +00002736 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2737 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002738 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002739 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002740 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002741 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002742 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002743 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002744 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002745 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002746 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002747 }
2748 return Result;
2749}
2750
Evan Cheng218977b2010-07-13 19:27:42 +00002751/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2752/// to morph to an integer compare sequence.
2753static bool canChangeToInt(SDValue Op, bool &SeenZero,
2754 const ARMSubtarget *Subtarget) {
2755 SDNode *N = Op.getNode();
2756 if (!N->hasOneUse())
2757 // Otherwise it requires moving the value from fp to integer registers.
2758 return false;
2759 if (!N->getNumValues())
2760 return false;
2761 EVT VT = Op.getValueType();
2762 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2763 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2764 // vmrs are very slow, e.g. cortex-a8.
2765 return false;
2766
2767 if (isFloatingPointZero(Op)) {
2768 SeenZero = true;
2769 return true;
2770 }
2771 return ISD::isNormalLoad(N);
2772}
2773
2774static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2775 if (isFloatingPointZero(Op))
2776 return DAG.getConstant(0, MVT::i32);
2777
2778 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2779 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002780 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002781 Ld->isVolatile(), Ld->isNonTemporal(),
2782 Ld->getAlignment());
2783
2784 llvm_unreachable("Unknown VFP cmp argument!");
2785}
2786
2787static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2788 SDValue &RetVal1, SDValue &RetVal2) {
2789 if (isFloatingPointZero(Op)) {
2790 RetVal1 = DAG.getConstant(0, MVT::i32);
2791 RetVal2 = DAG.getConstant(0, MVT::i32);
2792 return;
2793 }
2794
2795 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2796 SDValue Ptr = Ld->getBasePtr();
2797 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2798 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002799 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002800 Ld->isVolatile(), Ld->isNonTemporal(),
2801 Ld->getAlignment());
2802
2803 EVT PtrType = Ptr.getValueType();
2804 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2805 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2806 PtrType, Ptr, DAG.getConstant(4, PtrType));
2807 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2808 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002809 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002810 Ld->isVolatile(), Ld->isNonTemporal(),
2811 NewAlign);
2812 return;
2813 }
2814
2815 llvm_unreachable("Unknown VFP cmp argument!");
2816}
2817
2818/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2819/// f32 and even f64 comparisons to integer ones.
2820SDValue
2821ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2822 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002823 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002824 SDValue LHS = Op.getOperand(2);
2825 SDValue RHS = Op.getOperand(3);
2826 SDValue Dest = Op.getOperand(4);
2827 DebugLoc dl = Op.getDebugLoc();
2828
2829 bool SeenZero = false;
2830 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2831 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002832 // If one of the operand is zero, it's safe to ignore the NaN case since
2833 // we only care about equality comparisons.
2834 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002835 // If unsafe fp math optimization is enabled and there are no other uses of
2836 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002837 // to an integer comparison.
2838 if (CC == ISD::SETOEQ)
2839 CC = ISD::SETEQ;
2840 else if (CC == ISD::SETUNE)
2841 CC = ISD::SETNE;
2842
2843 SDValue ARMcc;
2844 if (LHS.getValueType() == MVT::f32) {
2845 LHS = bitcastf32Toi32(LHS, DAG);
2846 RHS = bitcastf32Toi32(RHS, DAG);
2847 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2848 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2849 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2850 Chain, Dest, ARMcc, CCR, Cmp);
2851 }
2852
2853 SDValue LHS1, LHS2;
2854 SDValue RHS1, RHS2;
2855 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2856 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2857 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2858 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002859 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002860 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2861 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2862 }
2863
2864 return SDValue();
2865}
2866
2867SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2868 SDValue Chain = Op.getOperand(0);
2869 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2870 SDValue LHS = Op.getOperand(2);
2871 SDValue RHS = Op.getOperand(3);
2872 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002873 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002874
Owen Anderson825b72b2009-08-11 20:47:22 +00002875 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002876 SDValue ARMcc;
2877 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002878 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002879 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002880 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002881 }
2882
Owen Anderson825b72b2009-08-11 20:47:22 +00002883 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002884
2885 if (UnsafeFPMath &&
2886 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2887 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2888 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2889 if (Result.getNode())
2890 return Result;
2891 }
2892
Evan Chenga8e29892007-01-19 07:51:42 +00002893 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002894 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002895
Evan Cheng218977b2010-07-13 19:27:42 +00002896 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2897 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002899 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002900 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002901 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002902 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002903 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2904 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002905 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002906 }
2907 return Res;
2908}
2909
Dan Gohmand858e902010-04-17 15:26:15 +00002910SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002911 SDValue Chain = Op.getOperand(0);
2912 SDValue Table = Op.getOperand(1);
2913 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002914 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002915
Owen Andersone50ed302009-08-10 22:56:29 +00002916 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002917 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2918 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002919 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002920 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002921 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002922 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2923 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002924 if (Subtarget->isThumb2()) {
2925 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2926 // which does another jump to the destination. This also makes it easier
2927 // to translate it to TBB / TBH later.
2928 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002929 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002930 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002931 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002932 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002933 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002934 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002935 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002936 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002937 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002938 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002939 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002940 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002941 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002942 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002943 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002944 }
Evan Chenga8e29892007-01-19 07:51:42 +00002945}
2946
Bob Wilson76a312b2010-03-19 22:51:32 +00002947static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2948 DebugLoc dl = Op.getDebugLoc();
2949 unsigned Opc;
2950
2951 switch (Op.getOpcode()) {
2952 default:
2953 assert(0 && "Invalid opcode!");
2954 case ISD::FP_TO_SINT:
2955 Opc = ARMISD::FTOSI;
2956 break;
2957 case ISD::FP_TO_UINT:
2958 Opc = ARMISD::FTOUI;
2959 break;
2960 }
2961 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002962 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002963}
2964
Cameron Zwarich3007d332011-03-29 21:41:55 +00002965static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2966 EVT VT = Op.getValueType();
2967 DebugLoc dl = Op.getDebugLoc();
2968
2969 EVT OperandVT = Op.getOperand(0).getValueType();
2970 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2971 if (VT != MVT::v4f32)
2972 return DAG.UnrollVectorOp(Op.getNode());
2973
2974 unsigned CastOpc;
2975 unsigned Opc;
2976 switch (Op.getOpcode()) {
2977 default:
2978 assert(0 && "Invalid opcode!");
2979 case ISD::SINT_TO_FP:
2980 CastOpc = ISD::SIGN_EXTEND;
2981 Opc = ISD::SINT_TO_FP;
2982 break;
2983 case ISD::UINT_TO_FP:
2984 CastOpc = ISD::ZERO_EXTEND;
2985 Opc = ISD::UINT_TO_FP;
2986 break;
2987 }
2988
2989 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2990 return DAG.getNode(Opc, dl, VT, Op);
2991}
2992
Bob Wilson76a312b2010-03-19 22:51:32 +00002993static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2994 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002995 if (VT.isVector())
2996 return LowerVectorINT_TO_FP(Op, DAG);
2997
Bob Wilson76a312b2010-03-19 22:51:32 +00002998 DebugLoc dl = Op.getDebugLoc();
2999 unsigned Opc;
3000
3001 switch (Op.getOpcode()) {
3002 default:
3003 assert(0 && "Invalid opcode!");
3004 case ISD::SINT_TO_FP:
3005 Opc = ARMISD::SITOF;
3006 break;
3007 case ISD::UINT_TO_FP:
3008 Opc = ARMISD::UITOF;
3009 break;
3010 }
3011
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003012 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003013 return DAG.getNode(Opc, dl, VT, Op);
3014}
3015
Evan Cheng515fe3a2010-07-08 02:08:50 +00003016SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003017 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003018 SDValue Tmp0 = Op.getOperand(0);
3019 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003020 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003021 EVT VT = Op.getValueType();
3022 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003023 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3024 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3025 bool UseNEON = !InGPR && Subtarget->hasNEON();
3026
3027 if (UseNEON) {
3028 // Use VBSL to copy the sign bit.
3029 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3030 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3031 DAG.getTargetConstant(EncodedVal, MVT::i32));
3032 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3033 if (VT == MVT::f64)
3034 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3035 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3036 DAG.getConstant(32, MVT::i32));
3037 else /*if (VT == MVT::f32)*/
3038 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3039 if (SrcVT == MVT::f32) {
3040 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3041 if (VT == MVT::f64)
3042 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3043 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3044 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003045 } else if (VT == MVT::f32)
3046 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3047 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3048 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003049 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3050 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3051
3052 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3053 MVT::i32);
3054 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3055 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3056 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003057
Evan Chenge573fb32011-02-23 02:24:55 +00003058 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3059 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3060 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003061 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003062 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3063 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3064 DAG.getConstant(0, MVT::i32));
3065 } else {
3066 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3067 }
3068
3069 return Res;
3070 }
Evan Chengc143dd42011-02-11 02:28:55 +00003071
3072 // Bitcast operand 1 to i32.
3073 if (SrcVT == MVT::f64)
3074 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3075 &Tmp1, 1).getValue(1);
3076 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3077
Evan Chenge573fb32011-02-23 02:24:55 +00003078 // Or in the signbit with integer operations.
3079 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3080 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3081 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3082 if (VT == MVT::f32) {
3083 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3084 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3085 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3086 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003087 }
3088
Evan Chenge573fb32011-02-23 02:24:55 +00003089 // f64: Or the high part with signbit and then combine two parts.
3090 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3091 &Tmp0, 1);
3092 SDValue Lo = Tmp0.getValue(0);
3093 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3094 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3095 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003096}
3097
Evan Cheng2457f2c2010-05-22 01:47:14 +00003098SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3099 MachineFunction &MF = DAG.getMachineFunction();
3100 MachineFrameInfo *MFI = MF.getFrameInfo();
3101 MFI->setReturnAddressIsTaken(true);
3102
3103 EVT VT = Op.getValueType();
3104 DebugLoc dl = Op.getDebugLoc();
3105 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3106 if (Depth) {
3107 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3108 SDValue Offset = DAG.getConstant(4, MVT::i32);
3109 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3110 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003111 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003112 }
3113
3114 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003115 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003116 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3117}
3118
Dan Gohmand858e902010-04-17 15:26:15 +00003119SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003120 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3121 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003122
Owen Andersone50ed302009-08-10 22:56:29 +00003123 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003124 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3125 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003126 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003127 ? ARM::R7 : ARM::R11;
3128 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3129 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003130 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3131 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003132 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003133 return FrameAddr;
3134}
3135
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003136/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003137/// expand a bit convert where either the source or destination type is i64 to
3138/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3139/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3140/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003141static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003142 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3143 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003144 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003145
Bob Wilson9f3f0612010-04-17 05:30:19 +00003146 // This function is only supposed to be called for i64 types, either as the
3147 // source or destination of the bit convert.
3148 EVT SrcVT = Op.getValueType();
3149 EVT DstVT = N->getValueType(0);
3150 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003151 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003152
Bob Wilson9f3f0612010-04-17 05:30:19 +00003153 // Turn i64->f64 into VMOVDRR.
3154 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3156 DAG.getConstant(0, MVT::i32));
3157 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3158 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003159 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003160 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003161 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003162
Jim Grosbache5165492009-11-09 00:11:35 +00003163 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003164 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3165 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3166 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3167 // Merge the pieces into a single i64 value.
3168 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3169 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003170
Bob Wilson9f3f0612010-04-17 05:30:19 +00003171 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003172}
3173
Bob Wilson5bafff32009-06-22 23:27:02 +00003174/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003175/// Zero vectors are used to represent vector negation and in those cases
3176/// will be implemented with the NEON VNEG instruction. However, VNEG does
3177/// not support i64 elements, so sometimes the zero vectors will need to be
3178/// explicitly constructed. Regardless, use a canonical VMOV to create the
3179/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003180static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003181 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003182 // The canonical modified immediate encoding of a zero vector is....0!
3183 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3184 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3185 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003186 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003187}
3188
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003189/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3190/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003191SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3192 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003193 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3194 EVT VT = Op.getValueType();
3195 unsigned VTBits = VT.getSizeInBits();
3196 DebugLoc dl = Op.getDebugLoc();
3197 SDValue ShOpLo = Op.getOperand(0);
3198 SDValue ShOpHi = Op.getOperand(1);
3199 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003200 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003201 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003202
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003203 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3204
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003205 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3206 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3207 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3208 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3209 DAG.getConstant(VTBits, MVT::i32));
3210 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3211 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003212 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003213
3214 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3215 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003216 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003217 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003218 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003219 CCR, Cmp);
3220
3221 SDValue Ops[2] = { Lo, Hi };
3222 return DAG.getMergeValues(Ops, 2, dl);
3223}
3224
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003225/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3226/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003227SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3228 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003229 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3230 EVT VT = Op.getValueType();
3231 unsigned VTBits = VT.getSizeInBits();
3232 DebugLoc dl = Op.getDebugLoc();
3233 SDValue ShOpLo = Op.getOperand(0);
3234 SDValue ShOpHi = Op.getOperand(1);
3235 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003236 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003237
3238 assert(Op.getOpcode() == ISD::SHL_PARTS);
3239 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3240 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3241 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3242 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3243 DAG.getConstant(VTBits, MVT::i32));
3244 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3245 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3246
3247 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3248 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3249 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003250 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003251 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003252 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003253 CCR, Cmp);
3254
3255 SDValue Ops[2] = { Lo, Hi };
3256 return DAG.getMergeValues(Ops, 2, dl);
3257}
3258
Jim Grosbach4725ca72010-09-08 03:54:02 +00003259SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003260 SelectionDAG &DAG) const {
3261 // The rounding mode is in bits 23:22 of the FPSCR.
3262 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3263 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3264 // so that the shift + and get folded into a bitfield extract.
3265 DebugLoc dl = Op.getDebugLoc();
3266 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3267 DAG.getConstant(Intrinsic::arm_get_fpscr,
3268 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003269 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003270 DAG.getConstant(1U << 22, MVT::i32));
3271 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3272 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003273 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003274 DAG.getConstant(3, MVT::i32));
3275}
3276
Jim Grosbach3482c802010-01-18 19:58:49 +00003277static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3278 const ARMSubtarget *ST) {
3279 EVT VT = N->getValueType(0);
3280 DebugLoc dl = N->getDebugLoc();
3281
3282 if (!ST->hasV6T2Ops())
3283 return SDValue();
3284
3285 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3286 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3287}
3288
Bob Wilson5bafff32009-06-22 23:27:02 +00003289static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3290 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003291 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003292 DebugLoc dl = N->getDebugLoc();
3293
Bob Wilsond5448bb2010-11-18 21:16:28 +00003294 if (!VT.isVector())
3295 return SDValue();
3296
Bob Wilson5bafff32009-06-22 23:27:02 +00003297 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003298 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003299
Bob Wilsond5448bb2010-11-18 21:16:28 +00003300 // Left shifts translate directly to the vshiftu intrinsic.
3301 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003302 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003303 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3304 N->getOperand(0), N->getOperand(1));
3305
3306 assert((N->getOpcode() == ISD::SRA ||
3307 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3308
3309 // NEON uses the same intrinsics for both left and right shifts. For
3310 // right shifts, the shift amounts are negative, so negate the vector of
3311 // shift amounts.
3312 EVT ShiftVT = N->getOperand(1).getValueType();
3313 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3314 getZeroVector(ShiftVT, DAG, dl),
3315 N->getOperand(1));
3316 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3317 Intrinsic::arm_neon_vshifts :
3318 Intrinsic::arm_neon_vshiftu);
3319 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3320 DAG.getConstant(vshiftInt, MVT::i32),
3321 N->getOperand(0), NegatedCount);
3322}
3323
3324static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3325 const ARMSubtarget *ST) {
3326 EVT VT = N->getValueType(0);
3327 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003328
Eli Friedmance392eb2009-08-22 03:13:10 +00003329 // We can get here for a node like i32 = ISD::SHL i32, i64
3330 if (VT != MVT::i64)
3331 return SDValue();
3332
3333 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003334 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003335
Chris Lattner27a6c732007-11-24 07:07:01 +00003336 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3337 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003338 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003339 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003340
Chris Lattner27a6c732007-11-24 07:07:01 +00003341 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003342 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003343
Chris Lattner27a6c732007-11-24 07:07:01 +00003344 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003346 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003348 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003349
Chris Lattner27a6c732007-11-24 07:07:01 +00003350 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3351 // captures the result into a carry flag.
3352 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003353 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003354
Chris Lattner27a6c732007-11-24 07:07:01 +00003355 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003357
Chris Lattner27a6c732007-11-24 07:07:01 +00003358 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003359 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003360}
3361
Bob Wilson5bafff32009-06-22 23:27:02 +00003362static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3363 SDValue TmpOp0, TmpOp1;
3364 bool Invert = false;
3365 bool Swap = false;
3366 unsigned Opc = 0;
3367
3368 SDValue Op0 = Op.getOperand(0);
3369 SDValue Op1 = Op.getOperand(1);
3370 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003371 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003372 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3373 DebugLoc dl = Op.getDebugLoc();
3374
3375 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3376 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003377 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003378 case ISD::SETUNE:
3379 case ISD::SETNE: Invert = true; // Fallthrough
3380 case ISD::SETOEQ:
3381 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3382 case ISD::SETOLT:
3383 case ISD::SETLT: Swap = true; // Fallthrough
3384 case ISD::SETOGT:
3385 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3386 case ISD::SETOLE:
3387 case ISD::SETLE: Swap = true; // Fallthrough
3388 case ISD::SETOGE:
3389 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3390 case ISD::SETUGE: Swap = true; // Fallthrough
3391 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3392 case ISD::SETUGT: Swap = true; // Fallthrough
3393 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3394 case ISD::SETUEQ: Invert = true; // Fallthrough
3395 case ISD::SETONE:
3396 // Expand this to (OLT | OGT).
3397 TmpOp0 = Op0;
3398 TmpOp1 = Op1;
3399 Opc = ISD::OR;
3400 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3401 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3402 break;
3403 case ISD::SETUO: Invert = true; // Fallthrough
3404 case ISD::SETO:
3405 // Expand this to (OLT | OGE).
3406 TmpOp0 = Op0;
3407 TmpOp1 = Op1;
3408 Opc = ISD::OR;
3409 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3410 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3411 break;
3412 }
3413 } else {
3414 // Integer comparisons.
3415 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003416 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003417 case ISD::SETNE: Invert = true;
3418 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3419 case ISD::SETLT: Swap = true;
3420 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3421 case ISD::SETLE: Swap = true;
3422 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3423 case ISD::SETULT: Swap = true;
3424 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3425 case ISD::SETULE: Swap = true;
3426 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3427 }
3428
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003429 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003430 if (Opc == ARMISD::VCEQ) {
3431
3432 SDValue AndOp;
3433 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3434 AndOp = Op0;
3435 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3436 AndOp = Op1;
3437
3438 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003439 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003440 AndOp = AndOp.getOperand(0);
3441
3442 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3443 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003444 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3445 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003446 Invert = !Invert;
3447 }
3448 }
3449 }
3450
3451 if (Swap)
3452 std::swap(Op0, Op1);
3453
Owen Andersonc24cb352010-11-08 23:21:22 +00003454 // If one of the operands is a constant vector zero, attempt to fold the
3455 // comparison to a specialized compare-against-zero form.
3456 SDValue SingleOp;
3457 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3458 SingleOp = Op0;
3459 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3460 if (Opc == ARMISD::VCGE)
3461 Opc = ARMISD::VCLEZ;
3462 else if (Opc == ARMISD::VCGT)
3463 Opc = ARMISD::VCLTZ;
3464 SingleOp = Op1;
3465 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003466
Owen Andersonc24cb352010-11-08 23:21:22 +00003467 SDValue Result;
3468 if (SingleOp.getNode()) {
3469 switch (Opc) {
3470 case ARMISD::VCEQ:
3471 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3472 case ARMISD::VCGE:
3473 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3474 case ARMISD::VCLEZ:
3475 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3476 case ARMISD::VCGT:
3477 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3478 case ARMISD::VCLTZ:
3479 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3480 default:
3481 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3482 }
3483 } else {
3484 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3485 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003486
3487 if (Invert)
3488 Result = DAG.getNOT(dl, Result, VT);
3489
3490 return Result;
3491}
3492
Bob Wilsond3c42842010-06-14 22:19:57 +00003493/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3494/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003495/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003496static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3497 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003498 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003499 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003500
Bob Wilson827b2102010-06-15 19:05:35 +00003501 // SplatBitSize is set to the smallest size that splats the vector, so a
3502 // zero vector will always have SplatBitSize == 8. However, NEON modified
3503 // immediate instructions others than VMOV do not support the 8-bit encoding
3504 // of a zero vector, and the default encoding of zero is supposed to be the
3505 // 32-bit version.
3506 if (SplatBits == 0)
3507 SplatBitSize = 32;
3508
Bob Wilson5bafff32009-06-22 23:27:02 +00003509 switch (SplatBitSize) {
3510 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003511 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003512 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003513 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003514 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003515 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003516 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003517 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003518 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003519
3520 case 16:
3521 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003522 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003523 if ((SplatBits & ~0xff) == 0) {
3524 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003525 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003526 Imm = SplatBits;
3527 break;
3528 }
3529 if ((SplatBits & ~0xff00) == 0) {
3530 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003531 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003532 Imm = SplatBits >> 8;
3533 break;
3534 }
3535 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003536
3537 case 32:
3538 // NEON's 32-bit VMOV supports splat values where:
3539 // * only one byte is nonzero, or
3540 // * the least significant byte is 0xff and the second byte is nonzero, or
3541 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003542 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003543 if ((SplatBits & ~0xff) == 0) {
3544 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003545 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003546 Imm = SplatBits;
3547 break;
3548 }
3549 if ((SplatBits & ~0xff00) == 0) {
3550 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003551 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003552 Imm = SplatBits >> 8;
3553 break;
3554 }
3555 if ((SplatBits & ~0xff0000) == 0) {
3556 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003557 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003558 Imm = SplatBits >> 16;
3559 break;
3560 }
3561 if ((SplatBits & ~0xff000000) == 0) {
3562 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003563 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003564 Imm = SplatBits >> 24;
3565 break;
3566 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003567
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003568 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3569 if (type == OtherModImm) return SDValue();
3570
Bob Wilson5bafff32009-06-22 23:27:02 +00003571 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003572 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3573 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003574 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003575 Imm = SplatBits >> 8;
3576 SplatBits |= 0xff;
3577 break;
3578 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003579
3580 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003581 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3582 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003583 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003584 Imm = SplatBits >> 16;
3585 SplatBits |= 0xffff;
3586 break;
3587 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003588
3589 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3590 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3591 // VMOV.I32. A (very) minor optimization would be to replicate the value
3592 // and fall through here to test for a valid 64-bit splat. But, then the
3593 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003594 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003595
3596 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003597 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003598 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003599 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003600 uint64_t BitMask = 0xff;
3601 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003602 unsigned ImmMask = 1;
3603 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003604 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003605 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003606 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003607 Imm |= ImmMask;
3608 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003609 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003610 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003611 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003613 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003614 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003615 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003616 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003617 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003618 break;
3619 }
3620
Bob Wilson1a913ed2010-06-11 21:34:50 +00003621 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003622 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003623 return SDValue();
3624 }
3625
Bob Wilsoncba270d2010-07-13 21:16:48 +00003626 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3627 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003628}
3629
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003630static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3631 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003632 unsigned NumElts = VT.getVectorNumElements();
3633 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003634
3635 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3636 if (M[0] < 0)
3637 return false;
3638
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003639 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003640
3641 // If this is a VEXT shuffle, the immediate value is the index of the first
3642 // element. The other shuffle indices must be the successive elements after
3643 // the first one.
3644 unsigned ExpectedElt = Imm;
3645 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003646 // Increment the expected index. If it wraps around, it may still be
3647 // a VEXT but the source vectors must be swapped.
3648 ExpectedElt += 1;
3649 if (ExpectedElt == NumElts * 2) {
3650 ExpectedElt = 0;
3651 ReverseVEXT = true;
3652 }
3653
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003654 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003655 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003656 return false;
3657 }
3658
3659 // Adjust the index value if the source operands will be swapped.
3660 if (ReverseVEXT)
3661 Imm -= NumElts;
3662
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003663 return true;
3664}
3665
Bob Wilson8bb9e482009-07-26 00:39:34 +00003666/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3667/// instruction with the specified blocksize. (The order of the elements
3668/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003669static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3670 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003671 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3672 "Only possible block sizes for VREV are: 16, 32, 64");
3673
Bob Wilson8bb9e482009-07-26 00:39:34 +00003674 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003675 if (EltSz == 64)
3676 return false;
3677
3678 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003679 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003680 // If the first shuffle index is UNDEF, be optimistic.
3681 if (M[0] < 0)
3682 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003683
3684 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3685 return false;
3686
3687 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003688 if (M[i] < 0) continue; // ignore UNDEF indices
3689 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003690 return false;
3691 }
3692
3693 return true;
3694}
3695
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003696static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3697 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3698 // range, then 0 is placed into the resulting vector. So pretty much any mask
3699 // of 8 elements can work here.
3700 return VT == MVT::v8i8 && M.size() == 8;
3701}
3702
Bob Wilsonc692cb72009-08-21 20:54:19 +00003703static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3704 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003705 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3706 if (EltSz == 64)
3707 return false;
3708
Bob Wilsonc692cb72009-08-21 20:54:19 +00003709 unsigned NumElts = VT.getVectorNumElements();
3710 WhichResult = (M[0] == 0 ? 0 : 1);
3711 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003712 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3713 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003714 return false;
3715 }
3716 return true;
3717}
3718
Bob Wilson324f4f12009-12-03 06:40:55 +00003719/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3720/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3721/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3722static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3723 unsigned &WhichResult) {
3724 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3725 if (EltSz == 64)
3726 return false;
3727
3728 unsigned NumElts = VT.getVectorNumElements();
3729 WhichResult = (M[0] == 0 ? 0 : 1);
3730 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003731 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3732 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003733 return false;
3734 }
3735 return true;
3736}
3737
Bob Wilsonc692cb72009-08-21 20:54:19 +00003738static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3739 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003740 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3741 if (EltSz == 64)
3742 return false;
3743
Bob Wilsonc692cb72009-08-21 20:54:19 +00003744 unsigned NumElts = VT.getVectorNumElements();
3745 WhichResult = (M[0] == 0 ? 0 : 1);
3746 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003747 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003748 if ((unsigned) M[i] != 2 * i + WhichResult)
3749 return false;
3750 }
3751
3752 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003753 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003754 return false;
3755
3756 return true;
3757}
3758
Bob Wilson324f4f12009-12-03 06:40:55 +00003759/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3760/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3761/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3762static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3763 unsigned &WhichResult) {
3764 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3765 if (EltSz == 64)
3766 return false;
3767
3768 unsigned Half = VT.getVectorNumElements() / 2;
3769 WhichResult = (M[0] == 0 ? 0 : 1);
3770 for (unsigned j = 0; j != 2; ++j) {
3771 unsigned Idx = WhichResult;
3772 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003773 int MIdx = M[i + j * Half];
3774 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003775 return false;
3776 Idx += 2;
3777 }
3778 }
3779
3780 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3781 if (VT.is64BitVector() && EltSz == 32)
3782 return false;
3783
3784 return true;
3785}
3786
Bob Wilsonc692cb72009-08-21 20:54:19 +00003787static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3788 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003789 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3790 if (EltSz == 64)
3791 return false;
3792
Bob Wilsonc692cb72009-08-21 20:54:19 +00003793 unsigned NumElts = VT.getVectorNumElements();
3794 WhichResult = (M[0] == 0 ? 0 : 1);
3795 unsigned Idx = WhichResult * NumElts / 2;
3796 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003797 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3798 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003799 return false;
3800 Idx += 1;
3801 }
3802
3803 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003804 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003805 return false;
3806
3807 return true;
3808}
3809
Bob Wilson324f4f12009-12-03 06:40:55 +00003810/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3811/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3812/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3813static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3814 unsigned &WhichResult) {
3815 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3816 if (EltSz == 64)
3817 return false;
3818
3819 unsigned NumElts = VT.getVectorNumElements();
3820 WhichResult = (M[0] == 0 ? 0 : 1);
3821 unsigned Idx = WhichResult * NumElts / 2;
3822 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003823 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3824 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003825 return false;
3826 Idx += 1;
3827 }
3828
3829 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3830 if (VT.is64BitVector() && EltSz == 32)
3831 return false;
3832
3833 return true;
3834}
3835
Dale Johannesenf630c712010-07-29 20:10:08 +00003836// If N is an integer constant that can be moved into a register in one
3837// instruction, return an SDValue of such a constant (will become a MOV
3838// instruction). Otherwise return null.
3839static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3840 const ARMSubtarget *ST, DebugLoc dl) {
3841 uint64_t Val;
3842 if (!isa<ConstantSDNode>(N))
3843 return SDValue();
3844 Val = cast<ConstantSDNode>(N)->getZExtValue();
3845
3846 if (ST->isThumb1Only()) {
3847 if (Val <= 255 || ~Val <= 255)
3848 return DAG.getConstant(Val, MVT::i32);
3849 } else {
3850 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3851 return DAG.getConstant(Val, MVT::i32);
3852 }
3853 return SDValue();
3854}
3855
Bob Wilson5bafff32009-06-22 23:27:02 +00003856// If this is a case we can't handle, return null and let the default
3857// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003858SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3859 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003860 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003861 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003862 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003863
3864 APInt SplatBits, SplatUndef;
3865 unsigned SplatBitSize;
3866 bool HasAnyUndefs;
3867 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003868 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003869 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003870 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003871 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003872 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003873 DAG, VmovVT, VT.is128BitVector(),
3874 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003875 if (Val.getNode()) {
3876 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003877 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003878 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003879
3880 // Try an immediate VMVN.
3881 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3882 ((1LL << SplatBitSize) - 1));
3883 Val = isNEONModifiedImm(NegatedImm,
3884 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003885 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003886 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003887 if (Val.getNode()) {
3888 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003889 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003890 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003891 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003892 }
3893
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003894 // Scan through the operands to see if only one value is used.
3895 unsigned NumElts = VT.getVectorNumElements();
3896 bool isOnlyLowElement = true;
3897 bool usesOnlyOneValue = true;
3898 bool isConstant = true;
3899 SDValue Value;
3900 for (unsigned i = 0; i < NumElts; ++i) {
3901 SDValue V = Op.getOperand(i);
3902 if (V.getOpcode() == ISD::UNDEF)
3903 continue;
3904 if (i > 0)
3905 isOnlyLowElement = false;
3906 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3907 isConstant = false;
3908
3909 if (!Value.getNode())
3910 Value = V;
3911 else if (V != Value)
3912 usesOnlyOneValue = false;
3913 }
3914
3915 if (!Value.getNode())
3916 return DAG.getUNDEF(VT);
3917
3918 if (isOnlyLowElement)
3919 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3920
Dale Johannesenf630c712010-07-29 20:10:08 +00003921 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3922
Dale Johannesen575cd142010-10-19 20:00:17 +00003923 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3924 // i32 and try again.
3925 if (usesOnlyOneValue && EltSize <= 32) {
3926 if (!isConstant)
3927 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3928 if (VT.getVectorElementType().isFloatingPoint()) {
3929 SmallVector<SDValue, 8> Ops;
3930 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003931 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003932 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003933 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3934 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003935 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3936 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003937 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003938 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003939 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3940 if (Val.getNode())
3941 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003942 }
3943
3944 // If all elements are constants and the case above didn't get hit, fall back
3945 // to the default expansion, which will generate a load from the constant
3946 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003947 if (isConstant)
3948 return SDValue();
3949
Bob Wilson11a1dff2011-01-07 21:37:30 +00003950 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3951 if (NumElts >= 4) {
3952 SDValue shuffle = ReconstructShuffle(Op, DAG);
3953 if (shuffle != SDValue())
3954 return shuffle;
3955 }
3956
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003957 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003958 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3959 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003960 if (EltSize >= 32) {
3961 // Do the expansion with floating-point types, since that is what the VFP
3962 // registers are defined to use, and since i64 is not legal.
3963 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3964 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003965 SmallVector<SDValue, 8> Ops;
3966 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003967 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003968 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003969 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003970 }
3971
3972 return SDValue();
3973}
3974
Bob Wilson11a1dff2011-01-07 21:37:30 +00003975// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003976// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003977SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3978 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003979 DebugLoc dl = Op.getDebugLoc();
3980 EVT VT = Op.getValueType();
3981 unsigned NumElts = VT.getVectorNumElements();
3982
3983 SmallVector<SDValue, 2> SourceVecs;
3984 SmallVector<unsigned, 2> MinElts;
3985 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003986
Bob Wilson11a1dff2011-01-07 21:37:30 +00003987 for (unsigned i = 0; i < NumElts; ++i) {
3988 SDValue V = Op.getOperand(i);
3989 if (V.getOpcode() == ISD::UNDEF)
3990 continue;
3991 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3992 // A shuffle can only come from building a vector from various
3993 // elements of other vectors.
3994 return SDValue();
3995 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003996
Bob Wilson11a1dff2011-01-07 21:37:30 +00003997 // Record this extraction against the appropriate vector if possible...
3998 SDValue SourceVec = V.getOperand(0);
3999 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4000 bool FoundSource = false;
4001 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4002 if (SourceVecs[j] == SourceVec) {
4003 if (MinElts[j] > EltNo)
4004 MinElts[j] = EltNo;
4005 if (MaxElts[j] < EltNo)
4006 MaxElts[j] = EltNo;
4007 FoundSource = true;
4008 break;
4009 }
4010 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004011
Bob Wilson11a1dff2011-01-07 21:37:30 +00004012 // Or record a new source if not...
4013 if (!FoundSource) {
4014 SourceVecs.push_back(SourceVec);
4015 MinElts.push_back(EltNo);
4016 MaxElts.push_back(EltNo);
4017 }
4018 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004019
Bob Wilson11a1dff2011-01-07 21:37:30 +00004020 // Currently only do something sane when at most two source vectors
4021 // involved.
4022 if (SourceVecs.size() > 2)
4023 return SDValue();
4024
4025 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4026 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004027
Bob Wilson11a1dff2011-01-07 21:37:30 +00004028 // This loop extracts the usage patterns of the source vectors
4029 // and prepares appropriate SDValues for a shuffle if possible.
4030 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4031 if (SourceVecs[i].getValueType() == VT) {
4032 // No VEXT necessary
4033 ShuffleSrcs[i] = SourceVecs[i];
4034 VEXTOffsets[i] = 0;
4035 continue;
4036 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4037 // It probably isn't worth padding out a smaller vector just to
4038 // break it down again in a shuffle.
4039 return SDValue();
4040 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004041
Bob Wilson11a1dff2011-01-07 21:37:30 +00004042 // Since only 64-bit and 128-bit vectors are legal on ARM and
4043 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004044 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4045 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004046
Bob Wilson11a1dff2011-01-07 21:37:30 +00004047 if (MaxElts[i] - MinElts[i] >= NumElts) {
4048 // Span too large for a VEXT to cope
4049 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004050 }
4051
Bob Wilson11a1dff2011-01-07 21:37:30 +00004052 if (MinElts[i] >= NumElts) {
4053 // The extraction can just take the second half
4054 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004055 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4056 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004057 DAG.getIntPtrConstant(NumElts));
4058 } else if (MaxElts[i] < NumElts) {
4059 // The extraction can just take the first half
4060 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004061 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4062 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004063 DAG.getIntPtrConstant(0));
4064 } else {
4065 // An actual VEXT is needed
4066 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004067 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4068 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004069 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004070 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4071 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004072 DAG.getIntPtrConstant(NumElts));
4073 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4074 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4075 }
4076 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004077
Bob Wilson11a1dff2011-01-07 21:37:30 +00004078 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004079
Bob Wilson11a1dff2011-01-07 21:37:30 +00004080 for (unsigned i = 0; i < NumElts; ++i) {
4081 SDValue Entry = Op.getOperand(i);
4082 if (Entry.getOpcode() == ISD::UNDEF) {
4083 Mask.push_back(-1);
4084 continue;
4085 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004086
Bob Wilson11a1dff2011-01-07 21:37:30 +00004087 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004088 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4089 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004090 if (ExtractVec == SourceVecs[0]) {
4091 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4092 } else {
4093 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4094 }
4095 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004096
Bob Wilson11a1dff2011-01-07 21:37:30 +00004097 // Final check before we try to produce nonsense...
4098 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004099 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4100 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004101
Bob Wilson11a1dff2011-01-07 21:37:30 +00004102 return SDValue();
4103}
4104
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004105/// isShuffleMaskLegal - Targets can use this to indicate that they only
4106/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4107/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4108/// are assumed to be legal.
4109bool
4110ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4111 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004112 if (VT.getVectorNumElements() == 4 &&
4113 (VT.is128BitVector() || VT.is64BitVector())) {
4114 unsigned PFIndexes[4];
4115 for (unsigned i = 0; i != 4; ++i) {
4116 if (M[i] < 0)
4117 PFIndexes[i] = 8;
4118 else
4119 PFIndexes[i] = M[i];
4120 }
4121
4122 // Compute the index in the perfect shuffle table.
4123 unsigned PFTableIndex =
4124 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4125 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4126 unsigned Cost = (PFEntry >> 30);
4127
4128 if (Cost <= 4)
4129 return true;
4130 }
4131
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004132 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004133 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004134
Bob Wilson53dd2452010-06-07 23:53:38 +00004135 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4136 return (EltSize >= 32 ||
4137 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004138 isVREVMask(M, VT, 64) ||
4139 isVREVMask(M, VT, 32) ||
4140 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004141 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004142 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004143 isVTRNMask(M, VT, WhichResult) ||
4144 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004145 isVZIPMask(M, VT, WhichResult) ||
4146 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4147 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4148 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004149}
4150
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004151/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4152/// the specified operations to build the shuffle.
4153static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4154 SDValue RHS, SelectionDAG &DAG,
4155 DebugLoc dl) {
4156 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4157 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4158 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4159
4160 enum {
4161 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4162 OP_VREV,
4163 OP_VDUP0,
4164 OP_VDUP1,
4165 OP_VDUP2,
4166 OP_VDUP3,
4167 OP_VEXT1,
4168 OP_VEXT2,
4169 OP_VEXT3,
4170 OP_VUZPL, // VUZP, left result
4171 OP_VUZPR, // VUZP, right result
4172 OP_VZIPL, // VZIP, left result
4173 OP_VZIPR, // VZIP, right result
4174 OP_VTRNL, // VTRN, left result
4175 OP_VTRNR // VTRN, right result
4176 };
4177
4178 if (OpNum == OP_COPY) {
4179 if (LHSID == (1*9+2)*9+3) return LHS;
4180 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4181 return RHS;
4182 }
4183
4184 SDValue OpLHS, OpRHS;
4185 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4186 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4187 EVT VT = OpLHS.getValueType();
4188
4189 switch (OpNum) {
4190 default: llvm_unreachable("Unknown shuffle opcode!");
4191 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004192 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004193 if (VT.getVectorElementType() == MVT::i32 ||
4194 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004195 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4196 // vrev <4 x i16> -> VREV32
4197 if (VT.getVectorElementType() == MVT::i16)
4198 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4199 // vrev <4 x i8> -> VREV16
4200 assert(VT.getVectorElementType() == MVT::i8);
4201 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004202 case OP_VDUP0:
4203 case OP_VDUP1:
4204 case OP_VDUP2:
4205 case OP_VDUP3:
4206 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004207 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004208 case OP_VEXT1:
4209 case OP_VEXT2:
4210 case OP_VEXT3:
4211 return DAG.getNode(ARMISD::VEXT, dl, VT,
4212 OpLHS, OpRHS,
4213 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4214 case OP_VUZPL:
4215 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004216 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004217 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4218 case OP_VZIPL:
4219 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004220 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004221 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4222 case OP_VTRNL:
4223 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004224 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4225 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004226 }
4227}
4228
Bill Wendling69a05a72011-03-14 23:02:38 +00004229static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4230 SmallVectorImpl<int> &ShuffleMask,
4231 SelectionDAG &DAG) {
4232 // Check to see if we can use the VTBL instruction.
4233 SDValue V1 = Op.getOperand(0);
4234 SDValue V2 = Op.getOperand(1);
4235 DebugLoc DL = Op.getDebugLoc();
4236
4237 SmallVector<SDValue, 8> VTBLMask;
4238 for (SmallVectorImpl<int>::iterator
4239 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4240 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4241
4242 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4243 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4244 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4245 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004246
Owen Anderson76706012011-04-05 21:48:57 +00004247 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004248 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4249 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004250}
4251
Bob Wilson5bafff32009-06-22 23:27:02 +00004252static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004253 SDValue V1 = Op.getOperand(0);
4254 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004255 DebugLoc dl = Op.getDebugLoc();
4256 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004257 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004258 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004259
Bob Wilson28865062009-08-13 02:13:04 +00004260 // Convert shuffles that are directly supported on NEON to target-specific
4261 // DAG nodes, instead of keeping them as shuffles and matching them again
4262 // during code selection. This is more efficient and avoids the possibility
4263 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004264 // FIXME: floating-point vectors should be canonicalized to integer vectors
4265 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004266 SVN->getMask(ShuffleMask);
4267
Bob Wilson53dd2452010-06-07 23:53:38 +00004268 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4269 if (EltSize <= 32) {
4270 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4271 int Lane = SVN->getSplatIndex();
4272 // If this is undef splat, generate it via "just" vdup, if possible.
4273 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004274
Bob Wilson53dd2452010-06-07 23:53:38 +00004275 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4276 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4277 }
4278 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4279 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004280 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004281
4282 bool ReverseVEXT;
4283 unsigned Imm;
4284 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4285 if (ReverseVEXT)
4286 std::swap(V1, V2);
4287 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4288 DAG.getConstant(Imm, MVT::i32));
4289 }
4290
4291 if (isVREVMask(ShuffleMask, VT, 64))
4292 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4293 if (isVREVMask(ShuffleMask, VT, 32))
4294 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4295 if (isVREVMask(ShuffleMask, VT, 16))
4296 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4297
4298 // Check for Neon shuffles that modify both input vectors in place.
4299 // If both results are used, i.e., if there are two shuffles with the same
4300 // source operands and with masks corresponding to both results of one of
4301 // these operations, DAG memoization will ensure that a single node is
4302 // used for both shuffles.
4303 unsigned WhichResult;
4304 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4305 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4306 V1, V2).getValue(WhichResult);
4307 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4308 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4309 V1, V2).getValue(WhichResult);
4310 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4311 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4312 V1, V2).getValue(WhichResult);
4313
4314 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4315 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4316 V1, V1).getValue(WhichResult);
4317 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4318 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4319 V1, V1).getValue(WhichResult);
4320 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4321 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4322 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004323 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004324
Bob Wilsonc692cb72009-08-21 20:54:19 +00004325 // If the shuffle is not directly supported and it has 4 elements, use
4326 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004327 unsigned NumElts = VT.getVectorNumElements();
4328 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004329 unsigned PFIndexes[4];
4330 for (unsigned i = 0; i != 4; ++i) {
4331 if (ShuffleMask[i] < 0)
4332 PFIndexes[i] = 8;
4333 else
4334 PFIndexes[i] = ShuffleMask[i];
4335 }
4336
4337 // Compute the index in the perfect shuffle table.
4338 unsigned PFTableIndex =
4339 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004340 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4341 unsigned Cost = (PFEntry >> 30);
4342
4343 if (Cost <= 4)
4344 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4345 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004346
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004347 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004348 if (EltSize >= 32) {
4349 // Do the expansion with floating-point types, since that is what the VFP
4350 // registers are defined to use, and since i64 is not legal.
4351 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4352 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004353 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4354 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004355 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004356 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004357 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004358 Ops.push_back(DAG.getUNDEF(EltVT));
4359 else
4360 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4361 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4362 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4363 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004364 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004365 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004366 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004367 }
4368
Bill Wendling69a05a72011-03-14 23:02:38 +00004369 if (VT == MVT::v8i8) {
4370 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4371 if (NewOp.getNode())
4372 return NewOp;
4373 }
4374
Bob Wilson22cac0d2009-08-14 05:16:33 +00004375 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004376}
4377
Bob Wilson5bafff32009-06-22 23:27:02 +00004378static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004379 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004380 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004381 if (!isa<ConstantSDNode>(Lane))
4382 return SDValue();
4383
4384 SDValue Vec = Op.getOperand(0);
4385 if (Op.getValueType() == MVT::i32 &&
4386 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4387 DebugLoc dl = Op.getDebugLoc();
4388 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4389 }
4390
4391 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004392}
4393
Bob Wilsona6d65862009-08-03 20:36:38 +00004394static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4395 // The only time a CONCAT_VECTORS operation can have legal types is when
4396 // two 64-bit vectors are concatenated to a 128-bit vector.
4397 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4398 "unexpected CONCAT_VECTORS");
4399 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004400 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004401 SDValue Op0 = Op.getOperand(0);
4402 SDValue Op1 = Op.getOperand(1);
4403 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004404 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004405 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004406 DAG.getIntPtrConstant(0));
4407 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004408 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004409 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004410 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004411 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004412}
4413
Bob Wilson626613d2010-11-23 19:38:38 +00004414/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4415/// element has been zero/sign-extended, depending on the isSigned parameter,
4416/// from an integer type half its size.
4417static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4418 bool isSigned) {
4419 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4420 EVT VT = N->getValueType(0);
4421 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4422 SDNode *BVN = N->getOperand(0).getNode();
4423 if (BVN->getValueType(0) != MVT::v4i32 ||
4424 BVN->getOpcode() != ISD::BUILD_VECTOR)
4425 return false;
4426 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4427 unsigned HiElt = 1 - LoElt;
4428 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4429 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4430 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4431 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4432 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4433 return false;
4434 if (isSigned) {
4435 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4436 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4437 return true;
4438 } else {
4439 if (Hi0->isNullValue() && Hi1->isNullValue())
4440 return true;
4441 }
4442 return false;
4443 }
4444
4445 if (N->getOpcode() != ISD::BUILD_VECTOR)
4446 return false;
4447
4448 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4449 SDNode *Elt = N->getOperand(i).getNode();
4450 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4451 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4452 unsigned HalfSize = EltSize / 2;
4453 if (isSigned) {
4454 int64_t SExtVal = C->getSExtValue();
4455 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4456 return false;
4457 } else {
4458 if ((C->getZExtValue() >> HalfSize) != 0)
4459 return false;
4460 }
4461 continue;
4462 }
4463 return false;
4464 }
4465
4466 return true;
4467}
4468
4469/// isSignExtended - Check if a node is a vector value that is sign-extended
4470/// or a constant BUILD_VECTOR with sign-extended elements.
4471static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4472 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4473 return true;
4474 if (isExtendedBUILD_VECTOR(N, DAG, true))
4475 return true;
4476 return false;
4477}
4478
4479/// isZeroExtended - Check if a node is a vector value that is zero-extended
4480/// or a constant BUILD_VECTOR with zero-extended elements.
4481static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4482 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4483 return true;
4484 if (isExtendedBUILD_VECTOR(N, DAG, false))
4485 return true;
4486 return false;
4487}
4488
4489/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4490/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004491static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4492 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4493 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004494 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4495 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4496 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4497 LD->isNonTemporal(), LD->getAlignment());
4498 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4499 // have been legalized as a BITCAST from v4i32.
4500 if (N->getOpcode() == ISD::BITCAST) {
4501 SDNode *BVN = N->getOperand(0).getNode();
4502 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4503 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4504 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4505 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4506 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4507 }
4508 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4509 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4510 EVT VT = N->getValueType(0);
4511 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4512 unsigned NumElts = VT.getVectorNumElements();
4513 MVT TruncVT = MVT::getIntegerVT(EltSize);
4514 SmallVector<SDValue, 8> Ops;
4515 for (unsigned i = 0; i != NumElts; ++i) {
4516 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4517 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004518 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004519 }
4520 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4521 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004522}
4523
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004524static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4525 unsigned Opcode = N->getOpcode();
4526 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4527 SDNode *N0 = N->getOperand(0).getNode();
4528 SDNode *N1 = N->getOperand(1).getNode();
4529 return N0->hasOneUse() && N1->hasOneUse() &&
4530 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4531 }
4532 return false;
4533}
4534
4535static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4536 unsigned Opcode = N->getOpcode();
4537 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4538 SDNode *N0 = N->getOperand(0).getNode();
4539 SDNode *N1 = N->getOperand(1).getNode();
4540 return N0->hasOneUse() && N1->hasOneUse() &&
4541 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4542 }
4543 return false;
4544}
4545
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004546static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4547 // Multiplications are only custom-lowered for 128-bit vectors so that
4548 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4549 EVT VT = Op.getValueType();
4550 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4551 SDNode *N0 = Op.getOperand(0).getNode();
4552 SDNode *N1 = Op.getOperand(1).getNode();
4553 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004554 bool isMLA = false;
4555 bool isN0SExt = isSignExtended(N0, DAG);
4556 bool isN1SExt = isSignExtended(N1, DAG);
4557 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004558 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004559 else {
4560 bool isN0ZExt = isZeroExtended(N0, DAG);
4561 bool isN1ZExt = isZeroExtended(N1, DAG);
4562 if (isN0ZExt && isN1ZExt)
4563 NewOpc = ARMISD::VMULLu;
4564 else if (isN1SExt || isN1ZExt) {
4565 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4566 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4567 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4568 NewOpc = ARMISD::VMULLs;
4569 isMLA = true;
4570 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4571 NewOpc = ARMISD::VMULLu;
4572 isMLA = true;
4573 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4574 std::swap(N0, N1);
4575 NewOpc = ARMISD::VMULLu;
4576 isMLA = true;
4577 }
4578 }
4579
4580 if (!NewOpc) {
4581 if (VT == MVT::v2i64)
4582 // Fall through to expand this. It is not legal.
4583 return SDValue();
4584 else
4585 // Other vector multiplications are legal.
4586 return Op;
4587 }
4588 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004589
4590 // Legalize to a VMULL instruction.
4591 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004592 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004593 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004594 if (!isMLA) {
4595 Op0 = SkipExtension(N0, DAG);
4596 assert(Op0.getValueType().is64BitVector() &&
4597 Op1.getValueType().is64BitVector() &&
4598 "unexpected types for extended operands to VMULL");
4599 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4600 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004601
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004602 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4603 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4604 // vmull q0, d4, d6
4605 // vmlal q0, d5, d6
4606 // is faster than
4607 // vaddl q0, d4, d5
4608 // vmovl q1, d6
4609 // vmul q0, q0, q1
4610 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4611 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4612 EVT Op1VT = Op1.getValueType();
4613 return DAG.getNode(N0->getOpcode(), DL, VT,
4614 DAG.getNode(NewOpc, DL, VT,
4615 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4616 DAG.getNode(NewOpc, DL, VT,
4617 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004618}
4619
Owen Anderson76706012011-04-05 21:48:57 +00004620static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004621LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4622 // Convert to float
4623 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4624 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4625 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4626 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4627 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4628 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4629 // Get reciprocal estimate.
4630 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004631 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004632 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4633 // Because char has a smaller range than uchar, we can actually get away
4634 // without any newton steps. This requires that we use a weird bias
4635 // of 0xb000, however (again, this has been exhaustively tested).
4636 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4637 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4638 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4639 Y = DAG.getConstant(0xb000, MVT::i32);
4640 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4641 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4642 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4643 // Convert back to short.
4644 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4645 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4646 return X;
4647}
4648
Owen Anderson76706012011-04-05 21:48:57 +00004649static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004650LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4651 SDValue N2;
4652 // Convert to float.
4653 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4654 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4655 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4656 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4657 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4658 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004659
Nate Begeman7973f352011-02-11 20:53:29 +00004660 // Use reciprocal estimate and one refinement step.
4661 // float4 recip = vrecpeq_f32(yf);
4662 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004663 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004664 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004665 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004666 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4667 N1, N2);
4668 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4669 // Because short has a smaller range than ushort, we can actually get away
4670 // with only a single newton step. This requires that we use a weird bias
4671 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004672 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004673 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4674 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004675 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004676 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4677 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4678 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4679 // Convert back to integer and return.
4680 // return vmovn_s32(vcvt_s32_f32(result));
4681 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4682 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4683 return N0;
4684}
4685
4686static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4687 EVT VT = Op.getValueType();
4688 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4689 "unexpected type for custom-lowering ISD::SDIV");
4690
4691 DebugLoc dl = Op.getDebugLoc();
4692 SDValue N0 = Op.getOperand(0);
4693 SDValue N1 = Op.getOperand(1);
4694 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004695
Nate Begeman7973f352011-02-11 20:53:29 +00004696 if (VT == MVT::v8i8) {
4697 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4698 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004699
Nate Begeman7973f352011-02-11 20:53:29 +00004700 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4701 DAG.getIntPtrConstant(4));
4702 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004703 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004704 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4705 DAG.getIntPtrConstant(0));
4706 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4707 DAG.getIntPtrConstant(0));
4708
4709 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4710 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4711
4712 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4713 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004714
Nate Begeman7973f352011-02-11 20:53:29 +00004715 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4716 return N0;
4717 }
4718 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4719}
4720
4721static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4722 EVT VT = Op.getValueType();
4723 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4724 "unexpected type for custom-lowering ISD::UDIV");
4725
4726 DebugLoc dl = Op.getDebugLoc();
4727 SDValue N0 = Op.getOperand(0);
4728 SDValue N1 = Op.getOperand(1);
4729 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004730
Nate Begeman7973f352011-02-11 20:53:29 +00004731 if (VT == MVT::v8i8) {
4732 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4733 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004734
Nate Begeman7973f352011-02-11 20:53:29 +00004735 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4736 DAG.getIntPtrConstant(4));
4737 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004738 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004739 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4740 DAG.getIntPtrConstant(0));
4741 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4742 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004743
Nate Begeman7973f352011-02-11 20:53:29 +00004744 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4745 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004746
Nate Begeman7973f352011-02-11 20:53:29 +00004747 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4748 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004749
4750 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004751 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4752 N0);
4753 return N0;
4754 }
Owen Anderson76706012011-04-05 21:48:57 +00004755
Nate Begeman7973f352011-02-11 20:53:29 +00004756 // v4i16 sdiv ... Convert to float.
4757 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4758 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4759 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4760 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4761 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004762 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004763
4764 // Use reciprocal estimate and two refinement steps.
4765 // float4 recip = vrecpeq_f32(yf);
4766 // recip *= vrecpsq_f32(yf, recip);
4767 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004768 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004769 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004770 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004771 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004772 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004773 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004774 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004775 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004776 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004777 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4778 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4779 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4780 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004781 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004782 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4783 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4784 N1 = DAG.getConstant(2, MVT::i32);
4785 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4786 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4787 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4788 // Convert back to integer and return.
4789 // return vmovn_u32(vcvt_s32_f32(result));
4790 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4791 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4792 return N0;
4793}
4794
Dan Gohmand858e902010-04-17 15:26:15 +00004795SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004796 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004797 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004798 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004799 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004800 case ISD::GlobalAddress:
4801 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4802 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004803 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004804 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004805 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4806 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004807 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004808 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004809 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004810 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004811 case ISD::SINT_TO_FP:
4812 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4813 case ISD::FP_TO_SINT:
4814 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004815 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004816 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004817 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004818 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004819 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004820 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004821 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004822 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4823 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004824 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004825 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004826 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004827 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004828 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004829 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004830 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004831 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004832 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004833 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004834 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004835 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004836 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004837 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004838 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004839 case ISD::SDIV: return LowerSDIV(Op, DAG);
4840 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004841 }
Dan Gohman475871a2008-07-27 21:46:04 +00004842 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004843}
4844
Duncan Sands1607f052008-12-01 11:39:25 +00004845/// ReplaceNodeResults - Replace the results of node with an illegal result
4846/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004847void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4848 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004849 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004850 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004851 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004852 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004853 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004854 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004855 case ISD::BITCAST:
4856 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004857 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004858 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004859 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004860 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004861 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004862 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004863 if (Res.getNode())
4864 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004865}
Chris Lattner27a6c732007-11-24 07:07:01 +00004866
Evan Chenga8e29892007-01-19 07:51:42 +00004867//===----------------------------------------------------------------------===//
4868// ARM Scheduler Hooks
4869//===----------------------------------------------------------------------===//
4870
4871MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004872ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4873 MachineBasicBlock *BB,
4874 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004875 unsigned dest = MI->getOperand(0).getReg();
4876 unsigned ptr = MI->getOperand(1).getReg();
4877 unsigned oldval = MI->getOperand(2).getReg();
4878 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004879 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4880 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004881 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004882
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004883 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4884 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00004885 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004886 : ARM::GPRRegisterClass);
4887
4888 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00004889 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
4890 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
4891 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00004892 }
4893
Jim Grosbach5278eb82009-12-11 01:42:04 +00004894 unsigned ldrOpc, strOpc;
4895 switch (Size) {
4896 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004897 case 1:
4898 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004899 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004900 break;
4901 case 2:
4902 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4903 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4904 break;
4905 case 4:
4906 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4907 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4908 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004909 }
4910
4911 MachineFunction *MF = BB->getParent();
4912 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4913 MachineFunction::iterator It = BB;
4914 ++It; // insert the new blocks after the current block
4915
4916 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4917 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4918 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4919 MF->insert(It, loop1MBB);
4920 MF->insert(It, loop2MBB);
4921 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004922
4923 // Transfer the remainder of BB and its successor edges to exitMBB.
4924 exitMBB->splice(exitMBB->begin(), BB,
4925 llvm::next(MachineBasicBlock::iterator(MI)),
4926 BB->end());
4927 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004928
4929 // thisMBB:
4930 // ...
4931 // fallthrough --> loop1MBB
4932 BB->addSuccessor(loop1MBB);
4933
4934 // loop1MBB:
4935 // ldrex dest, [ptr]
4936 // cmp dest, oldval
4937 // bne exitMBB
4938 BB = loop1MBB;
4939 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004940 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004941 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004942 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4943 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004944 BB->addSuccessor(loop2MBB);
4945 BB->addSuccessor(exitMBB);
4946
4947 // loop2MBB:
4948 // strex scratch, newval, [ptr]
4949 // cmp scratch, #0
4950 // bne loop1MBB
4951 BB = loop2MBB;
4952 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4953 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004954 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004955 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004956 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4957 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004958 BB->addSuccessor(loop1MBB);
4959 BB->addSuccessor(exitMBB);
4960
4961 // exitMBB:
4962 // ...
4963 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004964
Dan Gohman14152b42010-07-06 20:24:04 +00004965 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004966
Jim Grosbach5278eb82009-12-11 01:42:04 +00004967 return BB;
4968}
4969
4970MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004971ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4972 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004973 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4974 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4975
4976 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004977 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004978 MachineFunction::iterator It = BB;
4979 ++It;
4980
4981 unsigned dest = MI->getOperand(0).getReg();
4982 unsigned ptr = MI->getOperand(1).getReg();
4983 unsigned incr = MI->getOperand(2).getReg();
4984 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004985
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004986 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004987 unsigned ldrOpc, strOpc;
4988 switch (Size) {
4989 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004990 case 1:
4991 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004992 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004993 break;
4994 case 2:
4995 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4996 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4997 break;
4998 case 4:
4999 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5000 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5001 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005002 }
5003
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005004 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5005 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5006 MF->insert(It, loopMBB);
5007 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005008
5009 // Transfer the remainder of BB and its successor edges to exitMBB.
5010 exitMBB->splice(exitMBB->begin(), BB,
5011 llvm::next(MachineBasicBlock::iterator(MI)),
5012 BB->end());
5013 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005014
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005015 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005016 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5017 unsigned scratch2 = (!BinOpcode) ? incr :
5018 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5019
5020 // thisMBB:
5021 // ...
5022 // fallthrough --> loopMBB
5023 BB->addSuccessor(loopMBB);
5024
5025 // loopMBB:
5026 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005027 // <binop> scratch2, dest, incr
5028 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005029 // cmp scratch, #0
5030 // bne- loopMBB
5031 // fallthrough --> exitMBB
5032 BB = loopMBB;
5033 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00005034 if (BinOpcode) {
5035 // operand order needs to go the other way for NAND
5036 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5037 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5038 addReg(incr).addReg(dest)).addReg(0);
5039 else
5040 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5041 addReg(dest).addReg(incr)).addReg(0);
5042 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005043
5044 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5045 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005046 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005047 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005048 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5049 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005050
5051 BB->addSuccessor(loopMBB);
5052 BB->addSuccessor(exitMBB);
5053
5054 // exitMBB:
5055 // ...
5056 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005057
Dan Gohman14152b42010-07-06 20:24:04 +00005058 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005059
Jim Grosbachc3c23542009-12-14 04:22:04 +00005060 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005061}
5062
Jim Grosbachf7da8822011-04-26 19:44:18 +00005063MachineBasicBlock *
5064ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5065 MachineBasicBlock *BB,
5066 unsigned Size,
5067 bool signExtend,
5068 ARMCC::CondCodes Cond) const {
5069 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5070
5071 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5072 MachineFunction *MF = BB->getParent();
5073 MachineFunction::iterator It = BB;
5074 ++It;
5075
5076 unsigned dest = MI->getOperand(0).getReg();
5077 unsigned ptr = MI->getOperand(1).getReg();
5078 unsigned incr = MI->getOperand(2).getReg();
5079 unsigned oldval = dest;
5080 DebugLoc dl = MI->getDebugLoc();
5081
5082 bool isThumb2 = Subtarget->isThumb2();
5083 unsigned ldrOpc, strOpc, extendOpc;
5084 switch (Size) {
5085 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5086 case 1:
5087 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5088 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
5089 extendOpc = isThumb2 ? ARM::t2SXTBr : ARM::SXTBr;
5090 break;
5091 case 2:
5092 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5093 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5094 extendOpc = isThumb2 ? ARM::t2SXTHr : ARM::SXTHr;
5095 break;
5096 case 4:
5097 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5098 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5099 extendOpc = 0;
5100 break;
5101 }
5102
5103 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5104 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5105 MF->insert(It, loopMBB);
5106 MF->insert(It, exitMBB);
5107
5108 // Transfer the remainder of BB and its successor edges to exitMBB.
5109 exitMBB->splice(exitMBB->begin(), BB,
5110 llvm::next(MachineBasicBlock::iterator(MI)),
5111 BB->end());
5112 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5113
5114 MachineRegisterInfo &RegInfo = MF->getRegInfo();
5115 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5116 unsigned scratch2 = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5117
5118 // thisMBB:
5119 // ...
5120 // fallthrough --> loopMBB
5121 BB->addSuccessor(loopMBB);
5122
5123 // loopMBB:
5124 // ldrex dest, ptr
5125 // (sign extend dest, if required)
5126 // cmp dest, incr
5127 // cmov.cond scratch2, dest, incr
5128 // strex scratch, scratch2, ptr
5129 // cmp scratch, #0
5130 // bne- loopMBB
5131 // fallthrough --> exitMBB
5132 BB = loopMBB;
5133 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
5134
5135 // Sign extend the value, if necessary.
5136 if (signExtend && extendOpc) {
5137 oldval = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
5138 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval).addReg(dest));
5139 }
5140
5141 // Build compare and cmov instructions.
5142 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5143 .addReg(oldval).addReg(incr));
5144 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5145 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5146
5147 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
5148 .addReg(ptr));
5149 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5150 .addReg(scratch).addImm(0));
5151 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5152 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5153
5154 BB->addSuccessor(loopMBB);
5155 BB->addSuccessor(exitMBB);
5156
5157 // exitMBB:
5158 // ...
5159 BB = exitMBB;
5160
5161 MI->eraseFromParent(); // The instruction is gone now.
5162
5163 return BB;
5164}
5165
Evan Cheng218977b2010-07-13 19:27:42 +00005166static
5167MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5168 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5169 E = MBB->succ_end(); I != E; ++I)
5170 if (*I != Succ)
5171 return *I;
5172 llvm_unreachable("Expecting a BB with two successors!");
5173}
5174
Andrew Trick1c3af772011-04-23 03:55:32 +00005175// FIXME: This opcode table should obviously be expressed in the target
5176// description. We probably just need a "machine opcode" value in the pseudo
5177// instruction. But the ideal solution maybe to simply remove the "S" version
5178// of the opcode altogether.
5179struct AddSubFlagsOpcodePair {
5180 unsigned PseudoOpc;
5181 unsigned MachineOpc;
5182};
5183
5184static AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
5185 {ARM::ADCSri, ARM::ADCri},
5186 {ARM::ADCSrr, ARM::ADCrr},
5187 {ARM::ADCSrs, ARM::ADCrs},
5188 {ARM::SBCSri, ARM::SBCri},
5189 {ARM::SBCSrr, ARM::SBCrr},
5190 {ARM::SBCSrs, ARM::SBCrs},
5191 {ARM::RSBSri, ARM::RSBri},
5192 {ARM::RSBSrr, ARM::RSBrr},
5193 {ARM::RSBSrs, ARM::RSBrs},
5194 {ARM::RSCSri, ARM::RSCri},
5195 {ARM::RSCSrs, ARM::RSCrs},
5196 {ARM::t2ADCSri, ARM::t2ADCri},
5197 {ARM::t2ADCSrr, ARM::t2ADCrr},
5198 {ARM::t2ADCSrs, ARM::t2ADCrs},
5199 {ARM::t2SBCSri, ARM::t2SBCri},
5200 {ARM::t2SBCSrr, ARM::t2SBCrr},
5201 {ARM::t2SBCSrs, ARM::t2SBCrs},
5202 {ARM::t2RSBSri, ARM::t2RSBri},
5203 {ARM::t2RSBSrs, ARM::t2RSBrs},
5204};
5205
5206// Convert and Add or Subtract with Carry and Flags to a generic opcode with
5207// CPSR<def> operand. e.g. ADCS (...) -> ADC (... CPSR<def>).
5208//
5209// FIXME: Somewhere we should assert that CPSR<def> is in the correct
5210// position to be recognized by the target descrition as the 'S' bit.
5211bool ARMTargetLowering::RemapAddSubWithFlags(MachineInstr *MI,
5212 MachineBasicBlock *BB) const {
5213 unsigned OldOpc = MI->getOpcode();
5214 unsigned NewOpc = 0;
5215
5216 // This is only called for instructions that need remapping, so iterating over
5217 // the tiny opcode table is not costly.
5218 static const int NPairs =
5219 sizeof(AddSubFlagsOpcodeMap) / sizeof(AddSubFlagsOpcodePair);
5220 for (AddSubFlagsOpcodePair *Pair = &AddSubFlagsOpcodeMap[0],
5221 *End = &AddSubFlagsOpcodeMap[NPairs]; Pair != End; ++Pair) {
5222 if (OldOpc == Pair->PseudoOpc) {
5223 NewOpc = Pair->MachineOpc;
5224 break;
5225 }
5226 }
5227 if (!NewOpc)
5228 return false;
5229
5230 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5231 DebugLoc dl = MI->getDebugLoc();
5232 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5233 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5234 MIB.addOperand(MI->getOperand(i));
5235 AddDefaultPred(MIB);
5236 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5237 MI->eraseFromParent();
5238 return true;
5239}
5240
Jim Grosbache801dc42009-12-12 01:40:06 +00005241MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005242ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005243 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005244 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005245 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005246 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005247 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005248 default: {
5249 if (RemapAddSubWithFlags(MI, BB))
5250 return BB;
5251
Jim Grosbach5278eb82009-12-11 01:42:04 +00005252 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005253 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005254 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005255 case ARM::ATOMIC_LOAD_ADD_I8:
5256 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5257 case ARM::ATOMIC_LOAD_ADD_I16:
5258 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5259 case ARM::ATOMIC_LOAD_ADD_I32:
5260 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005261
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005262 case ARM::ATOMIC_LOAD_AND_I8:
5263 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5264 case ARM::ATOMIC_LOAD_AND_I16:
5265 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5266 case ARM::ATOMIC_LOAD_AND_I32:
5267 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005268
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005269 case ARM::ATOMIC_LOAD_OR_I8:
5270 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5271 case ARM::ATOMIC_LOAD_OR_I16:
5272 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5273 case ARM::ATOMIC_LOAD_OR_I32:
5274 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005275
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005276 case ARM::ATOMIC_LOAD_XOR_I8:
5277 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5278 case ARM::ATOMIC_LOAD_XOR_I16:
5279 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5280 case ARM::ATOMIC_LOAD_XOR_I32:
5281 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005282
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005283 case ARM::ATOMIC_LOAD_NAND_I8:
5284 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5285 case ARM::ATOMIC_LOAD_NAND_I16:
5286 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5287 case ARM::ATOMIC_LOAD_NAND_I32:
5288 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005289
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 case ARM::ATOMIC_LOAD_SUB_I8:
5291 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5292 case ARM::ATOMIC_LOAD_SUB_I16:
5293 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5294 case ARM::ATOMIC_LOAD_SUB_I32:
5295 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005296
Jim Grosbachf7da8822011-04-26 19:44:18 +00005297 case ARM::ATOMIC_LOAD_MIN_I8:
5298 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5299 case ARM::ATOMIC_LOAD_MIN_I16:
5300 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5301 case ARM::ATOMIC_LOAD_MIN_I32:
5302 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5303
5304 case ARM::ATOMIC_LOAD_MAX_I8:
5305 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5306 case ARM::ATOMIC_LOAD_MAX_I16:
5307 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5308 case ARM::ATOMIC_LOAD_MAX_I32:
5309 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5310
5311 case ARM::ATOMIC_LOAD_UMIN_I8:
5312 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5313 case ARM::ATOMIC_LOAD_UMIN_I16:
5314 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5315 case ARM::ATOMIC_LOAD_UMIN_I32:
5316 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5317
5318 case ARM::ATOMIC_LOAD_UMAX_I8:
5319 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5320 case ARM::ATOMIC_LOAD_UMAX_I16:
5321 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5322 case ARM::ATOMIC_LOAD_UMAX_I32:
5323 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5324
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005325 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5326 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5327 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005328
5329 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5330 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5331 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005332
Evan Cheng007ea272009-08-12 05:17:19 +00005333 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005334 // To "insert" a SELECT_CC instruction, we actually have to insert the
5335 // diamond control-flow pattern. The incoming instruction knows the
5336 // destination vreg to set, the condition code register to branch on, the
5337 // true/false values to select between, and a branch opcode to use.
5338 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005339 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005340 ++It;
5341
5342 // thisMBB:
5343 // ...
5344 // TrueVal = ...
5345 // cmpTY ccX, r1, r2
5346 // bCC copy1MBB
5347 // fallthrough --> copy0MBB
5348 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005349 MachineFunction *F = BB->getParent();
5350 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5351 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005352 F->insert(It, copy0MBB);
5353 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005354
5355 // Transfer the remainder of BB and its successor edges to sinkMBB.
5356 sinkMBB->splice(sinkMBB->begin(), BB,
5357 llvm::next(MachineBasicBlock::iterator(MI)),
5358 BB->end());
5359 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5360
Dan Gohman258c58c2010-07-06 15:49:48 +00005361 BB->addSuccessor(copy0MBB);
5362 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005363
Dan Gohman14152b42010-07-06 20:24:04 +00005364 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5365 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5366
Evan Chenga8e29892007-01-19 07:51:42 +00005367 // copy0MBB:
5368 // %FalseValue = ...
5369 // # fallthrough to sinkMBB
5370 BB = copy0MBB;
5371
5372 // Update machine-CFG edges
5373 BB->addSuccessor(sinkMBB);
5374
5375 // sinkMBB:
5376 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5377 // ...
5378 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005379 BuildMI(*BB, BB->begin(), dl,
5380 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005381 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5382 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5383
Dan Gohman14152b42010-07-06 20:24:04 +00005384 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005385 return BB;
5386 }
Evan Cheng86198642009-08-07 00:34:42 +00005387
Evan Cheng218977b2010-07-13 19:27:42 +00005388 case ARM::BCCi64:
5389 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005390 // If there is an unconditional branch to the other successor, remove it.
5391 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005392
Evan Cheng218977b2010-07-13 19:27:42 +00005393 // Compare both parts that make up the double comparison separately for
5394 // equality.
5395 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5396
5397 unsigned LHS1 = MI->getOperand(1).getReg();
5398 unsigned LHS2 = MI->getOperand(2).getReg();
5399 if (RHSisZero) {
5400 AddDefaultPred(BuildMI(BB, dl,
5401 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5402 .addReg(LHS1).addImm(0));
5403 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5404 .addReg(LHS2).addImm(0)
5405 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5406 } else {
5407 unsigned RHS1 = MI->getOperand(3).getReg();
5408 unsigned RHS2 = MI->getOperand(4).getReg();
5409 AddDefaultPred(BuildMI(BB, dl,
5410 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5411 .addReg(LHS1).addReg(RHS1));
5412 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5413 .addReg(LHS2).addReg(RHS2)
5414 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5415 }
5416
5417 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5418 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5419 if (MI->getOperand(0).getImm() == ARMCC::NE)
5420 std::swap(destMBB, exitMBB);
5421
5422 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5423 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5424 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5425 .addMBB(exitMBB);
5426
5427 MI->eraseFromParent(); // The pseudo instruction is gone now.
5428 return BB;
5429 }
Evan Chenga8e29892007-01-19 07:51:42 +00005430 }
5431}
5432
5433//===----------------------------------------------------------------------===//
5434// ARM Optimization Hooks
5435//===----------------------------------------------------------------------===//
5436
Chris Lattnerd1980a52009-03-12 06:52:53 +00005437static
5438SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5439 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005440 SelectionDAG &DAG = DCI.DAG;
5441 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005442 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005443 unsigned Opc = N->getOpcode();
5444 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5445 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5446 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5447 ISD::CondCode CC = ISD::SETCC_INVALID;
5448
5449 if (isSlctCC) {
5450 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5451 } else {
5452 SDValue CCOp = Slct.getOperand(0);
5453 if (CCOp.getOpcode() == ISD::SETCC)
5454 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5455 }
5456
5457 bool DoXform = false;
5458 bool InvCC = false;
5459 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5460 "Bad input!");
5461
5462 if (LHS.getOpcode() == ISD::Constant &&
5463 cast<ConstantSDNode>(LHS)->isNullValue()) {
5464 DoXform = true;
5465 } else if (CC != ISD::SETCC_INVALID &&
5466 RHS.getOpcode() == ISD::Constant &&
5467 cast<ConstantSDNode>(RHS)->isNullValue()) {
5468 std::swap(LHS, RHS);
5469 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005470 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005471 Op0.getOperand(0).getValueType();
5472 bool isInt = OpVT.isInteger();
5473 CC = ISD::getSetCCInverse(CC, isInt);
5474
5475 if (!TLI.isCondCodeLegal(CC, OpVT))
5476 return SDValue(); // Inverse operator isn't legal.
5477
5478 DoXform = true;
5479 InvCC = true;
5480 }
5481
5482 if (DoXform) {
5483 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5484 if (isSlctCC)
5485 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5486 Slct.getOperand(0), Slct.getOperand(1), CC);
5487 SDValue CCOp = Slct.getOperand(0);
5488 if (InvCC)
5489 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5490 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5491 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5492 CCOp, OtherOp, Result);
5493 }
5494 return SDValue();
5495}
5496
Bob Wilson3d5792a2010-07-29 20:34:14 +00005497/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5498/// operands N0 and N1. This is a helper for PerformADDCombine that is
5499/// called with the default operands, and if that fails, with commuted
5500/// operands.
5501static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5502 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005503 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5504 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5505 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5506 if (Result.getNode()) return Result;
5507 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005508 return SDValue();
5509}
5510
Bob Wilson3d5792a2010-07-29 20:34:14 +00005511/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5512///
5513static SDValue PerformADDCombine(SDNode *N,
5514 TargetLowering::DAGCombinerInfo &DCI) {
5515 SDValue N0 = N->getOperand(0);
5516 SDValue N1 = N->getOperand(1);
5517
5518 // First try with the default operand order.
5519 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5520 if (Result.getNode())
5521 return Result;
5522
5523 // If that didn't work, try again with the operands commuted.
5524 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5525}
5526
Chris Lattnerd1980a52009-03-12 06:52:53 +00005527/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005528///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005529static SDValue PerformSUBCombine(SDNode *N,
5530 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005531 SDValue N0 = N->getOperand(0);
5532 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005533
Chris Lattnerd1980a52009-03-12 06:52:53 +00005534 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5535 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5536 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5537 if (Result.getNode()) return Result;
5538 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005539
Chris Lattnerd1980a52009-03-12 06:52:53 +00005540 return SDValue();
5541}
5542
Evan Cheng463d3582011-03-31 19:38:48 +00005543/// PerformVMULCombine
5544/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5545/// special multiplier accumulator forwarding.
5546/// vmul d3, d0, d2
5547/// vmla d3, d1, d2
5548/// is faster than
5549/// vadd d3, d0, d1
5550/// vmul d3, d3, d2
5551static SDValue PerformVMULCombine(SDNode *N,
5552 TargetLowering::DAGCombinerInfo &DCI,
5553 const ARMSubtarget *Subtarget) {
5554 if (!Subtarget->hasVMLxForwarding())
5555 return SDValue();
5556
5557 SelectionDAG &DAG = DCI.DAG;
5558 SDValue N0 = N->getOperand(0);
5559 SDValue N1 = N->getOperand(1);
5560 unsigned Opcode = N0.getOpcode();
5561 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5562 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5563 Opcode = N0.getOpcode();
5564 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5565 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5566 return SDValue();
5567 std::swap(N0, N1);
5568 }
5569
5570 EVT VT = N->getValueType(0);
5571 DebugLoc DL = N->getDebugLoc();
5572 SDValue N00 = N0->getOperand(0);
5573 SDValue N01 = N0->getOperand(1);
5574 return DAG.getNode(Opcode, DL, VT,
5575 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5576 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5577}
5578
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005579static SDValue PerformMULCombine(SDNode *N,
5580 TargetLowering::DAGCombinerInfo &DCI,
5581 const ARMSubtarget *Subtarget) {
5582 SelectionDAG &DAG = DCI.DAG;
5583
5584 if (Subtarget->isThumb1Only())
5585 return SDValue();
5586
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005587 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5588 return SDValue();
5589
5590 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005591 if (VT.is64BitVector() || VT.is128BitVector())
5592 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005593 if (VT != MVT::i32)
5594 return SDValue();
5595
5596 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5597 if (!C)
5598 return SDValue();
5599
5600 uint64_t MulAmt = C->getZExtValue();
5601 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5602 ShiftAmt = ShiftAmt & (32 - 1);
5603 SDValue V = N->getOperand(0);
5604 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005605
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005606 SDValue Res;
5607 MulAmt >>= ShiftAmt;
5608 if (isPowerOf2_32(MulAmt - 1)) {
5609 // (mul x, 2^N + 1) => (add (shl x, N), x)
5610 Res = DAG.getNode(ISD::ADD, DL, VT,
5611 V, DAG.getNode(ISD::SHL, DL, VT,
5612 V, DAG.getConstant(Log2_32(MulAmt-1),
5613 MVT::i32)));
5614 } else if (isPowerOf2_32(MulAmt + 1)) {
5615 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5616 Res = DAG.getNode(ISD::SUB, DL, VT,
5617 DAG.getNode(ISD::SHL, DL, VT,
5618 V, DAG.getConstant(Log2_32(MulAmt+1),
5619 MVT::i32)),
5620 V);
5621 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005622 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005623
5624 if (ShiftAmt != 0)
5625 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5626 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005627
5628 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005629 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005630 return SDValue();
5631}
5632
Owen Anderson080c0922010-11-05 19:27:46 +00005633static SDValue PerformANDCombine(SDNode *N,
5634 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005635
Owen Anderson080c0922010-11-05 19:27:46 +00005636 // Attempt to use immediate-form VBIC
5637 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5638 DebugLoc dl = N->getDebugLoc();
5639 EVT VT = N->getValueType(0);
5640 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005641
Tanya Lattner0433b212011-04-07 15:24:20 +00005642 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5643 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005644
Owen Anderson080c0922010-11-05 19:27:46 +00005645 APInt SplatBits, SplatUndef;
5646 unsigned SplatBitSize;
5647 bool HasAnyUndefs;
5648 if (BVN &&
5649 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5650 if (SplatBitSize <= 64) {
5651 EVT VbicVT;
5652 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5653 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005654 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005655 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005656 if (Val.getNode()) {
5657 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005658 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005659 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005660 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005661 }
5662 }
5663 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005664
Owen Anderson080c0922010-11-05 19:27:46 +00005665 return SDValue();
5666}
5667
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005668/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5669static SDValue PerformORCombine(SDNode *N,
5670 TargetLowering::DAGCombinerInfo &DCI,
5671 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005672 // Attempt to use immediate-form VORR
5673 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5674 DebugLoc dl = N->getDebugLoc();
5675 EVT VT = N->getValueType(0);
5676 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005677
Tanya Lattner0433b212011-04-07 15:24:20 +00005678 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5679 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00005680
Owen Anderson60f48702010-11-03 23:15:26 +00005681 APInt SplatBits, SplatUndef;
5682 unsigned SplatBitSize;
5683 bool HasAnyUndefs;
5684 if (BVN && Subtarget->hasNEON() &&
5685 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5686 if (SplatBitSize <= 64) {
5687 EVT VorrVT;
5688 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5689 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005690 DAG, VorrVT, VT.is128BitVector(),
5691 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005692 if (Val.getNode()) {
5693 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005694 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005695 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005696 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005697 }
5698 }
5699 }
5700
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005701 SDValue N0 = N->getOperand(0);
5702 if (N0.getOpcode() != ISD::AND)
5703 return SDValue();
5704 SDValue N1 = N->getOperand(1);
5705
5706 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5707 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5708 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5709 APInt SplatUndef;
5710 unsigned SplatBitSize;
5711 bool HasAnyUndefs;
5712
5713 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5714 APInt SplatBits0;
5715 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5716 HasAnyUndefs) && !HasAnyUndefs) {
5717 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5718 APInt SplatBits1;
5719 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5720 HasAnyUndefs) && !HasAnyUndefs &&
5721 SplatBits0 == ~SplatBits1) {
5722 // Canonicalize the vector type to make instruction selection simpler.
5723 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5724 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5725 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00005726 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005727 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5728 }
5729 }
5730 }
5731
Jim Grosbach54238562010-07-17 03:30:54 +00005732 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5733 // reasonable.
5734
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005735 // BFI is only available on V6T2+
5736 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5737 return SDValue();
5738
Jim Grosbach54238562010-07-17 03:30:54 +00005739 DebugLoc DL = N->getDebugLoc();
5740 // 1) or (and A, mask), val => ARMbfi A, val, mask
5741 // iff (val & mask) == val
5742 //
5743 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5744 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005745 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005746 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005747 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005748 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005749
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005750 if (VT != MVT::i32)
5751 return SDValue();
5752
Evan Cheng30fb13f2010-12-13 20:32:54 +00005753 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005754
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005755 // The value and the mask need to be constants so we can verify this is
5756 // actually a bitfield set. If the mask is 0xffff, we can do better
5757 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005758 SDValue MaskOp = N0.getOperand(1);
5759 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5760 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005761 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005762 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005763 if (Mask == 0xffff)
5764 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005765 SDValue Res;
5766 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005767 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5768 if (N1C) {
5769 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005770 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005771 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005772
Evan Chenga9688c42010-12-11 04:11:38 +00005773 if (ARM::isBitFieldInvertedMask(Mask)) {
5774 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005775
Evan Cheng30fb13f2010-12-13 20:32:54 +00005776 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005777 DAG.getConstant(Val, MVT::i32),
5778 DAG.getConstant(Mask, MVT::i32));
5779
5780 // Do not add new nodes to DAG combiner worklist.
5781 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005782 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005783 }
Jim Grosbach54238562010-07-17 03:30:54 +00005784 } else if (N1.getOpcode() == ISD::AND) {
5785 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005786 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5787 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005788 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005789 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005790
Eric Christopher29aeed12011-03-26 01:21:03 +00005791 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5792 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005793 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005794 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005795 // The pack halfword instruction works better for masks that fit it,
5796 // so use that when it's available.
5797 if (Subtarget->hasT2ExtractPack() &&
5798 (Mask == 0xffff || Mask == 0xffff0000))
5799 return SDValue();
5800 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005801 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005802 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005803 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005804 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005805 DAG.getConstant(Mask, MVT::i32));
5806 // Do not add new nodes to DAG combiner worklist.
5807 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005808 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005809 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005810 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005811 // The pack halfword instruction works better for masks that fit it,
5812 // so use that when it's available.
5813 if (Subtarget->hasT2ExtractPack() &&
5814 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5815 return SDValue();
5816 // 2b
5817 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005818 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005819 DAG.getConstant(lsb, MVT::i32));
5820 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005821 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005822 // Do not add new nodes to DAG combiner worklist.
5823 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005824 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005825 }
5826 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005827
Evan Cheng30fb13f2010-12-13 20:32:54 +00005828 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5829 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5830 ARM::isBitFieldInvertedMask(~Mask)) {
5831 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5832 // where lsb(mask) == #shamt and masked bits of B are known zero.
5833 SDValue ShAmt = N00.getOperand(1);
5834 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5835 unsigned LSB = CountTrailingZeros_32(Mask);
5836 if (ShAmtC != LSB)
5837 return SDValue();
5838
5839 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5840 DAG.getConstant(~Mask, MVT::i32));
5841
5842 // Do not add new nodes to DAG combiner worklist.
5843 DCI.CombineTo(N, Res, false);
5844 }
5845
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005846 return SDValue();
5847}
5848
Evan Cheng0c1aec12010-12-14 03:22:07 +00005849/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5850/// C1 & C2 == C1.
5851static SDValue PerformBFICombine(SDNode *N,
5852 TargetLowering::DAGCombinerInfo &DCI) {
5853 SDValue N1 = N->getOperand(1);
5854 if (N1.getOpcode() == ISD::AND) {
5855 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5856 if (!N11C)
5857 return SDValue();
5858 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5859 unsigned Mask2 = N11C->getZExtValue();
5860 if ((Mask & Mask2) == Mask2)
5861 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5862 N->getOperand(0), N1.getOperand(0),
5863 N->getOperand(2));
5864 }
5865 return SDValue();
5866}
5867
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005868/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5869/// ARMISD::VMOVRRD.
5870static SDValue PerformVMOVRRDCombine(SDNode *N,
5871 TargetLowering::DAGCombinerInfo &DCI) {
5872 // vmovrrd(vmovdrr x, y) -> x,y
5873 SDValue InDouble = N->getOperand(0);
5874 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5875 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00005876
5877 // vmovrrd(load f64) -> (load i32), (load i32)
5878 SDNode *InNode = InDouble.getNode();
5879 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
5880 InNode->getValueType(0) == MVT::f64 &&
5881 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
5882 !cast<LoadSDNode>(InNode)->isVolatile()) {
5883 // TODO: Should this be done for non-FrameIndex operands?
5884 LoadSDNode *LD = cast<LoadSDNode>(InNode);
5885
5886 SelectionDAG &DAG = DCI.DAG;
5887 DebugLoc DL = LD->getDebugLoc();
5888 SDValue BasePtr = LD->getBasePtr();
5889 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
5890 LD->getPointerInfo(), LD->isVolatile(),
5891 LD->isNonTemporal(), LD->getAlignment());
5892
5893 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5894 DAG.getConstant(4, MVT::i32));
5895 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
5896 LD->getPointerInfo(), LD->isVolatile(),
5897 LD->isNonTemporal(),
5898 std::min(4U, LD->getAlignment() / 2));
5899
5900 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
5901 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
5902 DCI.RemoveFromWorklist(LD);
5903 DAG.DeleteNode(LD);
5904 return Result;
5905 }
5906
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005907 return SDValue();
5908}
5909
5910/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5911/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5912static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5913 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5914 SDValue Op0 = N->getOperand(0);
5915 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005916 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005917 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005918 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005919 Op1 = Op1.getOperand(0);
5920 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5921 Op0.getNode() == Op1.getNode() &&
5922 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005923 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005924 N->getValueType(0), Op0.getOperand(0));
5925 return SDValue();
5926}
5927
Bob Wilson31600902010-12-21 06:43:19 +00005928/// PerformSTORECombine - Target-specific dag combine xforms for
5929/// ISD::STORE.
5930static SDValue PerformSTORECombine(SDNode *N,
5931 TargetLowering::DAGCombinerInfo &DCI) {
5932 // Bitcast an i64 store extracted from a vector to f64.
5933 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5934 StoreSDNode *St = cast<StoreSDNode>(N);
5935 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00005936 if (!ISD::isNormalStore(St) || St->isVolatile())
5937 return SDValue();
5938
5939 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
5940 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
5941 SelectionDAG &DAG = DCI.DAG;
5942 DebugLoc DL = St->getDebugLoc();
5943 SDValue BasePtr = St->getBasePtr();
5944 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
5945 StVal.getNode()->getOperand(0), BasePtr,
5946 St->getPointerInfo(), St->isVolatile(),
5947 St->isNonTemporal(), St->getAlignment());
5948
5949 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5950 DAG.getConstant(4, MVT::i32));
5951 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
5952 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
5953 St->isNonTemporal(),
5954 std::min(4U, St->getAlignment() / 2));
5955 }
5956
5957 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00005958 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5959 return SDValue();
5960
5961 SelectionDAG &DAG = DCI.DAG;
5962 DebugLoc dl = StVal.getDebugLoc();
5963 SDValue IntVec = StVal.getOperand(0);
5964 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5965 IntVec.getValueType().getVectorNumElements());
5966 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5967 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5968 Vec, StVal.getOperand(1));
5969 dl = N->getDebugLoc();
5970 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5971 // Make the DAGCombiner fold the bitcasts.
5972 DCI.AddToWorklist(Vec.getNode());
5973 DCI.AddToWorklist(ExtElt.getNode());
5974 DCI.AddToWorklist(V.getNode());
5975 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5976 St->getPointerInfo(), St->isVolatile(),
5977 St->isNonTemporal(), St->getAlignment(),
5978 St->getTBAAInfo());
5979}
5980
5981/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5982/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5983/// i64 vector to have f64 elements, since the value can then be loaded
5984/// directly into a VFP register.
5985static bool hasNormalLoadOperand(SDNode *N) {
5986 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5987 for (unsigned i = 0; i < NumElts; ++i) {
5988 SDNode *Elt = N->getOperand(i).getNode();
5989 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5990 return true;
5991 }
5992 return false;
5993}
5994
Bob Wilson75f02882010-09-17 22:59:05 +00005995/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5996/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005997static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5998 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005999 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6000 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6001 // into a pair of GPRs, which is fine when the value is used as a scalar,
6002 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006003 SelectionDAG &DAG = DCI.DAG;
6004 if (N->getNumOperands() == 2) {
6005 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6006 if (RV.getNode())
6007 return RV;
6008 }
Bob Wilson75f02882010-09-17 22:59:05 +00006009
Bob Wilson31600902010-12-21 06:43:19 +00006010 // Load i64 elements as f64 values so that type legalization does not split
6011 // them up into i32 values.
6012 EVT VT = N->getValueType(0);
6013 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6014 return SDValue();
6015 DebugLoc dl = N->getDebugLoc();
6016 SmallVector<SDValue, 8> Ops;
6017 unsigned NumElts = VT.getVectorNumElements();
6018 for (unsigned i = 0; i < NumElts; ++i) {
6019 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6020 Ops.push_back(V);
6021 // Make the DAGCombiner fold the bitcast.
6022 DCI.AddToWorklist(V.getNode());
6023 }
6024 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6025 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6026 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6027}
6028
6029/// PerformInsertEltCombine - Target-specific dag combine xforms for
6030/// ISD::INSERT_VECTOR_ELT.
6031static SDValue PerformInsertEltCombine(SDNode *N,
6032 TargetLowering::DAGCombinerInfo &DCI) {
6033 // Bitcast an i64 load inserted into a vector to f64.
6034 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6035 EVT VT = N->getValueType(0);
6036 SDNode *Elt = N->getOperand(1).getNode();
6037 if (VT.getVectorElementType() != MVT::i64 ||
6038 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6039 return SDValue();
6040
6041 SelectionDAG &DAG = DCI.DAG;
6042 DebugLoc dl = N->getDebugLoc();
6043 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6044 VT.getVectorNumElements());
6045 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6046 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6047 // Make the DAGCombiner fold the bitcasts.
6048 DCI.AddToWorklist(Vec.getNode());
6049 DCI.AddToWorklist(V.getNode());
6050 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6051 Vec, V, N->getOperand(2));
6052 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006053}
6054
Bob Wilsonf20700c2010-10-27 20:38:28 +00006055/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6056/// ISD::VECTOR_SHUFFLE.
6057static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6058 // The LLVM shufflevector instruction does not require the shuffle mask
6059 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6060 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6061 // operands do not match the mask length, they are extended by concatenating
6062 // them with undef vectors. That is probably the right thing for other
6063 // targets, but for NEON it is better to concatenate two double-register
6064 // size vector operands into a single quad-register size vector. Do that
6065 // transformation here:
6066 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6067 // shuffle(concat(v1, v2), undef)
6068 SDValue Op0 = N->getOperand(0);
6069 SDValue Op1 = N->getOperand(1);
6070 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6071 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6072 Op0.getNumOperands() != 2 ||
6073 Op1.getNumOperands() != 2)
6074 return SDValue();
6075 SDValue Concat0Op1 = Op0.getOperand(1);
6076 SDValue Concat1Op1 = Op1.getOperand(1);
6077 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6078 Concat1Op1.getOpcode() != ISD::UNDEF)
6079 return SDValue();
6080 // Skip the transformation if any of the types are illegal.
6081 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6082 EVT VT = N->getValueType(0);
6083 if (!TLI.isTypeLegal(VT) ||
6084 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6085 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6086 return SDValue();
6087
6088 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6089 Op0.getOperand(0), Op1.getOperand(0));
6090 // Translate the shuffle mask.
6091 SmallVector<int, 16> NewMask;
6092 unsigned NumElts = VT.getVectorNumElements();
6093 unsigned HalfElts = NumElts/2;
6094 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6095 for (unsigned n = 0; n < NumElts; ++n) {
6096 int MaskElt = SVN->getMaskElt(n);
6097 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006098 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006099 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006100 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006101 NewElt = HalfElts + MaskElt - NumElts;
6102 NewMask.push_back(NewElt);
6103 }
6104 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6105 DAG.getUNDEF(VT), NewMask.data());
6106}
6107
Bob Wilson1c3ef902011-02-07 17:43:21 +00006108/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6109/// NEON load/store intrinsics to merge base address updates.
6110static SDValue CombineBaseUpdate(SDNode *N,
6111 TargetLowering::DAGCombinerInfo &DCI) {
6112 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6113 return SDValue();
6114
6115 SelectionDAG &DAG = DCI.DAG;
6116 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6117 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6118 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6119 SDValue Addr = N->getOperand(AddrOpIdx);
6120
6121 // Search for a use of the address operand that is an increment.
6122 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6123 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6124 SDNode *User = *UI;
6125 if (User->getOpcode() != ISD::ADD ||
6126 UI.getUse().getResNo() != Addr.getResNo())
6127 continue;
6128
6129 // Check that the add is independent of the load/store. Otherwise, folding
6130 // it would create a cycle.
6131 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6132 continue;
6133
6134 // Find the new opcode for the updating load/store.
6135 bool isLoad = true;
6136 bool isLaneOp = false;
6137 unsigned NewOpc = 0;
6138 unsigned NumVecs = 0;
6139 if (isIntrinsic) {
6140 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6141 switch (IntNo) {
6142 default: assert(0 && "unexpected intrinsic for Neon base update");
6143 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6144 NumVecs = 1; break;
6145 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6146 NumVecs = 2; break;
6147 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6148 NumVecs = 3; break;
6149 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6150 NumVecs = 4; break;
6151 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6152 NumVecs = 2; isLaneOp = true; break;
6153 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6154 NumVecs = 3; isLaneOp = true; break;
6155 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6156 NumVecs = 4; isLaneOp = true; break;
6157 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6158 NumVecs = 1; isLoad = false; break;
6159 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6160 NumVecs = 2; isLoad = false; break;
6161 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6162 NumVecs = 3; isLoad = false; break;
6163 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6164 NumVecs = 4; isLoad = false; break;
6165 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6166 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6167 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6168 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6169 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6170 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6171 }
6172 } else {
6173 isLaneOp = true;
6174 switch (N->getOpcode()) {
6175 default: assert(0 && "unexpected opcode for Neon base update");
6176 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6177 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6178 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6179 }
6180 }
6181
6182 // Find the size of memory referenced by the load/store.
6183 EVT VecTy;
6184 if (isLoad)
6185 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006186 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006187 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6188 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6189 if (isLaneOp)
6190 NumBytes /= VecTy.getVectorNumElements();
6191
6192 // If the increment is a constant, it must match the memory ref size.
6193 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6194 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6195 uint64_t IncVal = CInc->getZExtValue();
6196 if (IncVal != NumBytes)
6197 continue;
6198 } else if (NumBytes >= 3 * 16) {
6199 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6200 // separate instructions that make it harder to use a non-constant update.
6201 continue;
6202 }
6203
6204 // Create the new updating load/store node.
6205 EVT Tys[6];
6206 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6207 unsigned n;
6208 for (n = 0; n < NumResultVecs; ++n)
6209 Tys[n] = VecTy;
6210 Tys[n++] = MVT::i32;
6211 Tys[n] = MVT::Other;
6212 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6213 SmallVector<SDValue, 8> Ops;
6214 Ops.push_back(N->getOperand(0)); // incoming chain
6215 Ops.push_back(N->getOperand(AddrOpIdx));
6216 Ops.push_back(Inc);
6217 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6218 Ops.push_back(N->getOperand(i));
6219 }
6220 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6221 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6222 Ops.data(), Ops.size(),
6223 MemInt->getMemoryVT(),
6224 MemInt->getMemOperand());
6225
6226 // Update the uses.
6227 std::vector<SDValue> NewResults;
6228 for (unsigned i = 0; i < NumResultVecs; ++i) {
6229 NewResults.push_back(SDValue(UpdN.getNode(), i));
6230 }
6231 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6232 DCI.CombineTo(N, NewResults);
6233 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6234
6235 break;
Owen Anderson76706012011-04-05 21:48:57 +00006236 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006237 return SDValue();
6238}
6239
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006240/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6241/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6242/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6243/// return true.
6244static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6245 SelectionDAG &DAG = DCI.DAG;
6246 EVT VT = N->getValueType(0);
6247 // vldN-dup instructions only support 64-bit vectors for N > 1.
6248 if (!VT.is64BitVector())
6249 return false;
6250
6251 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6252 SDNode *VLD = N->getOperand(0).getNode();
6253 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6254 return false;
6255 unsigned NumVecs = 0;
6256 unsigned NewOpc = 0;
6257 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6258 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6259 NumVecs = 2;
6260 NewOpc = ARMISD::VLD2DUP;
6261 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6262 NumVecs = 3;
6263 NewOpc = ARMISD::VLD3DUP;
6264 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6265 NumVecs = 4;
6266 NewOpc = ARMISD::VLD4DUP;
6267 } else {
6268 return false;
6269 }
6270
6271 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6272 // numbers match the load.
6273 unsigned VLDLaneNo =
6274 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6275 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6276 UI != UE; ++UI) {
6277 // Ignore uses of the chain result.
6278 if (UI.getUse().getResNo() == NumVecs)
6279 continue;
6280 SDNode *User = *UI;
6281 if (User->getOpcode() != ARMISD::VDUPLANE ||
6282 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6283 return false;
6284 }
6285
6286 // Create the vldN-dup node.
6287 EVT Tys[5];
6288 unsigned n;
6289 for (n = 0; n < NumVecs; ++n)
6290 Tys[n] = VT;
6291 Tys[n] = MVT::Other;
6292 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6293 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6294 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6295 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6296 Ops, 2, VLDMemInt->getMemoryVT(),
6297 VLDMemInt->getMemOperand());
6298
6299 // Update the uses.
6300 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6301 UI != UE; ++UI) {
6302 unsigned ResNo = UI.getUse().getResNo();
6303 // Ignore uses of the chain result.
6304 if (ResNo == NumVecs)
6305 continue;
6306 SDNode *User = *UI;
6307 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6308 }
6309
6310 // Now the vldN-lane intrinsic is dead except for its chain result.
6311 // Update uses of the chain.
6312 std::vector<SDValue> VLDDupResults;
6313 for (unsigned n = 0; n < NumVecs; ++n)
6314 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6315 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6316 DCI.CombineTo(VLD, VLDDupResults);
6317
6318 return true;
6319}
6320
Bob Wilson9e82bf12010-07-14 01:22:12 +00006321/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6322/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006323static SDValue PerformVDUPLANECombine(SDNode *N,
6324 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006325 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006326
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006327 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6328 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6329 if (CombineVLDDUP(N, DCI))
6330 return SDValue(N, 0);
6331
6332 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6333 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006334 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006335 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006336 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006337 return SDValue();
6338
6339 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6340 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6341 // The canonical VMOV for a zero vector uses a 32-bit element size.
6342 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6343 unsigned EltBits;
6344 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6345 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006346 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006347 if (EltSize > VT.getVectorElementType().getSizeInBits())
6348 return SDValue();
6349
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006350 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006351}
6352
Bob Wilson5bafff32009-06-22 23:27:02 +00006353/// getVShiftImm - Check if this is a valid build_vector for the immediate
6354/// operand of a vector shift operation, where all the elements of the
6355/// build_vector must have the same constant integer value.
6356static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6357 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006358 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006359 Op = Op.getOperand(0);
6360 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6361 APInt SplatBits, SplatUndef;
6362 unsigned SplatBitSize;
6363 bool HasAnyUndefs;
6364 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6365 HasAnyUndefs, ElementBits) ||
6366 SplatBitSize > ElementBits)
6367 return false;
6368 Cnt = SplatBits.getSExtValue();
6369 return true;
6370}
6371
6372/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6373/// operand of a vector shift left operation. That value must be in the range:
6374/// 0 <= Value < ElementBits for a left shift; or
6375/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006376static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006377 assert(VT.isVector() && "vector shift count is not a vector type");
6378 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6379 if (! getVShiftImm(Op, ElementBits, Cnt))
6380 return false;
6381 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6382}
6383
6384/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6385/// operand of a vector shift right operation. For a shift opcode, the value
6386/// is positive, but for an intrinsic the value count must be negative. The
6387/// absolute value must be in the range:
6388/// 1 <= |Value| <= ElementBits for a right shift; or
6389/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006390static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006391 int64_t &Cnt) {
6392 assert(VT.isVector() && "vector shift count is not a vector type");
6393 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6394 if (! getVShiftImm(Op, ElementBits, Cnt))
6395 return false;
6396 if (isIntrinsic)
6397 Cnt = -Cnt;
6398 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6399}
6400
6401/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6402static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6403 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6404 switch (IntNo) {
6405 default:
6406 // Don't do anything for most intrinsics.
6407 break;
6408
6409 // Vector shifts: check for immediate versions and lower them.
6410 // Note: This is done during DAG combining instead of DAG legalizing because
6411 // the build_vectors for 64-bit vector element shift counts are generally
6412 // not legal, and it is hard to see their values after they get legalized to
6413 // loads from a constant pool.
6414 case Intrinsic::arm_neon_vshifts:
6415 case Intrinsic::arm_neon_vshiftu:
6416 case Intrinsic::arm_neon_vshiftls:
6417 case Intrinsic::arm_neon_vshiftlu:
6418 case Intrinsic::arm_neon_vshiftn:
6419 case Intrinsic::arm_neon_vrshifts:
6420 case Intrinsic::arm_neon_vrshiftu:
6421 case Intrinsic::arm_neon_vrshiftn:
6422 case Intrinsic::arm_neon_vqshifts:
6423 case Intrinsic::arm_neon_vqshiftu:
6424 case Intrinsic::arm_neon_vqshiftsu:
6425 case Intrinsic::arm_neon_vqshiftns:
6426 case Intrinsic::arm_neon_vqshiftnu:
6427 case Intrinsic::arm_neon_vqshiftnsu:
6428 case Intrinsic::arm_neon_vqrshiftns:
6429 case Intrinsic::arm_neon_vqrshiftnu:
6430 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006431 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006432 int64_t Cnt;
6433 unsigned VShiftOpc = 0;
6434
6435 switch (IntNo) {
6436 case Intrinsic::arm_neon_vshifts:
6437 case Intrinsic::arm_neon_vshiftu:
6438 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6439 VShiftOpc = ARMISD::VSHL;
6440 break;
6441 }
6442 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6443 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6444 ARMISD::VSHRs : ARMISD::VSHRu);
6445 break;
6446 }
6447 return SDValue();
6448
6449 case Intrinsic::arm_neon_vshiftls:
6450 case Intrinsic::arm_neon_vshiftlu:
6451 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6452 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006453 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006454
6455 case Intrinsic::arm_neon_vrshifts:
6456 case Intrinsic::arm_neon_vrshiftu:
6457 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6458 break;
6459 return SDValue();
6460
6461 case Intrinsic::arm_neon_vqshifts:
6462 case Intrinsic::arm_neon_vqshiftu:
6463 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6464 break;
6465 return SDValue();
6466
6467 case Intrinsic::arm_neon_vqshiftsu:
6468 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6469 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006470 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006471
6472 case Intrinsic::arm_neon_vshiftn:
6473 case Intrinsic::arm_neon_vrshiftn:
6474 case Intrinsic::arm_neon_vqshiftns:
6475 case Intrinsic::arm_neon_vqshiftnu:
6476 case Intrinsic::arm_neon_vqshiftnsu:
6477 case Intrinsic::arm_neon_vqrshiftns:
6478 case Intrinsic::arm_neon_vqrshiftnu:
6479 case Intrinsic::arm_neon_vqrshiftnsu:
6480 // Narrowing shifts require an immediate right shift.
6481 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6482 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006483 llvm_unreachable("invalid shift count for narrowing vector shift "
6484 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006485
6486 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006487 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006488 }
6489
6490 switch (IntNo) {
6491 case Intrinsic::arm_neon_vshifts:
6492 case Intrinsic::arm_neon_vshiftu:
6493 // Opcode already set above.
6494 break;
6495 case Intrinsic::arm_neon_vshiftls:
6496 case Intrinsic::arm_neon_vshiftlu:
6497 if (Cnt == VT.getVectorElementType().getSizeInBits())
6498 VShiftOpc = ARMISD::VSHLLi;
6499 else
6500 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6501 ARMISD::VSHLLs : ARMISD::VSHLLu);
6502 break;
6503 case Intrinsic::arm_neon_vshiftn:
6504 VShiftOpc = ARMISD::VSHRN; break;
6505 case Intrinsic::arm_neon_vrshifts:
6506 VShiftOpc = ARMISD::VRSHRs; break;
6507 case Intrinsic::arm_neon_vrshiftu:
6508 VShiftOpc = ARMISD::VRSHRu; break;
6509 case Intrinsic::arm_neon_vrshiftn:
6510 VShiftOpc = ARMISD::VRSHRN; break;
6511 case Intrinsic::arm_neon_vqshifts:
6512 VShiftOpc = ARMISD::VQSHLs; break;
6513 case Intrinsic::arm_neon_vqshiftu:
6514 VShiftOpc = ARMISD::VQSHLu; break;
6515 case Intrinsic::arm_neon_vqshiftsu:
6516 VShiftOpc = ARMISD::VQSHLsu; break;
6517 case Intrinsic::arm_neon_vqshiftns:
6518 VShiftOpc = ARMISD::VQSHRNs; break;
6519 case Intrinsic::arm_neon_vqshiftnu:
6520 VShiftOpc = ARMISD::VQSHRNu; break;
6521 case Intrinsic::arm_neon_vqshiftnsu:
6522 VShiftOpc = ARMISD::VQSHRNsu; break;
6523 case Intrinsic::arm_neon_vqrshiftns:
6524 VShiftOpc = ARMISD::VQRSHRNs; break;
6525 case Intrinsic::arm_neon_vqrshiftnu:
6526 VShiftOpc = ARMISD::VQRSHRNu; break;
6527 case Intrinsic::arm_neon_vqrshiftnsu:
6528 VShiftOpc = ARMISD::VQRSHRNsu; break;
6529 }
6530
6531 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006532 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006533 }
6534
6535 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006536 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006537 int64_t Cnt;
6538 unsigned VShiftOpc = 0;
6539
6540 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6541 VShiftOpc = ARMISD::VSLI;
6542 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6543 VShiftOpc = ARMISD::VSRI;
6544 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006545 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006546 }
6547
6548 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6549 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006550 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006551 }
6552
6553 case Intrinsic::arm_neon_vqrshifts:
6554 case Intrinsic::arm_neon_vqrshiftu:
6555 // No immediate versions of these to check for.
6556 break;
6557 }
6558
6559 return SDValue();
6560}
6561
6562/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6563/// lowers them. As with the vector shift intrinsics, this is done during DAG
6564/// combining instead of DAG legalizing because the build_vectors for 64-bit
6565/// vector element shift counts are generally not legal, and it is hard to see
6566/// their values after they get legalized to loads from a constant pool.
6567static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6568 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006569 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006570
6571 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006572 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6573 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006574 return SDValue();
6575
6576 assert(ST->hasNEON() && "unexpected vector shift");
6577 int64_t Cnt;
6578
6579 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006580 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006581
6582 case ISD::SHL:
6583 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6584 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006585 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006586 break;
6587
6588 case ISD::SRA:
6589 case ISD::SRL:
6590 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6591 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6592 ARMISD::VSHRs : ARMISD::VSHRu);
6593 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006594 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006595 }
6596 }
6597 return SDValue();
6598}
6599
6600/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6601/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6602static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6603 const ARMSubtarget *ST) {
6604 SDValue N0 = N->getOperand(0);
6605
6606 // Check for sign- and zero-extensions of vector extract operations of 8-
6607 // and 16-bit vector elements. NEON supports these directly. They are
6608 // handled during DAG combining because type legalization will promote them
6609 // to 32-bit types and it is messy to recognize the operations after that.
6610 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6611 SDValue Vec = N0.getOperand(0);
6612 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006613 EVT VT = N->getValueType(0);
6614 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006615 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6616
Owen Anderson825b72b2009-08-11 20:47:22 +00006617 if (VT == MVT::i32 &&
6618 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006619 TLI.isTypeLegal(Vec.getValueType()) &&
6620 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006621
6622 unsigned Opc = 0;
6623 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006624 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006625 case ISD::SIGN_EXTEND:
6626 Opc = ARMISD::VGETLANEs;
6627 break;
6628 case ISD::ZERO_EXTEND:
6629 case ISD::ANY_EXTEND:
6630 Opc = ARMISD::VGETLANEu;
6631 break;
6632 }
6633 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6634 }
6635 }
6636
6637 return SDValue();
6638}
6639
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006640/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6641/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6642static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6643 const ARMSubtarget *ST) {
6644 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006645 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006646 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6647 // a NaN; only do the transformation when it matches that behavior.
6648
6649 // For now only do this when using NEON for FP operations; if using VFP, it
6650 // is not obvious that the benefit outweighs the cost of switching to the
6651 // NEON pipeline.
6652 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6653 N->getValueType(0) != MVT::f32)
6654 return SDValue();
6655
6656 SDValue CondLHS = N->getOperand(0);
6657 SDValue CondRHS = N->getOperand(1);
6658 SDValue LHS = N->getOperand(2);
6659 SDValue RHS = N->getOperand(3);
6660 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6661
6662 unsigned Opcode = 0;
6663 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006664 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006665 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006666 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006667 IsReversed = true ; // x CC y ? y : x
6668 } else {
6669 return SDValue();
6670 }
6671
Bob Wilsone742bb52010-02-24 22:15:53 +00006672 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006673 switch (CC) {
6674 default: break;
6675 case ISD::SETOLT:
6676 case ISD::SETOLE:
6677 case ISD::SETLT:
6678 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006679 case ISD::SETULT:
6680 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006681 // If LHS is NaN, an ordered comparison will be false and the result will
6682 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6683 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6684 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6685 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6686 break;
6687 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6688 // will return -0, so vmin can only be used for unsafe math or if one of
6689 // the operands is known to be nonzero.
6690 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6691 !UnsafeFPMath &&
6692 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6693 break;
6694 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006695 break;
6696
6697 case ISD::SETOGT:
6698 case ISD::SETOGE:
6699 case ISD::SETGT:
6700 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006701 case ISD::SETUGT:
6702 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006703 // If LHS is NaN, an ordered comparison will be false and the result will
6704 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6705 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6706 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6707 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6708 break;
6709 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6710 // will return +0, so vmax can only be used for unsafe math or if one of
6711 // the operands is known to be nonzero.
6712 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6713 !UnsafeFPMath &&
6714 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6715 break;
6716 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006717 break;
6718 }
6719
6720 if (!Opcode)
6721 return SDValue();
6722 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6723}
6724
Dan Gohman475871a2008-07-27 21:46:04 +00006725SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006726 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006727 switch (N->getOpcode()) {
6728 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006729 case ISD::ADD: return PerformADDCombine(N, DCI);
6730 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006731 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006732 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006733 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006734 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006735 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006736 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006737 case ISD::STORE: return PerformSTORECombine(N, DCI);
6738 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6739 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006740 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006741 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006742 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006743 case ISD::SHL:
6744 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006745 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006746 case ISD::SIGN_EXTEND:
6747 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006748 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6749 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006750 case ARMISD::VLD2DUP:
6751 case ARMISD::VLD3DUP:
6752 case ARMISD::VLD4DUP:
6753 return CombineBaseUpdate(N, DCI);
6754 case ISD::INTRINSIC_VOID:
6755 case ISD::INTRINSIC_W_CHAIN:
6756 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6757 case Intrinsic::arm_neon_vld1:
6758 case Intrinsic::arm_neon_vld2:
6759 case Intrinsic::arm_neon_vld3:
6760 case Intrinsic::arm_neon_vld4:
6761 case Intrinsic::arm_neon_vld2lane:
6762 case Intrinsic::arm_neon_vld3lane:
6763 case Intrinsic::arm_neon_vld4lane:
6764 case Intrinsic::arm_neon_vst1:
6765 case Intrinsic::arm_neon_vst2:
6766 case Intrinsic::arm_neon_vst3:
6767 case Intrinsic::arm_neon_vst4:
6768 case Intrinsic::arm_neon_vst2lane:
6769 case Intrinsic::arm_neon_vst3lane:
6770 case Intrinsic::arm_neon_vst4lane:
6771 return CombineBaseUpdate(N, DCI);
6772 default: break;
6773 }
6774 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006775 }
Dan Gohman475871a2008-07-27 21:46:04 +00006776 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006777}
6778
Evan Cheng31959b12011-02-02 01:06:55 +00006779bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6780 EVT VT) const {
6781 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6782}
6783
Bill Wendlingaf566342009-08-15 21:21:19 +00006784bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006785 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006786 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006787
6788 switch (VT.getSimpleVT().SimpleTy) {
6789 default:
6790 return false;
6791 case MVT::i8:
6792 case MVT::i16:
6793 case MVT::i32:
6794 return true;
6795 // FIXME: VLD1 etc with standard alignment is legal.
6796 }
6797}
6798
Evan Chenge6c835f2009-08-14 20:09:37 +00006799static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6800 if (V < 0)
6801 return false;
6802
6803 unsigned Scale = 1;
6804 switch (VT.getSimpleVT().SimpleTy) {
6805 default: return false;
6806 case MVT::i1:
6807 case MVT::i8:
6808 // Scale == 1;
6809 break;
6810 case MVT::i16:
6811 // Scale == 2;
6812 Scale = 2;
6813 break;
6814 case MVT::i32:
6815 // Scale == 4;
6816 Scale = 4;
6817 break;
6818 }
6819
6820 if ((V & (Scale - 1)) != 0)
6821 return false;
6822 V /= Scale;
6823 return V == (V & ((1LL << 5) - 1));
6824}
6825
6826static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6827 const ARMSubtarget *Subtarget) {
6828 bool isNeg = false;
6829 if (V < 0) {
6830 isNeg = true;
6831 V = - V;
6832 }
6833
6834 switch (VT.getSimpleVT().SimpleTy) {
6835 default: return false;
6836 case MVT::i1:
6837 case MVT::i8:
6838 case MVT::i16:
6839 case MVT::i32:
6840 // + imm12 or - imm8
6841 if (isNeg)
6842 return V == (V & ((1LL << 8) - 1));
6843 return V == (V & ((1LL << 12) - 1));
6844 case MVT::f32:
6845 case MVT::f64:
6846 // Same as ARM mode. FIXME: NEON?
6847 if (!Subtarget->hasVFP2())
6848 return false;
6849 if ((V & 3) != 0)
6850 return false;
6851 V >>= 2;
6852 return V == (V & ((1LL << 8) - 1));
6853 }
6854}
6855
Evan Chengb01fad62007-03-12 23:30:29 +00006856/// isLegalAddressImmediate - Return true if the integer value can be used
6857/// as the offset of the target addressing mode for load / store of the
6858/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006859static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006860 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006861 if (V == 0)
6862 return true;
6863
Evan Cheng65011532009-03-09 19:15:00 +00006864 if (!VT.isSimple())
6865 return false;
6866
Evan Chenge6c835f2009-08-14 20:09:37 +00006867 if (Subtarget->isThumb1Only())
6868 return isLegalT1AddressImmediate(V, VT);
6869 else if (Subtarget->isThumb2())
6870 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006871
Evan Chenge6c835f2009-08-14 20:09:37 +00006872 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006873 if (V < 0)
6874 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006876 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006877 case MVT::i1:
6878 case MVT::i8:
6879 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006880 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006881 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006882 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006883 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006884 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006885 case MVT::f32:
6886 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006887 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006888 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006889 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006890 return false;
6891 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006892 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006893 }
Evan Chenga8e29892007-01-19 07:51:42 +00006894}
6895
Evan Chenge6c835f2009-08-14 20:09:37 +00006896bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6897 EVT VT) const {
6898 int Scale = AM.Scale;
6899 if (Scale < 0)
6900 return false;
6901
6902 switch (VT.getSimpleVT().SimpleTy) {
6903 default: return false;
6904 case MVT::i1:
6905 case MVT::i8:
6906 case MVT::i16:
6907 case MVT::i32:
6908 if (Scale == 1)
6909 return true;
6910 // r + r << imm
6911 Scale = Scale & ~1;
6912 return Scale == 2 || Scale == 4 || Scale == 8;
6913 case MVT::i64:
6914 // r + r
6915 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6916 return true;
6917 return false;
6918 case MVT::isVoid:
6919 // Note, we allow "void" uses (basically, uses that aren't loads or
6920 // stores), because arm allows folding a scale into many arithmetic
6921 // operations. This should be made more precise and revisited later.
6922
6923 // Allow r << imm, but the imm has to be a multiple of two.
6924 if (Scale & 1) return false;
6925 return isPowerOf2_32(Scale);
6926 }
6927}
6928
Chris Lattner37caf8c2007-04-09 23:33:39 +00006929/// isLegalAddressingMode - Return true if the addressing mode represented
6930/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006931bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006932 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006933 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006934 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006935 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006936
Chris Lattner37caf8c2007-04-09 23:33:39 +00006937 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006938 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006939 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006940
Chris Lattner37caf8c2007-04-09 23:33:39 +00006941 switch (AM.Scale) {
6942 case 0: // no scale reg, must be "r+i" or "r", or "i".
6943 break;
6944 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006945 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006946 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006947 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006948 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006949 // ARM doesn't support any R+R*scale+imm addr modes.
6950 if (AM.BaseOffs)
6951 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006952
Bob Wilson2c7dab12009-04-08 17:55:28 +00006953 if (!VT.isSimple())
6954 return false;
6955
Evan Chenge6c835f2009-08-14 20:09:37 +00006956 if (Subtarget->isThumb2())
6957 return isLegalT2ScaledAddressingMode(AM, VT);
6958
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006959 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006961 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006962 case MVT::i1:
6963 case MVT::i8:
6964 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006965 if (Scale < 0) Scale = -Scale;
6966 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006967 return true;
6968 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006969 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006970 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006971 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006972 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006973 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006974 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006975 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006976
Owen Anderson825b72b2009-08-11 20:47:22 +00006977 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006978 // Note, we allow "void" uses (basically, uses that aren't loads or
6979 // stores), because arm allows folding a scale into many arithmetic
6980 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006981
Chris Lattner37caf8c2007-04-09 23:33:39 +00006982 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006983 if (Scale & 1) return false;
6984 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006985 }
6986 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006987 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006988 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006989}
6990
Evan Cheng77e47512009-11-11 19:05:52 +00006991/// isLegalICmpImmediate - Return true if the specified immediate is legal
6992/// icmp immediate, that is the target has icmp instructions which can compare
6993/// a register against the immediate without having to materialize the
6994/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006995bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006996 if (!Subtarget->isThumb())
6997 return ARM_AM::getSOImmVal(Imm) != -1;
6998 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006999 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007000 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007001}
7002
Dan Gohmancca82142011-05-03 00:46:49 +00007003/// isLegalAddImmediate - Return true if the specified immediate is legal
7004/// add immediate, that is the target has add instructions which can add
7005/// a register with the immediate without having to materialize the
7006/// immediate into a register.
7007bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7008 return ARM_AM::getSOImmVal(Imm) != -1;
7009}
7010
Owen Andersone50ed302009-08-10 22:56:29 +00007011static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007012 bool isSEXTLoad, SDValue &Base,
7013 SDValue &Offset, bool &isInc,
7014 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007015 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7016 return false;
7017
Owen Anderson825b72b2009-08-11 20:47:22 +00007018 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007019 // AddressingMode 3
7020 Base = Ptr->getOperand(0);
7021 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007022 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007023 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007024 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007025 isInc = false;
7026 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7027 return true;
7028 }
7029 }
7030 isInc = (Ptr->getOpcode() == ISD::ADD);
7031 Offset = Ptr->getOperand(1);
7032 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007034 // AddressingMode 2
7035 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007036 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007037 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007038 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007039 isInc = false;
7040 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7041 Base = Ptr->getOperand(0);
7042 return true;
7043 }
7044 }
7045
7046 if (Ptr->getOpcode() == ISD::ADD) {
7047 isInc = true;
7048 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
7049 if (ShOpcVal != ARM_AM::no_shift) {
7050 Base = Ptr->getOperand(1);
7051 Offset = Ptr->getOperand(0);
7052 } else {
7053 Base = Ptr->getOperand(0);
7054 Offset = Ptr->getOperand(1);
7055 }
7056 return true;
7057 }
7058
7059 isInc = (Ptr->getOpcode() == ISD::ADD);
7060 Base = Ptr->getOperand(0);
7061 Offset = Ptr->getOperand(1);
7062 return true;
7063 }
7064
Jim Grosbache5165492009-11-09 00:11:35 +00007065 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007066 return false;
7067}
7068
Owen Andersone50ed302009-08-10 22:56:29 +00007069static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007070 bool isSEXTLoad, SDValue &Base,
7071 SDValue &Offset, bool &isInc,
7072 SelectionDAG &DAG) {
7073 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7074 return false;
7075
7076 Base = Ptr->getOperand(0);
7077 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7078 int RHSC = (int)RHS->getZExtValue();
7079 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7080 assert(Ptr->getOpcode() == ISD::ADD);
7081 isInc = false;
7082 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7083 return true;
7084 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7085 isInc = Ptr->getOpcode() == ISD::ADD;
7086 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7087 return true;
7088 }
7089 }
7090
7091 return false;
7092}
7093
Evan Chenga8e29892007-01-19 07:51:42 +00007094/// getPreIndexedAddressParts - returns true by value, base pointer and
7095/// offset pointer and addressing mode by reference if the node's address
7096/// can be legally represented as pre-indexed load / store address.
7097bool
Dan Gohman475871a2008-07-27 21:46:04 +00007098ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7099 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007100 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007101 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007102 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007103 return false;
7104
Owen Andersone50ed302009-08-10 22:56:29 +00007105 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007106 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007107 bool isSEXTLoad = false;
7108 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7109 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007110 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007111 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7112 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7113 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007114 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007115 } else
7116 return false;
7117
7118 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007119 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007120 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007121 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7122 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007123 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007124 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007125 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007126 if (!isLegal)
7127 return false;
7128
7129 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7130 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007131}
7132
7133/// getPostIndexedAddressParts - returns true by value, base pointer and
7134/// offset pointer and addressing mode by reference if this node can be
7135/// combined with a load / store to form a post-indexed load / store.
7136bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007137 SDValue &Base,
7138 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007139 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007140 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007141 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007142 return false;
7143
Owen Andersone50ed302009-08-10 22:56:29 +00007144 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007145 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007146 bool isSEXTLoad = false;
7147 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007148 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007149 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007150 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7151 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007152 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007153 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007154 } else
7155 return false;
7156
7157 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007158 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007159 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007160 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007161 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007162 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007163 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7164 isInc, DAG);
7165 if (!isLegal)
7166 return false;
7167
Evan Cheng28dad2a2010-05-18 21:31:17 +00007168 if (Ptr != Base) {
7169 // Swap base ptr and offset to catch more post-index load / store when
7170 // it's legal. In Thumb2 mode, offset must be an immediate.
7171 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7172 !Subtarget->isThumb2())
7173 std::swap(Base, Offset);
7174
7175 // Post-indexed load / store update the base pointer.
7176 if (Ptr != Base)
7177 return false;
7178 }
7179
Evan Chenge88d5ce2009-07-02 07:28:31 +00007180 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7181 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007182}
7183
Dan Gohman475871a2008-07-27 21:46:04 +00007184void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007185 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007186 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007187 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007188 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007189 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007190 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007191 switch (Op.getOpcode()) {
7192 default: break;
7193 case ARMISD::CMOV: {
7194 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007195 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007196 if (KnownZero == 0 && KnownOne == 0) return;
7197
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007198 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007199 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7200 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007201 KnownZero &= KnownZeroRHS;
7202 KnownOne &= KnownOneRHS;
7203 return;
7204 }
7205 }
7206}
7207
7208//===----------------------------------------------------------------------===//
7209// ARM Inline Assembly Support
7210//===----------------------------------------------------------------------===//
7211
Evan Cheng55d42002011-01-08 01:24:27 +00007212bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7213 // Looking for "rev" which is V6+.
7214 if (!Subtarget->hasV6Ops())
7215 return false;
7216
7217 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7218 std::string AsmStr = IA->getAsmString();
7219 SmallVector<StringRef, 4> AsmPieces;
7220 SplitString(AsmStr, AsmPieces, ";\n");
7221
7222 switch (AsmPieces.size()) {
7223 default: return false;
7224 case 1:
7225 AsmStr = AsmPieces[0];
7226 AsmPieces.clear();
7227 SplitString(AsmStr, AsmPieces, " \t,");
7228
7229 // rev $0, $1
7230 if (AsmPieces.size() == 3 &&
7231 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7232 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
7233 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
7234 if (Ty && Ty->getBitWidth() == 32)
7235 return IntrinsicLowering::LowerToByteSwap(CI);
7236 }
7237 break;
7238 }
7239
7240 return false;
7241}
7242
Evan Chenga8e29892007-01-19 07:51:42 +00007243/// getConstraintType - Given a constraint letter, return the type of
7244/// constraint it is for this target.
7245ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007246ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7247 if (Constraint.size() == 1) {
7248 switch (Constraint[0]) {
7249 default: break;
7250 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007251 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00007252 }
Evan Chenga8e29892007-01-19 07:51:42 +00007253 }
Chris Lattner4234f572007-03-25 02:14:49 +00007254 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007255}
7256
John Thompson44ab89e2010-10-29 17:29:13 +00007257/// Examine constraint type and operand type and determine a weight value.
7258/// This object must already have been set up with the operand type
7259/// and the current alternative constraint selected.
7260TargetLowering::ConstraintWeight
7261ARMTargetLowering::getSingleConstraintMatchWeight(
7262 AsmOperandInfo &info, const char *constraint) const {
7263 ConstraintWeight weight = CW_Invalid;
7264 Value *CallOperandVal = info.CallOperandVal;
7265 // If we don't have a value, we can't do a match,
7266 // but allow it at the lowest weight.
7267 if (CallOperandVal == NULL)
7268 return CW_Default;
7269 const Type *type = CallOperandVal->getType();
7270 // Look at the constraint type.
7271 switch (*constraint) {
7272 default:
7273 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7274 break;
7275 case 'l':
7276 if (type->isIntegerTy()) {
7277 if (Subtarget->isThumb())
7278 weight = CW_SpecificReg;
7279 else
7280 weight = CW_Register;
7281 }
7282 break;
7283 case 'w':
7284 if (type->isFloatingPointTy())
7285 weight = CW_Register;
7286 break;
7287 }
7288 return weight;
7289}
7290
Bob Wilson2dc4f542009-03-20 22:42:55 +00007291std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007292ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007293 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007294 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007295 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007296 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007297 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007298 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007299 return std::make_pair(0U, ARM::tGPRRegisterClass);
7300 else
7301 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007302 case 'r':
7303 return std::make_pair(0U, ARM::GPRRegisterClass);
7304 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007305 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007306 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007307 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007308 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007309 if (VT.getSizeInBits() == 128)
7310 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007311 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007312 }
7313 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007314 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007315 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007316
Evan Chenga8e29892007-01-19 07:51:42 +00007317 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7318}
7319
7320std::vector<unsigned> ARMTargetLowering::
7321getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007322 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007323 if (Constraint.size() != 1)
7324 return std::vector<unsigned>();
7325
7326 switch (Constraint[0]) { // GCC ARM Constraint Letters
7327 default: break;
7328 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007329 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7330 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7331 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007332 case 'r':
7333 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7334 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7335 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7336 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007337 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007338 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007339 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7340 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7341 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7342 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7343 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7344 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7345 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7346 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007347 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007348 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7349 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7350 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7351 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007352 if (VT.getSizeInBits() == 128)
7353 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7354 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007355 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007356 }
7357
7358 return std::vector<unsigned>();
7359}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007360
7361/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7362/// vector. If it is invalid, don't add anything to Ops.
7363void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
7364 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007365 std::vector<SDValue>&Ops,
7366 SelectionDAG &DAG) const {
7367 SDValue Result(0, 0);
7368
7369 switch (Constraint) {
7370 default: break;
7371 case 'I': case 'J': case 'K': case 'L':
7372 case 'M': case 'N': case 'O':
7373 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7374 if (!C)
7375 return;
7376
7377 int64_t CVal64 = C->getSExtValue();
7378 int CVal = (int) CVal64;
7379 // None of these constraints allow values larger than 32 bits. Check
7380 // that the value fits in an int.
7381 if (CVal != CVal64)
7382 return;
7383
7384 switch (Constraint) {
7385 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007386 if (Subtarget->isThumb1Only()) {
7387 // This must be a constant between 0 and 255, for ADD
7388 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007389 if (CVal >= 0 && CVal <= 255)
7390 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007391 } else if (Subtarget->isThumb2()) {
7392 // A constant that can be used as an immediate value in a
7393 // data-processing instruction.
7394 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7395 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007396 } else {
7397 // A constant that can be used as an immediate value in a
7398 // data-processing instruction.
7399 if (ARM_AM::getSOImmVal(CVal) != -1)
7400 break;
7401 }
7402 return;
7403
7404 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007405 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007406 // This must be a constant between -255 and -1, for negated ADD
7407 // immediates. This can be used in GCC with an "n" modifier that
7408 // prints the negated value, for use with SUB instructions. It is
7409 // not useful otherwise but is implemented for compatibility.
7410 if (CVal >= -255 && CVal <= -1)
7411 break;
7412 } else {
7413 // This must be a constant between -4095 and 4095. It is not clear
7414 // what this constraint is intended for. Implemented for
7415 // compatibility with GCC.
7416 if (CVal >= -4095 && CVal <= 4095)
7417 break;
7418 }
7419 return;
7420
7421 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007422 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007423 // A 32-bit value where only one byte has a nonzero value. Exclude
7424 // zero to match GCC. This constraint is used by GCC internally for
7425 // constants that can be loaded with a move/shift combination.
7426 // It is not useful otherwise but is implemented for compatibility.
7427 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7428 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007429 } else if (Subtarget->isThumb2()) {
7430 // A constant whose bitwise inverse can be used as an immediate
7431 // value in a data-processing instruction. This can be used in GCC
7432 // with a "B" modifier that prints the inverted value, for use with
7433 // BIC and MVN instructions. It is not useful otherwise but is
7434 // implemented for compatibility.
7435 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7436 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007437 } else {
7438 // A constant whose bitwise inverse can be used as an immediate
7439 // value in a data-processing instruction. This can be used in GCC
7440 // with a "B" modifier that prints the inverted value, for use with
7441 // BIC and MVN instructions. It is not useful otherwise but is
7442 // implemented for compatibility.
7443 if (ARM_AM::getSOImmVal(~CVal) != -1)
7444 break;
7445 }
7446 return;
7447
7448 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007449 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007450 // This must be a constant between -7 and 7,
7451 // for 3-operand ADD/SUB immediate instructions.
7452 if (CVal >= -7 && CVal < 7)
7453 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007454 } else if (Subtarget->isThumb2()) {
7455 // A constant whose negation can be used as an immediate value in a
7456 // data-processing instruction. This can be used in GCC with an "n"
7457 // modifier that prints the negated value, for use with SUB
7458 // instructions. It is not useful otherwise but is implemented for
7459 // compatibility.
7460 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7461 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007462 } else {
7463 // A constant whose negation can be used as an immediate value in a
7464 // data-processing instruction. This can be used in GCC with an "n"
7465 // modifier that prints the negated value, for use with SUB
7466 // instructions. It is not useful otherwise but is implemented for
7467 // compatibility.
7468 if (ARM_AM::getSOImmVal(-CVal) != -1)
7469 break;
7470 }
7471 return;
7472
7473 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007474 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007475 // This must be a multiple of 4 between 0 and 1020, for
7476 // ADD sp + immediate.
7477 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7478 break;
7479 } else {
7480 // A power of two or a constant between 0 and 32. This is used in
7481 // GCC for the shift amount on shifted register operands, but it is
7482 // useful in general for any shift amounts.
7483 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7484 break;
7485 }
7486 return;
7487
7488 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007489 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007490 // This must be a constant between 0 and 31, for shift amounts.
7491 if (CVal >= 0 && CVal <= 31)
7492 break;
7493 }
7494 return;
7495
7496 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007497 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007498 // This must be a multiple of 4 between -508 and 508, for
7499 // ADD/SUB sp = sp + immediate.
7500 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7501 break;
7502 }
7503 return;
7504 }
7505 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7506 break;
7507 }
7508
7509 if (Result.getNode()) {
7510 Ops.push_back(Result);
7511 return;
7512 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007513 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007514}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007515
7516bool
7517ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7518 // The ARM target isn't yet aware of offsets.
7519 return false;
7520}
Evan Cheng39382422009-10-28 01:44:26 +00007521
7522int ARM::getVFPf32Imm(const APFloat &FPImm) {
7523 APInt Imm = FPImm.bitcastToAPInt();
7524 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7525 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7526 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7527
7528 // We can handle 4 bits of mantissa.
7529 // mantissa = (16+UInt(e:f:g:h))/16.
7530 if (Mantissa & 0x7ffff)
7531 return -1;
7532 Mantissa >>= 19;
7533 if ((Mantissa & 0xf) != Mantissa)
7534 return -1;
7535
7536 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7537 if (Exp < -3 || Exp > 4)
7538 return -1;
7539 Exp = ((Exp+3) & 0x7) ^ 4;
7540
7541 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7542}
7543
7544int ARM::getVFPf64Imm(const APFloat &FPImm) {
7545 APInt Imm = FPImm.bitcastToAPInt();
7546 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7547 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7548 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7549
7550 // We can handle 4 bits of mantissa.
7551 // mantissa = (16+UInt(e:f:g:h))/16.
7552 if (Mantissa & 0xffffffffffffLL)
7553 return -1;
7554 Mantissa >>= 48;
7555 if ((Mantissa & 0xf) != Mantissa)
7556 return -1;
7557
7558 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7559 if (Exp < -3 || Exp > 4)
7560 return -1;
7561 Exp = ((Exp+3) & 0x7) ^ 4;
7562
7563 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7564}
7565
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007566bool ARM::isBitFieldInvertedMask(unsigned v) {
7567 if (v == 0xffffffff)
7568 return 0;
7569 // there can be 1's on either or both "outsides", all the "inside"
7570 // bits must be 0's
7571 unsigned int lsb = 0, msb = 31;
7572 while (v & (1 << msb)) --msb;
7573 while (v & (1 << lsb)) ++lsb;
7574 for (unsigned int i = lsb; i <= msb; ++i) {
7575 if (v & (1 << i))
7576 return 0;
7577 }
7578 return 1;
7579}
7580
Evan Cheng39382422009-10-28 01:44:26 +00007581/// isFPImmLegal - Returns true if the target can instruction select the
7582/// specified FP immediate natively. If false, the legalizer will
7583/// materialize the FP immediate as a load from a constant pool.
7584bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7585 if (!Subtarget->hasVFP3())
7586 return false;
7587 if (VT == MVT::f32)
7588 return ARM::getVFPf32Imm(Imm) != -1;
7589 if (VT == MVT::f64)
7590 return ARM::getVFPf64Imm(Imm) != -1;
7591 return false;
7592}
Bob Wilson65ffec42010-09-21 17:56:22 +00007593
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007594/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007595/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7596/// specified in the intrinsic calls.
7597bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7598 const CallInst &I,
7599 unsigned Intrinsic) const {
7600 switch (Intrinsic) {
7601 case Intrinsic::arm_neon_vld1:
7602 case Intrinsic::arm_neon_vld2:
7603 case Intrinsic::arm_neon_vld3:
7604 case Intrinsic::arm_neon_vld4:
7605 case Intrinsic::arm_neon_vld2lane:
7606 case Intrinsic::arm_neon_vld3lane:
7607 case Intrinsic::arm_neon_vld4lane: {
7608 Info.opc = ISD::INTRINSIC_W_CHAIN;
7609 // Conservatively set memVT to the entire set of vectors loaded.
7610 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7611 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7612 Info.ptrVal = I.getArgOperand(0);
7613 Info.offset = 0;
7614 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7615 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7616 Info.vol = false; // volatile loads with NEON intrinsics not supported
7617 Info.readMem = true;
7618 Info.writeMem = false;
7619 return true;
7620 }
7621 case Intrinsic::arm_neon_vst1:
7622 case Intrinsic::arm_neon_vst2:
7623 case Intrinsic::arm_neon_vst3:
7624 case Intrinsic::arm_neon_vst4:
7625 case Intrinsic::arm_neon_vst2lane:
7626 case Intrinsic::arm_neon_vst3lane:
7627 case Intrinsic::arm_neon_vst4lane: {
7628 Info.opc = ISD::INTRINSIC_VOID;
7629 // Conservatively set memVT to the entire set of vectors stored.
7630 unsigned NumElts = 0;
7631 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7632 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7633 if (!ArgTy->isVectorTy())
7634 break;
7635 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7636 }
7637 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7638 Info.ptrVal = I.getArgOperand(0);
7639 Info.offset = 0;
7640 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7641 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7642 Info.vol = false; // volatile stores with NEON intrinsics not supported
7643 Info.readMem = false;
7644 Info.writeMem = true;
7645 return true;
7646 }
7647 default:
7648 break;
7649 }
7650
7651 return false;
7652}