blob: 4afc8c7fac78d416fe548c03425bb679a0c2f5af [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
Owen Andersone50ed302009-08-10 22:56:29 +000075void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
76 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000077 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000078 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000079 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
80 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000081
Owen Anderson70671842009-08-10 20:18:46 +000082 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000083 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000084 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000085 }
86
Owen Andersone50ed302009-08-10 22:56:29 +000087 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000088 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000089 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +000090 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000091 if (ElemTy != MVT::i32) {
92 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
94 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
95 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
96 }
Owen Anderson70671842009-08-10 20:18:46 +000097 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
98 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +000099 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000100 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000101 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
102 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000103 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000104 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
105 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000107 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
108 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000109 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
110 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
111 setTruncStoreAction(VT.getSimpleVT(),
112 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000113 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000114 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000115
116 // Promote all bit-wise operations.
117 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000118 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000119 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
120 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000121 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000122 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000123 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000125 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000126 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000127 }
Bob Wilson16330762009-09-16 00:17:28 +0000128
129 // Neon does not support vector divide/remainder operations.
130 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
131 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
132 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
133 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
134 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
135 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000136}
137
Owen Andersone50ed302009-08-10 22:56:29 +0000138void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000139 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000141}
142
Owen Andersone50ed302009-08-10 22:56:29 +0000143void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000144 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000146}
147
Chris Lattnerf0144122009-07-28 03:13:23 +0000148static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
149 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000150 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000151
Chris Lattner80ec2792009-08-02 00:34:36 +0000152 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000153}
154
Evan Chenga8e29892007-01-19 07:51:42 +0000155ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000156 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000157 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000158 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000159 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000160
Evan Chengb1df8f22007-04-27 08:15:43 +0000161 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000162 // Uses VFP for Thumb libfuncs if available.
163 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
164 // Single-precision floating-point arithmetic.
165 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
166 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
167 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
168 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000169
Evan Chengb1df8f22007-04-27 08:15:43 +0000170 // Double-precision floating-point arithmetic.
171 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
172 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
173 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
174 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000175
Evan Chengb1df8f22007-04-27 08:15:43 +0000176 // Single-precision comparisons.
177 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
178 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
179 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
180 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
181 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
182 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
183 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
184 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000185
Evan Chengb1df8f22007-04-27 08:15:43 +0000186 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000194
Evan Chengb1df8f22007-04-27 08:15:43 +0000195 // Double-precision comparisons.
196 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
197 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
198 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
199 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
200 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
201 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
202 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
203 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000204
Evan Chengb1df8f22007-04-27 08:15:43 +0000205 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Floating-point to integer conversions.
215 // i64 conversions are done via library routines even when generating VFP
216 // instructions, so use the same ones.
217 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
218 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
219 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
220 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000221
Evan Chengb1df8f22007-04-27 08:15:43 +0000222 // Conversions between floating types.
223 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
224 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
225
226 // Integer to floating-point conversions.
227 // i64 conversions are done via library routines even when generating VFP
228 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000229 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
230 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000231 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
232 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
233 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
234 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
235 }
Evan Chenga8e29892007-01-19 07:51:42 +0000236 }
237
Bob Wilson2f954612009-05-22 17:38:41 +0000238 // These libcalls are not available in 32-bit.
239 setLibcallName(RTLIB::SHL_I128, 0);
240 setLibcallName(RTLIB::SRL_I128, 0);
241 setLibcallName(RTLIB::SRA_I128, 0);
242
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000243 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000245 // RTABI chapter 4.1.2, Table 2
246 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
247 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
248 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
249 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
250 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
251 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
252 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
253 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
254
255 // Double-precision floating-point comparison helper functions
256 // RTABI chapter 4.1.2, Table 3
257 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
258 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
259 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
260 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
261 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
262 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
263 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
264 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
265 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
266 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
267 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
268 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
269 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
270 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
271 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
272 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
273 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
276 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
277 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
278 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
279 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
280 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
281
282 // Single-precision floating-point arithmetic helper functions
283 // RTABI chapter 4.1.2, Table 4
284 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
285 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
286 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
287 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
288 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
292
293 // Single-precision floating-point comparison helper functions
294 // RTABI chapter 4.1.2, Table 5
295 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
296 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
297 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
298 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
299 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
300 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
301 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
302 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
303 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
304 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
305 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
306 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
307 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
308 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
309 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
310 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
311 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
314 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
315 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
316 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
317 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
318 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
319
320 // Floating-point to integer conversions.
321 // RTABI chapter 4.1.2, Table 6
322 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
323 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
324 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
325 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
326 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
327 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
328 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
329 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
330 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
338
339 // Conversions between floating types.
340 // RTABI chapter 4.1.2, Table 7
341 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
342 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
343 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000344 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000345
346 // Integer to floating-point conversions.
347 // RTABI chapter 4.1.2, Table 8
348 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
349 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
350 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
351 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
352 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
353 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
354 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
355 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
356 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
360 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
361 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
362 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
363 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
364
365 // Long long helper functions
366 // RTABI chapter 4.2, Table 9
367 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
368 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
369 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
370 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
371 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
372 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
373 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
379
380 // Integer division functions
381 // RTABI chapter 4.3.1
382 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
383 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
384 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
385 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
386 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
387 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
388 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000393 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000394 }
395
David Goodwinf1daf7d2009-07-08 23:10:31 +0000396 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000398 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000400 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000402 if (!Subtarget->isFPOnlySP())
403 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000404
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000406 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000407
408 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 addDRTypeForNEON(MVT::v2f32);
410 addDRTypeForNEON(MVT::v8i8);
411 addDRTypeForNEON(MVT::v4i16);
412 addDRTypeForNEON(MVT::v2i32);
413 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000414
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 addQRTypeForNEON(MVT::v4f32);
416 addQRTypeForNEON(MVT::v2f64);
417 addQRTypeForNEON(MVT::v16i8);
418 addQRTypeForNEON(MVT::v8i16);
419 addQRTypeForNEON(MVT::v4i32);
420 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000421
Bob Wilson74dc72e2009-09-15 23:55:57 +0000422 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
423 // neither Neon nor VFP support any arithmetic operations on it.
424 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
425 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
426 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
427 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
428 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
429 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
430 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
431 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
432 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
433 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
435 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
436 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
437 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
438 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
439 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
440 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
441 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
442 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
443 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
444 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
445 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
446 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
447 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
448
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000449 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
450
Bob Wilson642b3292009-09-16 00:32:15 +0000451 // Neon does not support some operations on v1i64 and v2i64 types.
452 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000453 // Custom handling for some quad-vector types to detect VMULL.
454 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
455 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
456 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000457 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
458 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
459
Bob Wilson1c3ef902011-02-07 17:43:21 +0000460 setTargetDAGCombine(ISD::INTRINSIC_VOID);
461 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000462 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
463 setTargetDAGCombine(ISD::SHL);
464 setTargetDAGCombine(ISD::SRL);
465 setTargetDAGCombine(ISD::SRA);
466 setTargetDAGCombine(ISD::SIGN_EXTEND);
467 setTargetDAGCombine(ISD::ZERO_EXTEND);
468 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000469 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000470 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000471 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000472 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
473 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000474 }
475
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000476 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000477
478 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000480
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000481 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000483
Evan Chenga8e29892007-01-19 07:51:42 +0000484 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000485 if (!Subtarget->isThumb1Only()) {
486 for (unsigned im = (unsigned)ISD::PRE_INC;
487 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setIndexedLoadAction(im, MVT::i1, Legal);
489 setIndexedLoadAction(im, MVT::i8, Legal);
490 setIndexedLoadAction(im, MVT::i16, Legal);
491 setIndexedLoadAction(im, MVT::i32, Legal);
492 setIndexedStoreAction(im, MVT::i1, Legal);
493 setIndexedStoreAction(im, MVT::i8, Legal);
494 setIndexedStoreAction(im, MVT::i16, Legal);
495 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000496 }
Evan Chenga8e29892007-01-19 07:51:42 +0000497 }
498
499 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000500 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::MUL, MVT::i64, Expand);
502 setOperationAction(ISD::MULHU, MVT::i32, Expand);
503 setOperationAction(ISD::MULHS, MVT::i32, Expand);
504 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
505 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000506 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000507 setOperationAction(ISD::MUL, MVT::i64, Expand);
508 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000509 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000511 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000512 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000513 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000514 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::SRL, MVT::i64, Custom);
516 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000517
518 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000520 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000522 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000524
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000525 // Only ARMv6 has BSWAP.
526 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000528
Evan Chenga8e29892007-01-19 07:51:42 +0000529 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000530 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000531 // v7M has a hardware divider
532 setOperationAction(ISD::SDIV, MVT::i32, Expand);
533 setOperationAction(ISD::UDIV, MVT::i32, Expand);
534 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::SREM, MVT::i32, Expand);
536 setOperationAction(ISD::UREM, MVT::i32, Expand);
537 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
538 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000539
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
541 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
542 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
543 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000544 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000545
Evan Chengfb3611d2010-05-11 07:26:32 +0000546 setOperationAction(ISD::TRAP, MVT::Other, Legal);
547
Evan Chenga8e29892007-01-19 07:51:42 +0000548 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::VASTART, MVT::Other, Custom);
550 setOperationAction(ISD::VAARG, MVT::Other, Expand);
551 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
552 setOperationAction(ISD::VAEND, MVT::Other, Expand);
553 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
554 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000555 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000556 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
557 setExceptionPointerRegister(ARM::R0);
558 setExceptionSelectorRegister(ARM::R1);
559
Evan Cheng3a1588a2010-04-15 22:20:34 +0000560 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000561 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
562 // the default expansion.
563 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000564 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000565 // membarrier needs custom lowering; the rest are legal and handled
566 // normally.
567 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
568 } else {
569 // Set them all for expansion, which will force libcalls.
570 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
571 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
572 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
573 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000574 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
575 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
576 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000577 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
578 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
579 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
580 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
581 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
582 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
583 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
584 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
585 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
586 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
587 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
588 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
589 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
590 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
591 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
592 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
593 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
594 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000595 // Since the libcalls include locking, fold in the fences
596 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000597 }
598 // 64-bit versions are always libcalls (for now)
599 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000600 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000601 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
606 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000607
Evan Cheng416941d2010-11-04 05:19:35 +0000608 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000609
Eli Friedmana2c6f452010-06-26 04:36:50 +0000610 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
611 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
613 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000614 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000616
Nate Begemand1fb5832010-08-03 21:31:55 +0000617 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000618 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
619 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000620 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000621 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
622 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000623
624 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000626 if (Subtarget->isTargetDarwin()) {
627 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
628 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000629 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000630 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000631
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::SETCC, MVT::i32, Expand);
633 setOperationAction(ISD::SETCC, MVT::f32, Expand);
634 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000635 setOperationAction(ISD::SELECT, MVT::i32, Custom);
636 setOperationAction(ISD::SELECT, MVT::f32, Custom);
637 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
639 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
640 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000641
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
643 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
644 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
645 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
646 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000647
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000648 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000649 setOperationAction(ISD::FSIN, MVT::f64, Expand);
650 setOperationAction(ISD::FSIN, MVT::f32, Expand);
651 setOperationAction(ISD::FCOS, MVT::f32, Expand);
652 setOperationAction(ISD::FCOS, MVT::f64, Expand);
653 setOperationAction(ISD::FREM, MVT::f64, Expand);
654 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000655 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
657 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000658 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::FPOW, MVT::f64, Expand);
660 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000661
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000662 // Various VFP goodness
663 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000664 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
665 if (Subtarget->hasVFP2()) {
666 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
667 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
668 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
669 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
670 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000671 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000672 if (!Subtarget->hasFP16()) {
673 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
674 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000675 }
Evan Cheng110cf482008-04-01 01:50:16 +0000676 }
Evan Chenga8e29892007-01-19 07:51:42 +0000677
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000678 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000679 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000680 setTargetDAGCombine(ISD::ADD);
681 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000682 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000683
Owen Anderson080c0922010-11-05 19:27:46 +0000684 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000685 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000686 if (Subtarget->hasNEON())
687 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000688
Evan Chenga8e29892007-01-19 07:51:42 +0000689 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000690
Evan Chengf7d87ee2010-05-21 00:43:17 +0000691 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
692 setSchedulingPreference(Sched::RegPressure);
693 else
694 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000695
Evan Cheng05219282011-01-06 06:52:41 +0000696 //// temporary - rewrite interface to use type
697 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000698
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000699 // On ARM arguments smaller than 4 bytes are extended, so all arguments
700 // are at least 4 bytes aligned.
701 setMinStackArgumentAlignment(4);
702
Evan Chengfff606d2010-09-24 19:07:23 +0000703 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000704}
705
Andrew Trick32cec0a2011-01-19 02:35:27 +0000706// FIXME: It might make sense to define the representative register class as the
707// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
708// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
709// SPR's representative would be DPR_VFP2. This should work well if register
710// pressure tracking were modified such that a register use would increment the
711// pressure of the register class's representative and all of it's super
712// classes' representatives transitively. We have not implemented this because
713// of the difficulty prior to coalescing of modeling operand register classes
714// due to the common occurence of cross class copies and subregister insertions
715// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000716std::pair<const TargetRegisterClass*, uint8_t>
717ARMTargetLowering::findRepresentativeClass(EVT VT) const{
718 const TargetRegisterClass *RRC = 0;
719 uint8_t Cost = 1;
720 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000721 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000722 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000723 // Use DPR as representative register class for all floating point
724 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
725 // the cost is 1 for both f32 and f64.
726 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000727 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000728 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000729 // When NEON is used for SP, only half of the register file is available
730 // because operations that define both SP and DP results will be constrained
731 // to the VFP2 class (D0-D15). We currently model this constraint prior to
732 // coalescing by double-counting the SP regs. See the FIXME above.
733 if (Subtarget->useNEONForSinglePrecisionFP())
734 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000735 break;
736 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
737 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000738 RRC = ARM::DPRRegisterClass;
739 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000740 break;
741 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000742 RRC = ARM::DPRRegisterClass;
743 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000744 break;
745 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000746 RRC = ARM::DPRRegisterClass;
747 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000748 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000749 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000750 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000751}
752
Evan Chenga8e29892007-01-19 07:51:42 +0000753const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
754 switch (Opcode) {
755 default: return 0;
756 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000757 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000758 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000759 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
760 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000761 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000762 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
763 case ARMISD::tCALL: return "ARMISD::tCALL";
764 case ARMISD::BRCOND: return "ARMISD::BRCOND";
765 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000766 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000767 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
768 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
769 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000770 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000771 case ARMISD::CMPFP: return "ARMISD::CMPFP";
772 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000773 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000774 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
775 case ARMISD::CMOV: return "ARMISD::CMOV";
776 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000777
Jim Grosbach3482c802010-01-18 19:58:49 +0000778 case ARMISD::RBIT: return "ARMISD::RBIT";
779
Bob Wilson76a312b2010-03-19 22:51:32 +0000780 case ARMISD::FTOSI: return "ARMISD::FTOSI";
781 case ARMISD::FTOUI: return "ARMISD::FTOUI";
782 case ARMISD::SITOF: return "ARMISD::SITOF";
783 case ARMISD::UITOF: return "ARMISD::UITOF";
784
Evan Chenga8e29892007-01-19 07:51:42 +0000785 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
786 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
787 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000788
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000789 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
790 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000791
Evan Chengc5942082009-10-28 06:55:03 +0000792 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
793 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000794 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000795
Dale Johannesen51e28e62010-06-03 21:09:53 +0000796 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000797
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000798 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000799
Evan Cheng86198642009-08-07 00:34:42 +0000800 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
801
Jim Grosbach3728e962009-12-10 00:11:09 +0000802 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000803 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000804
Evan Chengdfed19f2010-11-03 06:34:55 +0000805 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
806
Bob Wilson5bafff32009-06-22 23:27:02 +0000807 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000808 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000809 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000810 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
811 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000812 case ARMISD::VCGEU: return "ARMISD::VCGEU";
813 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000814 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
815 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000816 case ARMISD::VCGTU: return "ARMISD::VCGTU";
817 case ARMISD::VTST: return "ARMISD::VTST";
818
819 case ARMISD::VSHL: return "ARMISD::VSHL";
820 case ARMISD::VSHRs: return "ARMISD::VSHRs";
821 case ARMISD::VSHRu: return "ARMISD::VSHRu";
822 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
823 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
824 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
825 case ARMISD::VSHRN: return "ARMISD::VSHRN";
826 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
827 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
828 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
829 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
830 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
831 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
832 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
833 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
834 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
835 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
836 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
837 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
838 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
839 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000840 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000841 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000842 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000843 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000844 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000845 case ARMISD::VREV64: return "ARMISD::VREV64";
846 case ARMISD::VREV32: return "ARMISD::VREV32";
847 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000848 case ARMISD::VZIP: return "ARMISD::VZIP";
849 case ARMISD::VUZP: return "ARMISD::VUZP";
850 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000851 case ARMISD::VMULLs: return "ARMISD::VMULLs";
852 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000853 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000854 case ARMISD::FMAX: return "ARMISD::FMAX";
855 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000856 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000857 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
858 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000859 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
860 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
861 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000862 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
863 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
864 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
865 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
866 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
867 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
868 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
869 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
870 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
871 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
872 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
873 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
874 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
875 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
876 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
877 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
878 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000879 }
880}
881
Evan Cheng06b666c2010-05-15 02:18:07 +0000882/// getRegClassFor - Return the register class that should be used for the
883/// specified value type.
884TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
885 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
886 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
887 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000888 if (Subtarget->hasNEON()) {
889 if (VT == MVT::v4i64)
890 return ARM::QQPRRegisterClass;
891 else if (VT == MVT::v8i64)
892 return ARM::QQQQPRRegisterClass;
893 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000894 return TargetLowering::getRegClassFor(VT);
895}
896
Eric Christopherab695882010-07-21 22:26:11 +0000897// Create a fast isel object.
898FastISel *
899ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
900 return ARM::createFastISel(funcInfo);
901}
902
Bill Wendlingb4202b82009-07-01 18:50:55 +0000903/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000904unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000905 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000906}
907
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000908/// getMaximalGlobalOffset - Returns the maximal possible offset which can
909/// be used for loads / stores from the global.
910unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
911 return (Subtarget->isThumb1Only() ? 127 : 4095);
912}
913
Evan Cheng1cc39842010-05-20 23:26:43 +0000914Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000915 unsigned NumVals = N->getNumValues();
916 if (!NumVals)
917 return Sched::RegPressure;
918
919 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000920 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000921 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000922 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000923 if (VT.isFloatingPoint() || VT.isVector())
924 return Sched::Latency;
925 }
Evan Chengc10f5432010-05-28 23:25:23 +0000926
927 if (!N->isMachineOpcode())
928 return Sched::RegPressure;
929
930 // Load are scheduled for latency even if there instruction itinerary
931 // is not available.
932 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
933 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000934
935 if (TID.getNumDefs() == 0)
936 return Sched::RegPressure;
937 if (!Itins->isEmpty() &&
938 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000939 return Sched::Latency;
940
Evan Cheng1cc39842010-05-20 23:26:43 +0000941 return Sched::RegPressure;
942}
943
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000944// FIXME: Move to RegInfo
Evan Cheng31446872010-07-23 22:39:59 +0000945unsigned
946ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
947 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000948 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000949
Evan Cheng31446872010-07-23 22:39:59 +0000950 switch (RC->getID()) {
951 default:
952 return 0;
953 case ARM::tGPRRegClassID:
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000954 return TFI->hasFP(MF) ? 4 : 5;
Evan Chengac096802010-08-10 19:30:19 +0000955 case ARM::GPRRegClassID: {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000956 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
Evan Chengac096802010-08-10 19:30:19 +0000957 return 10 - FP - (Subtarget->isR9Reserved() ? 1 : 0);
958 }
Evan Cheng31446872010-07-23 22:39:59 +0000959 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
960 case ARM::DPRRegClassID:
961 return 32 - 10;
962 }
963}
964
Evan Chenga8e29892007-01-19 07:51:42 +0000965//===----------------------------------------------------------------------===//
966// Lowering Code
967//===----------------------------------------------------------------------===//
968
Evan Chenga8e29892007-01-19 07:51:42 +0000969/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
970static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
971 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000972 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000973 case ISD::SETNE: return ARMCC::NE;
974 case ISD::SETEQ: return ARMCC::EQ;
975 case ISD::SETGT: return ARMCC::GT;
976 case ISD::SETGE: return ARMCC::GE;
977 case ISD::SETLT: return ARMCC::LT;
978 case ISD::SETLE: return ARMCC::LE;
979 case ISD::SETUGT: return ARMCC::HI;
980 case ISD::SETUGE: return ARMCC::HS;
981 case ISD::SETULT: return ARMCC::LO;
982 case ISD::SETULE: return ARMCC::LS;
983 }
984}
985
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000986/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
987static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000988 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000989 CondCode2 = ARMCC::AL;
990 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000991 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000992 case ISD::SETEQ:
993 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
994 case ISD::SETGT:
995 case ISD::SETOGT: CondCode = ARMCC::GT; break;
996 case ISD::SETGE:
997 case ISD::SETOGE: CondCode = ARMCC::GE; break;
998 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000999 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001000 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1001 case ISD::SETO: CondCode = ARMCC::VC; break;
1002 case ISD::SETUO: CondCode = ARMCC::VS; break;
1003 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1004 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1005 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1006 case ISD::SETLT:
1007 case ISD::SETULT: CondCode = ARMCC::LT; break;
1008 case ISD::SETLE:
1009 case ISD::SETULE: CondCode = ARMCC::LE; break;
1010 case ISD::SETNE:
1011 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1012 }
Evan Chenga8e29892007-01-19 07:51:42 +00001013}
1014
Bob Wilson1f595bb2009-04-17 19:07:39 +00001015//===----------------------------------------------------------------------===//
1016// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001017//===----------------------------------------------------------------------===//
1018
1019#include "ARMGenCallingConv.inc"
1020
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001021/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1022/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001023CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001024 bool Return,
1025 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001026 switch (CC) {
1027 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001028 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001029 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001030 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001031 if (!Subtarget->isAAPCS_ABI())
1032 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1033 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1034 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1035 }
1036 // Fallthrough
1037 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001038 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001039 if (!Subtarget->isAAPCS_ABI())
1040 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1041 else if (Subtarget->hasVFP2() &&
1042 FloatABIType == FloatABI::Hard && !isVarArg)
1043 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1044 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1045 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001046 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001047 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001048 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001049 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001050 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001051 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001052 }
1053}
1054
Dan Gohman98ca4f22009-08-05 01:29:28 +00001055/// LowerCallResult - Lower the result values of a call into the
1056/// appropriate copies out of appropriate physical registers.
1057SDValue
1058ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001059 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001060 const SmallVectorImpl<ISD::InputArg> &Ins,
1061 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001062 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001063
Bob Wilson1f595bb2009-04-17 19:07:39 +00001064 // Assign locations to each value returned by this call.
1065 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001066 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001067 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001068 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001069 CCAssignFnForNode(CallConv, /* Return*/ true,
1070 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001071
1072 // Copy all of the result registers out of their specified physreg.
1073 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1074 CCValAssign VA = RVLocs[i];
1075
Bob Wilson80915242009-04-25 00:33:20 +00001076 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001077 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001078 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001080 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001081 Chain = Lo.getValue(1);
1082 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001083 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001084 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001085 InFlag);
1086 Chain = Hi.getValue(1);
1087 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001088 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001089
Owen Anderson825b72b2009-08-11 20:47:22 +00001090 if (VA.getLocVT() == MVT::v2f64) {
1091 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1092 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1093 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001094
1095 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001097 Chain = Lo.getValue(1);
1098 InFlag = Lo.getValue(2);
1099 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001100 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001101 Chain = Hi.getValue(1);
1102 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001103 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001104 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1105 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001106 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001108 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1109 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001110 Chain = Val.getValue(1);
1111 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112 }
Bob Wilson80915242009-04-25 00:33:20 +00001113
1114 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001115 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001116 case CCValAssign::Full: break;
1117 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001118 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001119 break;
1120 }
1121
Dan Gohman98ca4f22009-08-05 01:29:28 +00001122 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001123 }
1124
Dan Gohman98ca4f22009-08-05 01:29:28 +00001125 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126}
1127
1128/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1129/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001130/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131/// a byval function parameter.
1132/// Sometimes what we are copying is the end of a larger object, the part that
1133/// does not fit in registers.
1134static SDValue
1135CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1136 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1137 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001138 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001139 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001140 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001141 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142}
1143
Bob Wilsondee46d72009-04-17 20:35:10 +00001144/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001145SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001146ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1147 SDValue StackPtr, SDValue Arg,
1148 DebugLoc dl, SelectionDAG &DAG,
1149 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001150 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001151 unsigned LocMemOffset = VA.getLocMemOffset();
1152 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1153 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001154 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001155 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001156
Bob Wilson1f595bb2009-04-17 19:07:39 +00001157 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001158 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001159 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001160}
1161
Dan Gohman98ca4f22009-08-05 01:29:28 +00001162void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001163 SDValue Chain, SDValue &Arg,
1164 RegsToPassVector &RegsToPass,
1165 CCValAssign &VA, CCValAssign &NextVA,
1166 SDValue &StackPtr,
1167 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001168 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001169
Jim Grosbache5165492009-11-09 00:11:35 +00001170 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001171 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001172 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1173
1174 if (NextVA.isRegLoc())
1175 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1176 else {
1177 assert(NextVA.isMemLoc());
1178 if (StackPtr.getNode() == 0)
1179 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1180
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1182 dl, DAG, NextVA,
1183 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001184 }
1185}
1186
Dan Gohman98ca4f22009-08-05 01:29:28 +00001187/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001188/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1189/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001191ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001192 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001193 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001195 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001196 const SmallVectorImpl<ISD::InputArg> &Ins,
1197 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001198 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001199 MachineFunction &MF = DAG.getMachineFunction();
1200 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1201 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001202 // Temporarily disable tail calls so things don't break.
1203 if (!EnableARMTailCalls)
1204 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001205 if (isTailCall) {
1206 // Check if it's really possible to do a tail call.
1207 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1208 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001209 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001210 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1211 // detected sibcalls.
1212 if (isTailCall) {
1213 ++NumTailCalls;
1214 IsSibCall = true;
1215 }
1216 }
Evan Chenga8e29892007-01-19 07:51:42 +00001217
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218 // Analyze operands of the call, assigning locations to each operand.
1219 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1221 *DAG.getContext());
1222 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001223 CCAssignFnForNode(CallConv, /* Return*/ false,
1224 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001225
Bob Wilson1f595bb2009-04-17 19:07:39 +00001226 // Get a count of how many bytes are to be pushed on the stack.
1227 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001228
Dale Johannesen51e28e62010-06-03 21:09:53 +00001229 // For tail calls, memory operands are available in our caller's stack.
1230 if (IsSibCall)
1231 NumBytes = 0;
1232
Evan Chenga8e29892007-01-19 07:51:42 +00001233 // Adjust the stack pointer for the new arguments...
1234 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001235 if (!IsSibCall)
1236 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001237
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001238 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001239
Bob Wilson5bafff32009-06-22 23:27:02 +00001240 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001242
Bob Wilson1f595bb2009-04-17 19:07:39 +00001243 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001244 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1246 i != e;
1247 ++i, ++realArgIdx) {
1248 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001249 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001250 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001251
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 // Promote the value if needed.
1253 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001254 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001255 case CCValAssign::Full: break;
1256 case CCValAssign::SExt:
1257 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1258 break;
1259 case CCValAssign::ZExt:
1260 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1261 break;
1262 case CCValAssign::AExt:
1263 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1264 break;
1265 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001266 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001267 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001268 }
1269
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001270 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001271 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 if (VA.getLocVT() == MVT::v2f64) {
1273 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1274 DAG.getConstant(0, MVT::i32));
1275 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1276 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001277
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001279 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1280
1281 VA = ArgLocs[++i]; // skip ahead to next loc
1282 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001284 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1285 } else {
1286 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001287
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1289 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001290 }
1291 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001293 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001294 }
1295 } else if (VA.isRegLoc()) {
1296 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001297 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001298 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001299
Dan Gohman98ca4f22009-08-05 01:29:28 +00001300 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1301 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001302 }
Evan Chenga8e29892007-01-19 07:51:42 +00001303 }
1304
1305 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001306 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001307 &MemOpChains[0], MemOpChains.size());
1308
1309 // Build a sequence of copy-to-reg nodes chained together with token chain
1310 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001311 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001312 // Tail call byval lowering might overwrite argument registers so in case of
1313 // tail call optimization the copies to registers are lowered later.
1314 if (!isTailCall)
1315 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1316 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1317 RegsToPass[i].second, InFlag);
1318 InFlag = Chain.getValue(1);
1319 }
Evan Chenga8e29892007-01-19 07:51:42 +00001320
Dale Johannesen51e28e62010-06-03 21:09:53 +00001321 // For tail calls lower the arguments to the 'real' stack slot.
1322 if (isTailCall) {
1323 // Force all the incoming stack arguments to be loaded from the stack
1324 // before any new outgoing arguments are stored to the stack, because the
1325 // outgoing stack slots may alias the incoming argument stack slots, and
1326 // the alias isn't otherwise explicit. This is slightly more conservative
1327 // than necessary, because it means that each store effectively depends
1328 // on every argument instead of just those arguments it would clobber.
1329
1330 // Do not flag preceeding copytoreg stuff together with the following stuff.
1331 InFlag = SDValue();
1332 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1333 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1334 RegsToPass[i].second, InFlag);
1335 InFlag = Chain.getValue(1);
1336 }
1337 InFlag =SDValue();
1338 }
1339
Bill Wendling056292f2008-09-16 21:48:12 +00001340 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1341 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1342 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001343 bool isDirect = false;
1344 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001345 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001346 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001347
1348 if (EnableARMLongCalls) {
1349 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1350 && "long-calls with non-static relocation model!");
1351 // Handle a global address or an external symbol. If it's not one of
1352 // those, the target's already in a register, so we don't need to do
1353 // anything extra.
1354 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001355 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001356 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001357 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001358 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1359 ARMPCLabelIndex,
1360 ARMCP::CPValue, 0);
1361 // Get the address of the callee into a register
1362 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1363 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1364 Callee = DAG.getLoad(getPointerTy(), dl,
1365 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001366 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001367 false, false, 0);
1368 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1369 const char *Sym = S->getSymbol();
1370
1371 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001372 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001373 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1374 Sym, ARMPCLabelIndex, 0);
1375 // Get the address of the callee into a register
1376 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1377 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1378 Callee = DAG.getLoad(getPointerTy(), dl,
1379 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001380 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001381 false, false, 0);
1382 }
1383 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001384 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001385 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001386 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001387 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001388 getTargetMachine().getRelocationModel() != Reloc::Static;
1389 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001390 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001391 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001392 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001393 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001394 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001395 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001396 ARMPCLabelIndex,
1397 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001398 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001399 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001400 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001401 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001402 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001403 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001404 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001405 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001406 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001407 } else {
1408 // On ELF targets for PIC code, direct calls should go through the PLT
1409 unsigned OpFlags = 0;
1410 if (Subtarget->isTargetELF() &&
1411 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1412 OpFlags = ARMII::MO_PLT;
1413 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1414 }
Bill Wendling056292f2008-09-16 21:48:12 +00001415 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001416 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001417 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001418 getTargetMachine().getRelocationModel() != Reloc::Static;
1419 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001420 // tBX takes a register source operand.
1421 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001422 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001423 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001424 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001425 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001426 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001427 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001428 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001429 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001430 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001431 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001432 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001433 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001434 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001435 } else {
1436 unsigned OpFlags = 0;
1437 // On ELF targets for PIC code, direct calls should go through the PLT
1438 if (Subtarget->isTargetELF() &&
1439 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1440 OpFlags = ARMII::MO_PLT;
1441 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1442 }
Evan Chenga8e29892007-01-19 07:51:42 +00001443 }
1444
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001445 // FIXME: handle tail calls differently.
1446 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001447 if (Subtarget->isThumb()) {
1448 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001449 CallOpc = ARMISD::CALL_NOLINK;
1450 else
1451 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1452 } else {
1453 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001454 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1455 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001456 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001457
Dan Gohman475871a2008-07-27 21:46:04 +00001458 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001459 Ops.push_back(Chain);
1460 Ops.push_back(Callee);
1461
1462 // Add argument registers to the end of the list so that they are known live
1463 // into the call.
1464 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1465 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1466 RegsToPass[i].second.getValueType()));
1467
Gabor Greifba36cb52008-08-28 21:40:38 +00001468 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001469 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001470
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001471 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001472 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001473 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001474
Duncan Sands4bdcb612008-07-02 17:40:58 +00001475 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001476 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001477 InFlag = Chain.getValue(1);
1478
Chris Lattnere563bbc2008-10-11 22:08:30 +00001479 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1480 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001482 InFlag = Chain.getValue(1);
1483
Bob Wilson1f595bb2009-04-17 19:07:39 +00001484 // Handle result values, copying them out of physregs into vregs that we
1485 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001486 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1487 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001488}
1489
Dale Johannesen51e28e62010-06-03 21:09:53 +00001490/// MatchingStackOffset - Return true if the given stack call argument is
1491/// already available in the same position (relatively) of the caller's
1492/// incoming argument stack.
1493static
1494bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1495 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1496 const ARMInstrInfo *TII) {
1497 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1498 int FI = INT_MAX;
1499 if (Arg.getOpcode() == ISD::CopyFromReg) {
1500 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001501 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001502 return false;
1503 MachineInstr *Def = MRI->getVRegDef(VR);
1504 if (!Def)
1505 return false;
1506 if (!Flags.isByVal()) {
1507 if (!TII->isLoadFromStackSlot(Def, FI))
1508 return false;
1509 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001510 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001511 }
1512 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1513 if (Flags.isByVal())
1514 // ByVal argument is passed in as a pointer but it's now being
1515 // dereferenced. e.g.
1516 // define @foo(%struct.X* %A) {
1517 // tail call @bar(%struct.X* byval %A)
1518 // }
1519 return false;
1520 SDValue Ptr = Ld->getBasePtr();
1521 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1522 if (!FINode)
1523 return false;
1524 FI = FINode->getIndex();
1525 } else
1526 return false;
1527
1528 assert(FI != INT_MAX);
1529 if (!MFI->isFixedObjectIndex(FI))
1530 return false;
1531 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1532}
1533
1534/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1535/// for tail call optimization. Targets which want to do tail call
1536/// optimization should implement this function.
1537bool
1538ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1539 CallingConv::ID CalleeCC,
1540 bool isVarArg,
1541 bool isCalleeStructRet,
1542 bool isCallerStructRet,
1543 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001544 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001545 const SmallVectorImpl<ISD::InputArg> &Ins,
1546 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001547 const Function *CallerF = DAG.getMachineFunction().getFunction();
1548 CallingConv::ID CallerCC = CallerF->getCallingConv();
1549 bool CCMatch = CallerCC == CalleeCC;
1550
1551 // Look for obvious safe cases to perform tail call optimization that do not
1552 // require ABI changes. This is what gcc calls sibcall.
1553
Jim Grosbach7616b642010-06-16 23:45:49 +00001554 // Do not sibcall optimize vararg calls unless the call site is not passing
1555 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001556 if (isVarArg && !Outs.empty())
1557 return false;
1558
1559 // Also avoid sibcall optimization if either caller or callee uses struct
1560 // return semantics.
1561 if (isCalleeStructRet || isCallerStructRet)
1562 return false;
1563
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001564 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001565 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001566 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1567 // LR. This means if we need to reload LR, it takes an extra instructions,
1568 // which outweighs the value of the tail call; but here we don't know yet
1569 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001570 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001571 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001572
1573 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1574 // but we need to make sure there are enough registers; the only valid
1575 // registers are the 4 used for parameters. We don't currently do this
1576 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001577 if (Subtarget->isThumb1Only())
1578 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001579
Dale Johannesen51e28e62010-06-03 21:09:53 +00001580 // If the calling conventions do not match, then we'd better make sure the
1581 // results are returned in the same way as what the caller expects.
1582 if (!CCMatch) {
1583 SmallVector<CCValAssign, 16> RVLocs1;
1584 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1585 RVLocs1, *DAG.getContext());
1586 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1587
1588 SmallVector<CCValAssign, 16> RVLocs2;
1589 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1590 RVLocs2, *DAG.getContext());
1591 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1592
1593 if (RVLocs1.size() != RVLocs2.size())
1594 return false;
1595 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1596 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1597 return false;
1598 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1599 return false;
1600 if (RVLocs1[i].isRegLoc()) {
1601 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1602 return false;
1603 } else {
1604 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1605 return false;
1606 }
1607 }
1608 }
1609
1610 // If the callee takes no arguments then go on to check the results of the
1611 // call.
1612 if (!Outs.empty()) {
1613 // Check if stack adjustment is needed. For now, do not do this if any
1614 // argument is passed on the stack.
1615 SmallVector<CCValAssign, 16> ArgLocs;
1616 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1617 ArgLocs, *DAG.getContext());
1618 CCInfo.AnalyzeCallOperands(Outs,
1619 CCAssignFnForNode(CalleeCC, false, isVarArg));
1620 if (CCInfo.getNextStackOffset()) {
1621 MachineFunction &MF = DAG.getMachineFunction();
1622
1623 // Check if the arguments are already laid out in the right way as
1624 // the caller's fixed stack objects.
1625 MachineFrameInfo *MFI = MF.getFrameInfo();
1626 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1627 const ARMInstrInfo *TII =
1628 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001629 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1630 i != e;
1631 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001632 CCValAssign &VA = ArgLocs[i];
1633 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001634 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001635 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636 if (VA.getLocInfo() == CCValAssign::Indirect)
1637 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001638 if (VA.needsCustom()) {
1639 // f64 and vector types are split into multiple registers or
1640 // register/stack-slot combinations. The types will not match
1641 // the registers; give up on memory f64 refs until we figure
1642 // out what to do about this.
1643 if (!VA.isRegLoc())
1644 return false;
1645 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001646 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001647 if (RegVT == MVT::v2f64) {
1648 if (!ArgLocs[++i].isRegLoc())
1649 return false;
1650 if (!ArgLocs[++i].isRegLoc())
1651 return false;
1652 }
1653 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001654 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1655 MFI, MRI, TII))
1656 return false;
1657 }
1658 }
1659 }
1660 }
1661
1662 return true;
1663}
1664
Dan Gohman98ca4f22009-08-05 01:29:28 +00001665SDValue
1666ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001667 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001668 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001669 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001670 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001671
Bob Wilsondee46d72009-04-17 20:35:10 +00001672 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001673 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001674
Bob Wilsondee46d72009-04-17 20:35:10 +00001675 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001676 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1677 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001678
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001680 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1681 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001682
1683 // If this is the first return lowered for this function, add
1684 // the regs to the liveout set for the function.
1685 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1686 for (unsigned i = 0; i != RVLocs.size(); ++i)
1687 if (RVLocs[i].isRegLoc())
1688 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001689 }
1690
Bob Wilson1f595bb2009-04-17 19:07:39 +00001691 SDValue Flag;
1692
1693 // Copy the result values into the output registers.
1694 for (unsigned i = 0, realRVLocIdx = 0;
1695 i != RVLocs.size();
1696 ++i, ++realRVLocIdx) {
1697 CCValAssign &VA = RVLocs[i];
1698 assert(VA.isRegLoc() && "Can only return in registers!");
1699
Dan Gohmanc9403652010-07-07 15:54:55 +00001700 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001701
1702 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001703 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001704 case CCValAssign::Full: break;
1705 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001706 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001707 break;
1708 }
1709
Bob Wilson1f595bb2009-04-17 19:07:39 +00001710 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001712 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1714 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001715 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001717
1718 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1719 Flag = Chain.getValue(1);
1720 VA = RVLocs[++i]; // skip ahead to next loc
1721 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1722 HalfGPRs.getValue(1), Flag);
1723 Flag = Chain.getValue(1);
1724 VA = RVLocs[++i]; // skip ahead to next loc
1725
1726 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001727 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1728 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001729 }
1730 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1731 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001732 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001733 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001734 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001735 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001736 VA = RVLocs[++i]; // skip ahead to next loc
1737 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1738 Flag);
1739 } else
1740 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1741
Bob Wilsondee46d72009-04-17 20:35:10 +00001742 // Guarantee that all emitted copies are
1743 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001744 Flag = Chain.getValue(1);
1745 }
1746
1747 SDValue result;
1748 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001749 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001750 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001751 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001752
1753 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001754}
1755
Evan Cheng3d2125c2010-11-30 23:55:39 +00001756bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1757 if (N->getNumValues() != 1)
1758 return false;
1759 if (!N->hasNUsesOfValue(1, 0))
1760 return false;
1761
1762 unsigned NumCopies = 0;
1763 SDNode* Copies[2];
1764 SDNode *Use = *N->use_begin();
1765 if (Use->getOpcode() == ISD::CopyToReg) {
1766 Copies[NumCopies++] = Use;
1767 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1768 // f64 returned in a pair of GPRs.
1769 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1770 UI != UE; ++UI) {
1771 if (UI->getOpcode() != ISD::CopyToReg)
1772 return false;
1773 Copies[UI.getUse().getResNo()] = *UI;
1774 ++NumCopies;
1775 }
1776 } else if (Use->getOpcode() == ISD::BITCAST) {
1777 // f32 returned in a single GPR.
1778 if (!Use->hasNUsesOfValue(1, 0))
1779 return false;
1780 Use = *Use->use_begin();
1781 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1782 return false;
1783 Copies[NumCopies++] = Use;
1784 } else {
1785 return false;
1786 }
1787
1788 if (NumCopies != 1 && NumCopies != 2)
1789 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001790
1791 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001792 for (unsigned i = 0; i < NumCopies; ++i) {
1793 SDNode *Copy = Copies[i];
1794 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1795 UI != UE; ++UI) {
1796 if (UI->getOpcode() == ISD::CopyToReg) {
1797 SDNode *Use = *UI;
1798 if (Use == Copies[0] || Use == Copies[1])
1799 continue;
1800 return false;
1801 }
1802 if (UI->getOpcode() != ARMISD::RET_FLAG)
1803 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001804 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001805 }
1806 }
1807
Evan Cheng1bf891a2010-12-01 22:59:46 +00001808 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001809}
1810
Bob Wilsonb62d2572009-11-03 00:02:05 +00001811// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1812// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1813// one of the above mentioned nodes. It has to be wrapped because otherwise
1814// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1815// be used to form addressing mode. These wrapped nodes will be selected
1816// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001817static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001818 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001819 // FIXME there is no actual debug info here
1820 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001821 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001822 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001823 if (CP->isMachineConstantPoolEntry())
1824 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1825 CP->getAlignment());
1826 else
1827 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1828 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001829 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001830}
1831
Jim Grosbache1102ca2010-07-19 17:20:38 +00001832unsigned ARMTargetLowering::getJumpTableEncoding() const {
1833 return MachineJumpTableInfo::EK_Inline;
1834}
1835
Dan Gohmand858e902010-04-17 15:26:15 +00001836SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1837 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001838 MachineFunction &MF = DAG.getMachineFunction();
1839 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1840 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001841 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001842 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001843 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001844 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1845 SDValue CPAddr;
1846 if (RelocM == Reloc::Static) {
1847 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1848 } else {
1849 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001850 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001851 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1852 ARMCP::CPBlockAddress,
1853 PCAdj);
1854 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1855 }
1856 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1857 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001858 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001859 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001860 if (RelocM == Reloc::Static)
1861 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001862 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001863 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001864}
1865
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001866// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001867SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001868ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001869 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001870 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001871 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001872 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001873 MachineFunction &MF = DAG.getMachineFunction();
1874 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001875 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001876 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001877 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001878 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001879 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001880 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001881 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001882 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001883 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001884 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001885
Evan Chenge7e0d622009-11-06 22:24:13 +00001886 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001887 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001888
1889 // call __tls_get_addr.
1890 ArgListTy Args;
1891 ArgListEntry Entry;
1892 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001893 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001894 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001895 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001896 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001897 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1898 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001899 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001900 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001901 return CallResult.first;
1902}
1903
1904// Lower ISD::GlobalTLSAddress using the "initial exec" or
1905// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001906SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001907ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001908 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001909 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001910 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001911 SDValue Offset;
1912 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001913 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001914 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001915 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001916
Chris Lattner4fb63d02009-07-15 04:12:33 +00001917 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001918 MachineFunction &MF = DAG.getMachineFunction();
1919 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001920 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001921 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001922 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1923 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001924 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001925 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001926 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001928 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001929 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001930 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001931 Chain = Offset.getValue(1);
1932
Evan Chenge7e0d622009-11-06 22:24:13 +00001933 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001934 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001935
Evan Cheng9eda6892009-10-31 03:39:36 +00001936 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001937 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001938 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001939 } else {
1940 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001941 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001942 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001943 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001944 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001945 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001946 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001947 }
1948
1949 // The address of the thread local variable is the add of the thread
1950 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001951 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001952}
1953
Dan Gohman475871a2008-07-27 21:46:04 +00001954SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001955ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001956 // TODO: implement the "local dynamic" model
1957 assert(Subtarget->isTargetELF() &&
1958 "TLS not implemented for non-ELF targets");
1959 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1960 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1961 // otherwise use the "Local Exec" TLS Model
1962 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1963 return LowerToTLSGeneralDynamicModel(GA, DAG);
1964 else
1965 return LowerToTLSExecModels(GA, DAG);
1966}
1967
Dan Gohman475871a2008-07-27 21:46:04 +00001968SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001969 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001970 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001971 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001972 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001973 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1974 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001975 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001976 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001977 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001978 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001980 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001981 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001982 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001983 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001984 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001985 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001986 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001987 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001988 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001989 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001990 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001991 }
1992
1993 // If we have T2 ops, we can materialize the address directly via movt/movw
1994 // pair. This is always cheaper.
1995 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00001996 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001997 // FIXME: Once remat is capable of dealing with instructions with register
1998 // operands, expand this into two nodes.
1999 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2000 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002001 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002002 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2003 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2004 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2005 MachinePointerInfo::getConstantPool(),
2006 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002007 }
2008}
2009
Dan Gohman475871a2008-07-27 21:46:04 +00002010SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002011 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002012 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002013 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002014 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002015 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002016 MachineFunction &MF = DAG.getMachineFunction();
2017 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2018
2019 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002020 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002021 // FIXME: Once remat is capable of dealing with instructions with register
2022 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002023 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002024 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2025 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2026
Evan Cheng53519f02011-01-21 18:55:51 +00002027 unsigned Wrapper = (RelocM == Reloc::PIC_)
2028 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2029 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002030 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002031 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2032 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2033 MachinePointerInfo::getGOT(), false, false, 0);
2034 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002035 }
2036
2037 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002038 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002039 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002040 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002041 } else {
2042 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002043 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2044 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002045 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002046 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002047 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002049
Evan Cheng9eda6892009-10-31 03:39:36 +00002050 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002051 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002052 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002053 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002054
2055 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002056 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002057 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002058 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002059
Evan Cheng63476a82009-09-03 07:04:02 +00002060 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002061 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002062 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002063
2064 return Result;
2065}
2066
Dan Gohman475871a2008-07-27 21:46:04 +00002067SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002068 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002069 assert(Subtarget->isTargetELF() &&
2070 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002071 MachineFunction &MF = DAG.getMachineFunction();
2072 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002073 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002074 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002075 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002076 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002077 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2078 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002079 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002080 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002081 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002082 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002083 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002084 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002085 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002086 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002087}
2088
Jim Grosbach0e0da732009-05-12 23:59:14 +00002089SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002090ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2091 const {
2092 DebugLoc dl = Op.getDebugLoc();
2093 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2094 Op.getOperand(0), Op.getOperand(1));
2095}
2096
2097SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002098ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2099 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002100 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002101 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2102 Op.getOperand(1), Val);
2103}
2104
2105SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002106ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2107 DebugLoc dl = Op.getDebugLoc();
2108 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2109 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2110}
2111
2112SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002113ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002114 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002115 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002116 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002117 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002118 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002119 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002120 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002121 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2122 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002123 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002124 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002125 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002126 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002127 EVT PtrVT = getPointerTy();
2128 DebugLoc dl = Op.getDebugLoc();
2129 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2130 SDValue CPAddr;
2131 unsigned PCAdj = (RelocM != Reloc::PIC_)
2132 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002133 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002134 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2135 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002136 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002137 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002138 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002139 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002140 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002141 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002142
2143 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002144 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002145 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2146 }
2147 return Result;
2148 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002149 }
2150}
2151
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002152static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002153 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002154 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002155 if (!Subtarget->hasDataBarrier()) {
2156 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2157 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2158 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002159 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002160 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002161 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002162 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002163 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002164
2165 SDValue Op5 = Op.getOperand(5);
2166 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2167 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2168 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2169 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2170
2171 ARM_MB::MemBOpt DMBOpt;
2172 if (isDeviceBarrier)
2173 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2174 else
2175 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2176 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2177 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002178}
2179
Evan Chengdfed19f2010-11-03 06:34:55 +00002180static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2181 const ARMSubtarget *Subtarget) {
2182 // ARM pre v5TE and Thumb1 does not have preload instructions.
2183 if (!(Subtarget->isThumb2() ||
2184 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2185 // Just preserve the chain.
2186 return Op.getOperand(0);
2187
2188 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002189 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2190 if (!isRead &&
2191 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2192 // ARMv7 with MP extension has PLDW.
2193 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002194
2195 if (Subtarget->isThumb())
2196 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002197 isRead = ~isRead & 1;
2198 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002199
Evan Cheng416941d2010-11-04 05:19:35 +00002200 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002201 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002202 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2203 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002204}
2205
Dan Gohman1e93df62010-04-17 14:41:14 +00002206static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2207 MachineFunction &MF = DAG.getMachineFunction();
2208 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2209
Evan Chenga8e29892007-01-19 07:51:42 +00002210 // vastart just stores the address of the VarArgsFrameIndex slot into the
2211 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002212 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002213 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002214 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002215 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002216 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2217 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002218}
2219
Dan Gohman475871a2008-07-27 21:46:04 +00002220SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002221ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2222 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002223 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002224 MachineFunction &MF = DAG.getMachineFunction();
2225 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2226
2227 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002228 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002229 RC = ARM::tGPRRegisterClass;
2230 else
2231 RC = ARM::GPRRegisterClass;
2232
2233 // Transform the arguments stored in physical registers into virtual ones.
Devang Patele9a7ea62011-01-31 21:38:14 +00002234 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002235 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002236
2237 SDValue ArgValue2;
2238 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002239 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002240 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002241
2242 // Create load node to retrieve arguments from the stack.
2243 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002244 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002245 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002246 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002247 } else {
Devang Patele9a7ea62011-01-31 21:38:14 +00002248 Reg = MF.addLiveIn(NextVA.getLocReg(), RC, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002249 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002250 }
2251
Jim Grosbache5165492009-11-09 00:11:35 +00002252 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002253}
2254
2255SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002256ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002257 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002258 const SmallVectorImpl<ISD::InputArg>
2259 &Ins,
2260 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002261 SmallVectorImpl<SDValue> &InVals)
2262 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002263
Bob Wilson1f595bb2009-04-17 19:07:39 +00002264 MachineFunction &MF = DAG.getMachineFunction();
2265 MachineFrameInfo *MFI = MF.getFrameInfo();
2266
Bob Wilson1f595bb2009-04-17 19:07:39 +00002267 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2268
2269 // Assign locations to all of the incoming arguments.
2270 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002271 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2272 *DAG.getContext());
2273 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002274 CCAssignFnForNode(CallConv, /* Return*/ false,
2275 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002276
2277 SmallVector<SDValue, 16> ArgValues;
2278
2279 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2280 CCValAssign &VA = ArgLocs[i];
2281
Bob Wilsondee46d72009-04-17 20:35:10 +00002282 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002283 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002284 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002285
Bob Wilson5bafff32009-06-22 23:27:02 +00002286 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002287 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002288 // f64 and vector types are split up into multiple registers or
2289 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002290 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002291 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002292 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002293 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002294 SDValue ArgValue2;
2295 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002296 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002297 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2298 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002299 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002300 false, false, 0);
2301 } else {
2302 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2303 Chain, DAG, dl);
2304 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002305 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2306 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002307 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002308 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002309 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2310 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002311 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002312
Bob Wilson5bafff32009-06-22 23:27:02 +00002313 } else {
2314 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002315
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002317 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002318 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002319 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002320 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002321 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002322 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002323 RC = (AFI->isThumb1OnlyFunction() ?
2324 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002325 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002326 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002327
2328 // Transform the arguments in physical registers into virtual ones.
Devang Patele9a7ea62011-01-31 21:38:14 +00002329 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002330 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002331 }
2332
2333 // If this is an 8 or 16-bit value, it is really passed promoted
2334 // to 32 bits. Insert an assert[sz]ext to capture this, then
2335 // truncate to the right size.
2336 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002337 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002338 case CCValAssign::Full: break;
2339 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002340 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002341 break;
2342 case CCValAssign::SExt:
2343 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2344 DAG.getValueType(VA.getValVT()));
2345 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2346 break;
2347 case CCValAssign::ZExt:
2348 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2349 DAG.getValueType(VA.getValVT()));
2350 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2351 break;
2352 }
2353
Dan Gohman98ca4f22009-08-05 01:29:28 +00002354 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002355
2356 } else { // VA.isRegLoc()
2357
2358 // sanity check
2359 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002360 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002361
2362 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002363 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002364
Bob Wilsondee46d72009-04-17 20:35:10 +00002365 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002366 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002367 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002368 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002369 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002370 }
2371 }
2372
2373 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002374 if (isVarArg) {
2375 static const unsigned GPRArgRegs[] = {
2376 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2377 };
2378
Bob Wilsondee46d72009-04-17 20:35:10 +00002379 unsigned NumGPRs = CCInfo.getFirstUnallocated
2380 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002381
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002382 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002383 unsigned VARegSize = (4 - NumGPRs) * 4;
2384 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002385 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002386 if (VARegSaveSize) {
2387 // If this function is vararg, store any remaining integer argument regs
2388 // to their spots on the stack so that they may be loaded by deferencing
2389 // the result of va_next.
2390 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002391 AFI->setVarArgsFrameIndex(
2392 MFI->CreateFixedObject(VARegSaveSize,
2393 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002394 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002395 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2396 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002397
Dan Gohman475871a2008-07-27 21:46:04 +00002398 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002399 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002400 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002401 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002402 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002403 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002404 RC = ARM::GPRRegisterClass;
2405
Devang Patele9a7ea62011-01-31 21:38:14 +00002406 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002407 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002408 SDValue Store =
2409 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002410 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2411 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002412 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002413 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002414 DAG.getConstant(4, getPointerTy()));
2415 }
2416 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002417 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002418 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002419 } else
2420 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002421 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002422 }
2423
Dan Gohman98ca4f22009-08-05 01:29:28 +00002424 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002425}
2426
2427/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002428static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002429 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002430 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002431 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002432 // Maybe this has already been legalized into the constant pool?
2433 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002434 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002435 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002436 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002437 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002438 }
2439 }
2440 return false;
2441}
2442
Evan Chenga8e29892007-01-19 07:51:42 +00002443/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2444/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002445SDValue
2446ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002447 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002448 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002449 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002450 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002451 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002452 // Constant does not fit, try adjusting it by one?
2453 switch (CC) {
2454 default: break;
2455 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002456 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002457 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002458 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002459 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002460 }
2461 break;
2462 case ISD::SETULT:
2463 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002464 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002465 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002466 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002467 }
2468 break;
2469 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002470 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002471 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002472 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002473 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002474 }
2475 break;
2476 case ISD::SETULE:
2477 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002478 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002479 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002480 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002481 }
2482 break;
2483 }
2484 }
2485 }
2486
2487 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002488 ARMISD::NodeType CompareType;
2489 switch (CondCode) {
2490 default:
2491 CompareType = ARMISD::CMP;
2492 break;
2493 case ARMCC::EQ:
2494 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002495 // Uses only Z Flag
2496 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002497 break;
2498 }
Evan Cheng218977b2010-07-13 19:27:42 +00002499 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002500 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002501}
2502
2503/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002504SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002505ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002506 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002507 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002508 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002509 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002510 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002511 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2512 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002513}
2514
Bill Wendlingde2b1512010-08-11 08:43:16 +00002515SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2516 SDValue Cond = Op.getOperand(0);
2517 SDValue SelectTrue = Op.getOperand(1);
2518 SDValue SelectFalse = Op.getOperand(2);
2519 DebugLoc dl = Op.getDebugLoc();
2520
2521 // Convert:
2522 //
2523 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2524 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2525 //
2526 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2527 const ConstantSDNode *CMOVTrue =
2528 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2529 const ConstantSDNode *CMOVFalse =
2530 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2531
2532 if (CMOVTrue && CMOVFalse) {
2533 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2534 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2535
2536 SDValue True;
2537 SDValue False;
2538 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2539 True = SelectTrue;
2540 False = SelectFalse;
2541 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2542 True = SelectFalse;
2543 False = SelectTrue;
2544 }
2545
2546 if (True.getNode() && False.getNode()) {
2547 EVT VT = Cond.getValueType();
2548 SDValue ARMcc = Cond.getOperand(2);
2549 SDValue CCR = Cond.getOperand(3);
2550 SDValue Cmp = Cond.getOperand(4);
2551 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2552 }
2553 }
2554 }
2555
2556 return DAG.getSelectCC(dl, Cond,
2557 DAG.getConstant(0, Cond.getValueType()),
2558 SelectTrue, SelectFalse, ISD::SETNE);
2559}
2560
Dan Gohmand858e902010-04-17 15:26:15 +00002561SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002562 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002563 SDValue LHS = Op.getOperand(0);
2564 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002565 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002566 SDValue TrueVal = Op.getOperand(2);
2567 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002568 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002569
Owen Anderson825b72b2009-08-11 20:47:22 +00002570 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002571 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002572 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002573 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2574 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002575 }
2576
2577 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002578 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002579
Evan Cheng218977b2010-07-13 19:27:42 +00002580 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2581 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002582 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002583 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002584 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002585 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002586 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002587 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002588 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002589 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002590 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002591 }
2592 return Result;
2593}
2594
Evan Cheng218977b2010-07-13 19:27:42 +00002595/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2596/// to morph to an integer compare sequence.
2597static bool canChangeToInt(SDValue Op, bool &SeenZero,
2598 const ARMSubtarget *Subtarget) {
2599 SDNode *N = Op.getNode();
2600 if (!N->hasOneUse())
2601 // Otherwise it requires moving the value from fp to integer registers.
2602 return false;
2603 if (!N->getNumValues())
2604 return false;
2605 EVT VT = Op.getValueType();
2606 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2607 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2608 // vmrs are very slow, e.g. cortex-a8.
2609 return false;
2610
2611 if (isFloatingPointZero(Op)) {
2612 SeenZero = true;
2613 return true;
2614 }
2615 return ISD::isNormalLoad(N);
2616}
2617
2618static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2619 if (isFloatingPointZero(Op))
2620 return DAG.getConstant(0, MVT::i32);
2621
2622 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2623 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002624 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002625 Ld->isVolatile(), Ld->isNonTemporal(),
2626 Ld->getAlignment());
2627
2628 llvm_unreachable("Unknown VFP cmp argument!");
2629}
2630
2631static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2632 SDValue &RetVal1, SDValue &RetVal2) {
2633 if (isFloatingPointZero(Op)) {
2634 RetVal1 = DAG.getConstant(0, MVT::i32);
2635 RetVal2 = DAG.getConstant(0, MVT::i32);
2636 return;
2637 }
2638
2639 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2640 SDValue Ptr = Ld->getBasePtr();
2641 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2642 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002643 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002644 Ld->isVolatile(), Ld->isNonTemporal(),
2645 Ld->getAlignment());
2646
2647 EVT PtrType = Ptr.getValueType();
2648 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2649 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2650 PtrType, Ptr, DAG.getConstant(4, PtrType));
2651 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2652 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002653 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002654 Ld->isVolatile(), Ld->isNonTemporal(),
2655 NewAlign);
2656 return;
2657 }
2658
2659 llvm_unreachable("Unknown VFP cmp argument!");
2660}
2661
2662/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2663/// f32 and even f64 comparisons to integer ones.
2664SDValue
2665ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2666 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002667 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002668 SDValue LHS = Op.getOperand(2);
2669 SDValue RHS = Op.getOperand(3);
2670 SDValue Dest = Op.getOperand(4);
2671 DebugLoc dl = Op.getDebugLoc();
2672
2673 bool SeenZero = false;
2674 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2675 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002676 // If one of the operand is zero, it's safe to ignore the NaN case since
2677 // we only care about equality comparisons.
2678 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002679 // If unsafe fp math optimization is enabled and there are no othter uses of
2680 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2681 // to an integer comparison.
2682 if (CC == ISD::SETOEQ)
2683 CC = ISD::SETEQ;
2684 else if (CC == ISD::SETUNE)
2685 CC = ISD::SETNE;
2686
2687 SDValue ARMcc;
2688 if (LHS.getValueType() == MVT::f32) {
2689 LHS = bitcastf32Toi32(LHS, DAG);
2690 RHS = bitcastf32Toi32(RHS, DAG);
2691 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2692 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2693 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2694 Chain, Dest, ARMcc, CCR, Cmp);
2695 }
2696
2697 SDValue LHS1, LHS2;
2698 SDValue RHS1, RHS2;
2699 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2700 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2701 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2702 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002703 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002704 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2705 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2706 }
2707
2708 return SDValue();
2709}
2710
2711SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2712 SDValue Chain = Op.getOperand(0);
2713 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2714 SDValue LHS = Op.getOperand(2);
2715 SDValue RHS = Op.getOperand(3);
2716 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002717 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002718
Owen Anderson825b72b2009-08-11 20:47:22 +00002719 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002720 SDValue ARMcc;
2721 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002722 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002723 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002724 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002725 }
2726
Owen Anderson825b72b2009-08-11 20:47:22 +00002727 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002728
2729 if (UnsafeFPMath &&
2730 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2731 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2732 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2733 if (Result.getNode())
2734 return Result;
2735 }
2736
Evan Chenga8e29892007-01-19 07:51:42 +00002737 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002738 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002739
Evan Cheng218977b2010-07-13 19:27:42 +00002740 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2741 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002742 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002743 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002744 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002745 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002746 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002747 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2748 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002749 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002750 }
2751 return Res;
2752}
2753
Dan Gohmand858e902010-04-17 15:26:15 +00002754SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002755 SDValue Chain = Op.getOperand(0);
2756 SDValue Table = Op.getOperand(1);
2757 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002758 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002759
Owen Andersone50ed302009-08-10 22:56:29 +00002760 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002761 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2762 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002763 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002764 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002765 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002766 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2767 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002768 if (Subtarget->isThumb2()) {
2769 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2770 // which does another jump to the destination. This also makes it easier
2771 // to translate it to TBB / TBH later.
2772 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002773 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002774 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002775 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002776 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002777 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002778 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002779 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002780 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002781 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002782 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002783 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002784 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002785 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002786 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002787 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002788 }
Evan Chenga8e29892007-01-19 07:51:42 +00002789}
2790
Bob Wilson76a312b2010-03-19 22:51:32 +00002791static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2792 DebugLoc dl = Op.getDebugLoc();
2793 unsigned Opc;
2794
2795 switch (Op.getOpcode()) {
2796 default:
2797 assert(0 && "Invalid opcode!");
2798 case ISD::FP_TO_SINT:
2799 Opc = ARMISD::FTOSI;
2800 break;
2801 case ISD::FP_TO_UINT:
2802 Opc = ARMISD::FTOUI;
2803 break;
2804 }
2805 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002806 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002807}
2808
2809static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2810 EVT VT = Op.getValueType();
2811 DebugLoc dl = Op.getDebugLoc();
2812 unsigned Opc;
2813
2814 switch (Op.getOpcode()) {
2815 default:
2816 assert(0 && "Invalid opcode!");
2817 case ISD::SINT_TO_FP:
2818 Opc = ARMISD::SITOF;
2819 break;
2820 case ISD::UINT_TO_FP:
2821 Opc = ARMISD::UITOF;
2822 break;
2823 }
2824
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002825 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002826 return DAG.getNode(Opc, dl, VT, Op);
2827}
2828
Evan Cheng515fe3a2010-07-08 02:08:50 +00002829SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002830 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002831 SDValue Tmp0 = Op.getOperand(0);
2832 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002833 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002834 EVT VT = Op.getValueType();
2835 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002836 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002837 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002838 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002839 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002840 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002841 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002842}
2843
Evan Cheng2457f2c2010-05-22 01:47:14 +00002844SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2845 MachineFunction &MF = DAG.getMachineFunction();
2846 MachineFrameInfo *MFI = MF.getFrameInfo();
2847 MFI->setReturnAddressIsTaken(true);
2848
2849 EVT VT = Op.getValueType();
2850 DebugLoc dl = Op.getDebugLoc();
2851 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2852 if (Depth) {
2853 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2854 SDValue Offset = DAG.getConstant(4, MVT::i32);
2855 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2856 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002857 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002858 }
2859
2860 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patele9a7ea62011-01-31 21:38:14 +00002861 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32), dl);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002862 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2863}
2864
Dan Gohmand858e902010-04-17 15:26:15 +00002865SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002866 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2867 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002868
Owen Andersone50ed302009-08-10 22:56:29 +00002869 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002870 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2871 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002872 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002873 ? ARM::R7 : ARM::R11;
2874 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2875 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002876 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
2877 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00002878 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002879 return FrameAddr;
2880}
2881
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002882/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00002883/// expand a bit convert where either the source or destination type is i64 to
2884/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2885/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2886/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002887static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002888 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2889 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002890 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002891
Bob Wilson9f3f0612010-04-17 05:30:19 +00002892 // This function is only supposed to be called for i64 types, either as the
2893 // source or destination of the bit convert.
2894 EVT SrcVT = Op.getValueType();
2895 EVT DstVT = N->getValueType(0);
2896 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002897 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002898
Bob Wilson9f3f0612010-04-17 05:30:19 +00002899 // Turn i64->f64 into VMOVDRR.
2900 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002901 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2902 DAG.getConstant(0, MVT::i32));
2903 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2904 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002905 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00002906 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002907 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002908
Jim Grosbache5165492009-11-09 00:11:35 +00002909 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002910 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2911 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2912 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2913 // Merge the pieces into a single i64 value.
2914 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2915 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002916
Bob Wilson9f3f0612010-04-17 05:30:19 +00002917 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002918}
2919
Bob Wilson5bafff32009-06-22 23:27:02 +00002920/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002921/// Zero vectors are used to represent vector negation and in those cases
2922/// will be implemented with the NEON VNEG instruction. However, VNEG does
2923/// not support i64 elements, so sometimes the zero vectors will need to be
2924/// explicitly constructed. Regardless, use a canonical VMOV to create the
2925/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002926static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002927 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002928 // The canonical modified immediate encoding of a zero vector is....0!
2929 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2930 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2931 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002932 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002933}
2934
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002935/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2936/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002937SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2938 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002939 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2940 EVT VT = Op.getValueType();
2941 unsigned VTBits = VT.getSizeInBits();
2942 DebugLoc dl = Op.getDebugLoc();
2943 SDValue ShOpLo = Op.getOperand(0);
2944 SDValue ShOpHi = Op.getOperand(1);
2945 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002946 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002947 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002948
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002949 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2950
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002951 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2952 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2953 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2954 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2955 DAG.getConstant(VTBits, MVT::i32));
2956 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2957 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002958 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002959
2960 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2961 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002962 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002963 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002964 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002965 CCR, Cmp);
2966
2967 SDValue Ops[2] = { Lo, Hi };
2968 return DAG.getMergeValues(Ops, 2, dl);
2969}
2970
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002971/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2972/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002973SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2974 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002975 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2976 EVT VT = Op.getValueType();
2977 unsigned VTBits = VT.getSizeInBits();
2978 DebugLoc dl = Op.getDebugLoc();
2979 SDValue ShOpLo = Op.getOperand(0);
2980 SDValue ShOpHi = Op.getOperand(1);
2981 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002982 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002983
2984 assert(Op.getOpcode() == ISD::SHL_PARTS);
2985 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2986 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2987 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2988 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2989 DAG.getConstant(VTBits, MVT::i32));
2990 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2991 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2992
2993 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2994 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2995 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002996 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002997 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002998 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002999 CCR, Cmp);
3000
3001 SDValue Ops[2] = { Lo, Hi };
3002 return DAG.getMergeValues(Ops, 2, dl);
3003}
3004
Jim Grosbach4725ca72010-09-08 03:54:02 +00003005SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003006 SelectionDAG &DAG) const {
3007 // The rounding mode is in bits 23:22 of the FPSCR.
3008 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3009 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3010 // so that the shift + and get folded into a bitfield extract.
3011 DebugLoc dl = Op.getDebugLoc();
3012 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3013 DAG.getConstant(Intrinsic::arm_get_fpscr,
3014 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003015 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003016 DAG.getConstant(1U << 22, MVT::i32));
3017 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3018 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003019 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003020 DAG.getConstant(3, MVT::i32));
3021}
3022
Jim Grosbach3482c802010-01-18 19:58:49 +00003023static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3024 const ARMSubtarget *ST) {
3025 EVT VT = N->getValueType(0);
3026 DebugLoc dl = N->getDebugLoc();
3027
3028 if (!ST->hasV6T2Ops())
3029 return SDValue();
3030
3031 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3032 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3033}
3034
Bob Wilson5bafff32009-06-22 23:27:02 +00003035static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3036 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003037 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003038 DebugLoc dl = N->getDebugLoc();
3039
Bob Wilsond5448bb2010-11-18 21:16:28 +00003040 if (!VT.isVector())
3041 return SDValue();
3042
Bob Wilson5bafff32009-06-22 23:27:02 +00003043 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003044 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003045
Bob Wilsond5448bb2010-11-18 21:16:28 +00003046 // Left shifts translate directly to the vshiftu intrinsic.
3047 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003048 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003049 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3050 N->getOperand(0), N->getOperand(1));
3051
3052 assert((N->getOpcode() == ISD::SRA ||
3053 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3054
3055 // NEON uses the same intrinsics for both left and right shifts. For
3056 // right shifts, the shift amounts are negative, so negate the vector of
3057 // shift amounts.
3058 EVT ShiftVT = N->getOperand(1).getValueType();
3059 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3060 getZeroVector(ShiftVT, DAG, dl),
3061 N->getOperand(1));
3062 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3063 Intrinsic::arm_neon_vshifts :
3064 Intrinsic::arm_neon_vshiftu);
3065 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3066 DAG.getConstant(vshiftInt, MVT::i32),
3067 N->getOperand(0), NegatedCount);
3068}
3069
3070static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3071 const ARMSubtarget *ST) {
3072 EVT VT = N->getValueType(0);
3073 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003074
Eli Friedmance392eb2009-08-22 03:13:10 +00003075 // We can get here for a node like i32 = ISD::SHL i32, i64
3076 if (VT != MVT::i64)
3077 return SDValue();
3078
3079 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003080 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003081
Chris Lattner27a6c732007-11-24 07:07:01 +00003082 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3083 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003084 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003085 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003086
Chris Lattner27a6c732007-11-24 07:07:01 +00003087 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003088 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003089
Chris Lattner27a6c732007-11-24 07:07:01 +00003090 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003091 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003092 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003093 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003094 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003095
Chris Lattner27a6c732007-11-24 07:07:01 +00003096 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3097 // captures the result into a carry flag.
3098 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003099 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003100
Chris Lattner27a6c732007-11-24 07:07:01 +00003101 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003102 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003103
Chris Lattner27a6c732007-11-24 07:07:01 +00003104 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003105 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003106}
3107
Bob Wilson5bafff32009-06-22 23:27:02 +00003108static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3109 SDValue TmpOp0, TmpOp1;
3110 bool Invert = false;
3111 bool Swap = false;
3112 unsigned Opc = 0;
3113
3114 SDValue Op0 = Op.getOperand(0);
3115 SDValue Op1 = Op.getOperand(1);
3116 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003117 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003118 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3119 DebugLoc dl = Op.getDebugLoc();
3120
3121 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3122 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003123 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003124 case ISD::SETUNE:
3125 case ISD::SETNE: Invert = true; // Fallthrough
3126 case ISD::SETOEQ:
3127 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3128 case ISD::SETOLT:
3129 case ISD::SETLT: Swap = true; // Fallthrough
3130 case ISD::SETOGT:
3131 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3132 case ISD::SETOLE:
3133 case ISD::SETLE: Swap = true; // Fallthrough
3134 case ISD::SETOGE:
3135 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3136 case ISD::SETUGE: Swap = true; // Fallthrough
3137 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3138 case ISD::SETUGT: Swap = true; // Fallthrough
3139 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3140 case ISD::SETUEQ: Invert = true; // Fallthrough
3141 case ISD::SETONE:
3142 // Expand this to (OLT | OGT).
3143 TmpOp0 = Op0;
3144 TmpOp1 = Op1;
3145 Opc = ISD::OR;
3146 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3147 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3148 break;
3149 case ISD::SETUO: Invert = true; // Fallthrough
3150 case ISD::SETO:
3151 // Expand this to (OLT | OGE).
3152 TmpOp0 = Op0;
3153 TmpOp1 = Op1;
3154 Opc = ISD::OR;
3155 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3156 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3157 break;
3158 }
3159 } else {
3160 // Integer comparisons.
3161 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003162 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003163 case ISD::SETNE: Invert = true;
3164 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3165 case ISD::SETLT: Swap = true;
3166 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3167 case ISD::SETLE: Swap = true;
3168 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3169 case ISD::SETULT: Swap = true;
3170 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3171 case ISD::SETULE: Swap = true;
3172 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3173 }
3174
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003175 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003176 if (Opc == ARMISD::VCEQ) {
3177
3178 SDValue AndOp;
3179 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3180 AndOp = Op0;
3181 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3182 AndOp = Op1;
3183
3184 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003185 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003186 AndOp = AndOp.getOperand(0);
3187
3188 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3189 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003190 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3191 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003192 Invert = !Invert;
3193 }
3194 }
3195 }
3196
3197 if (Swap)
3198 std::swap(Op0, Op1);
3199
Owen Andersonc24cb352010-11-08 23:21:22 +00003200 // If one of the operands is a constant vector zero, attempt to fold the
3201 // comparison to a specialized compare-against-zero form.
3202 SDValue SingleOp;
3203 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3204 SingleOp = Op0;
3205 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3206 if (Opc == ARMISD::VCGE)
3207 Opc = ARMISD::VCLEZ;
3208 else if (Opc == ARMISD::VCGT)
3209 Opc = ARMISD::VCLTZ;
3210 SingleOp = Op1;
3211 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003212
Owen Andersonc24cb352010-11-08 23:21:22 +00003213 SDValue Result;
3214 if (SingleOp.getNode()) {
3215 switch (Opc) {
3216 case ARMISD::VCEQ:
3217 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3218 case ARMISD::VCGE:
3219 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3220 case ARMISD::VCLEZ:
3221 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3222 case ARMISD::VCGT:
3223 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3224 case ARMISD::VCLTZ:
3225 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3226 default:
3227 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3228 }
3229 } else {
3230 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3231 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003232
3233 if (Invert)
3234 Result = DAG.getNOT(dl, Result, VT);
3235
3236 return Result;
3237}
3238
Bob Wilsond3c42842010-06-14 22:19:57 +00003239/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3240/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003241/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003242static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3243 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003244 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003245 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003246
Bob Wilson827b2102010-06-15 19:05:35 +00003247 // SplatBitSize is set to the smallest size that splats the vector, so a
3248 // zero vector will always have SplatBitSize == 8. However, NEON modified
3249 // immediate instructions others than VMOV do not support the 8-bit encoding
3250 // of a zero vector, and the default encoding of zero is supposed to be the
3251 // 32-bit version.
3252 if (SplatBits == 0)
3253 SplatBitSize = 32;
3254
Bob Wilson5bafff32009-06-22 23:27:02 +00003255 switch (SplatBitSize) {
3256 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003257 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003258 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003259 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003260 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003261 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003262 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003263 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003264 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003265
3266 case 16:
3267 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003268 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003269 if ((SplatBits & ~0xff) == 0) {
3270 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003271 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003272 Imm = SplatBits;
3273 break;
3274 }
3275 if ((SplatBits & ~0xff00) == 0) {
3276 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003277 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003278 Imm = SplatBits >> 8;
3279 break;
3280 }
3281 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003282
3283 case 32:
3284 // NEON's 32-bit VMOV supports splat values where:
3285 // * only one byte is nonzero, or
3286 // * the least significant byte is 0xff and the second byte is nonzero, or
3287 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003288 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003289 if ((SplatBits & ~0xff) == 0) {
3290 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003291 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003292 Imm = SplatBits;
3293 break;
3294 }
3295 if ((SplatBits & ~0xff00) == 0) {
3296 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003297 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003298 Imm = SplatBits >> 8;
3299 break;
3300 }
3301 if ((SplatBits & ~0xff0000) == 0) {
3302 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003303 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003304 Imm = SplatBits >> 16;
3305 break;
3306 }
3307 if ((SplatBits & ~0xff000000) == 0) {
3308 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003309 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003310 Imm = SplatBits >> 24;
3311 break;
3312 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003313
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003314 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3315 if (type == OtherModImm) return SDValue();
3316
Bob Wilson5bafff32009-06-22 23:27:02 +00003317 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003318 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3319 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003320 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003321 Imm = SplatBits >> 8;
3322 SplatBits |= 0xff;
3323 break;
3324 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003325
3326 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003327 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3328 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003329 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003330 Imm = SplatBits >> 16;
3331 SplatBits |= 0xffff;
3332 break;
3333 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003334
3335 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3336 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3337 // VMOV.I32. A (very) minor optimization would be to replicate the value
3338 // and fall through here to test for a valid 64-bit splat. But, then the
3339 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003340 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003341
3342 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003343 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003344 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003345 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003346 uint64_t BitMask = 0xff;
3347 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003348 unsigned ImmMask = 1;
3349 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003350 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003351 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003352 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003353 Imm |= ImmMask;
3354 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003355 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003356 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003357 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003358 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003359 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003360 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003361 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003362 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003363 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003364 break;
3365 }
3366
Bob Wilson1a913ed2010-06-11 21:34:50 +00003367 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003368 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003369 return SDValue();
3370 }
3371
Bob Wilsoncba270d2010-07-13 21:16:48 +00003372 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3373 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003374}
3375
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003376static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3377 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003378 unsigned NumElts = VT.getVectorNumElements();
3379 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003380
3381 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3382 if (M[0] < 0)
3383 return false;
3384
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003385 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003386
3387 // If this is a VEXT shuffle, the immediate value is the index of the first
3388 // element. The other shuffle indices must be the successive elements after
3389 // the first one.
3390 unsigned ExpectedElt = Imm;
3391 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003392 // Increment the expected index. If it wraps around, it may still be
3393 // a VEXT but the source vectors must be swapped.
3394 ExpectedElt += 1;
3395 if (ExpectedElt == NumElts * 2) {
3396 ExpectedElt = 0;
3397 ReverseVEXT = true;
3398 }
3399
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003400 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003401 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003402 return false;
3403 }
3404
3405 // Adjust the index value if the source operands will be swapped.
3406 if (ReverseVEXT)
3407 Imm -= NumElts;
3408
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003409 return true;
3410}
3411
Bob Wilson8bb9e482009-07-26 00:39:34 +00003412/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3413/// instruction with the specified blocksize. (The order of the elements
3414/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003415static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3416 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003417 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3418 "Only possible block sizes for VREV are: 16, 32, 64");
3419
Bob Wilson8bb9e482009-07-26 00:39:34 +00003420 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003421 if (EltSz == 64)
3422 return false;
3423
3424 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003425 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003426 // If the first shuffle index is UNDEF, be optimistic.
3427 if (M[0] < 0)
3428 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003429
3430 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3431 return false;
3432
3433 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003434 if (M[i] < 0) continue; // ignore UNDEF indices
3435 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003436 return false;
3437 }
3438
3439 return true;
3440}
3441
Bob Wilsonc692cb72009-08-21 20:54:19 +00003442static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3443 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003444 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3445 if (EltSz == 64)
3446 return false;
3447
Bob Wilsonc692cb72009-08-21 20:54:19 +00003448 unsigned NumElts = VT.getVectorNumElements();
3449 WhichResult = (M[0] == 0 ? 0 : 1);
3450 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003451 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3452 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003453 return false;
3454 }
3455 return true;
3456}
3457
Bob Wilson324f4f12009-12-03 06:40:55 +00003458/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3459/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3460/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3461static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3462 unsigned &WhichResult) {
3463 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3464 if (EltSz == 64)
3465 return false;
3466
3467 unsigned NumElts = VT.getVectorNumElements();
3468 WhichResult = (M[0] == 0 ? 0 : 1);
3469 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003470 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3471 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003472 return false;
3473 }
3474 return true;
3475}
3476
Bob Wilsonc692cb72009-08-21 20:54:19 +00003477static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3478 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003479 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3480 if (EltSz == 64)
3481 return false;
3482
Bob Wilsonc692cb72009-08-21 20:54:19 +00003483 unsigned NumElts = VT.getVectorNumElements();
3484 WhichResult = (M[0] == 0 ? 0 : 1);
3485 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003486 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003487 if ((unsigned) M[i] != 2 * i + WhichResult)
3488 return false;
3489 }
3490
3491 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003492 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003493 return false;
3494
3495 return true;
3496}
3497
Bob Wilson324f4f12009-12-03 06:40:55 +00003498/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3499/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3500/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3501static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3502 unsigned &WhichResult) {
3503 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3504 if (EltSz == 64)
3505 return false;
3506
3507 unsigned Half = VT.getVectorNumElements() / 2;
3508 WhichResult = (M[0] == 0 ? 0 : 1);
3509 for (unsigned j = 0; j != 2; ++j) {
3510 unsigned Idx = WhichResult;
3511 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003512 int MIdx = M[i + j * Half];
3513 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003514 return false;
3515 Idx += 2;
3516 }
3517 }
3518
3519 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3520 if (VT.is64BitVector() && EltSz == 32)
3521 return false;
3522
3523 return true;
3524}
3525
Bob Wilsonc692cb72009-08-21 20:54:19 +00003526static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3527 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003528 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3529 if (EltSz == 64)
3530 return false;
3531
Bob Wilsonc692cb72009-08-21 20:54:19 +00003532 unsigned NumElts = VT.getVectorNumElements();
3533 WhichResult = (M[0] == 0 ? 0 : 1);
3534 unsigned Idx = WhichResult * NumElts / 2;
3535 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003536 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3537 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003538 return false;
3539 Idx += 1;
3540 }
3541
3542 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003543 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003544 return false;
3545
3546 return true;
3547}
3548
Bob Wilson324f4f12009-12-03 06:40:55 +00003549/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3550/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3551/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3552static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3553 unsigned &WhichResult) {
3554 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3555 if (EltSz == 64)
3556 return false;
3557
3558 unsigned NumElts = VT.getVectorNumElements();
3559 WhichResult = (M[0] == 0 ? 0 : 1);
3560 unsigned Idx = WhichResult * NumElts / 2;
3561 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003562 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3563 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003564 return false;
3565 Idx += 1;
3566 }
3567
3568 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3569 if (VT.is64BitVector() && EltSz == 32)
3570 return false;
3571
3572 return true;
3573}
3574
Dale Johannesenf630c712010-07-29 20:10:08 +00003575// If N is an integer constant that can be moved into a register in one
3576// instruction, return an SDValue of such a constant (will become a MOV
3577// instruction). Otherwise return null.
3578static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3579 const ARMSubtarget *ST, DebugLoc dl) {
3580 uint64_t Val;
3581 if (!isa<ConstantSDNode>(N))
3582 return SDValue();
3583 Val = cast<ConstantSDNode>(N)->getZExtValue();
3584
3585 if (ST->isThumb1Only()) {
3586 if (Val <= 255 || ~Val <= 255)
3587 return DAG.getConstant(Val, MVT::i32);
3588 } else {
3589 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3590 return DAG.getConstant(Val, MVT::i32);
3591 }
3592 return SDValue();
3593}
3594
Bob Wilson5bafff32009-06-22 23:27:02 +00003595// If this is a case we can't handle, return null and let the default
3596// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003597SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3598 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003599 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003600 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003601 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003602
3603 APInt SplatBits, SplatUndef;
3604 unsigned SplatBitSize;
3605 bool HasAnyUndefs;
3606 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003607 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003608 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003609 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003610 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003611 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003612 DAG, VmovVT, VT.is128BitVector(),
3613 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003614 if (Val.getNode()) {
3615 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003616 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003617 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003618
3619 // Try an immediate VMVN.
3620 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3621 ((1LL << SplatBitSize) - 1));
3622 Val = isNEONModifiedImm(NegatedImm,
3623 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003624 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003625 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003626 if (Val.getNode()) {
3627 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003628 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003629 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003630 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003631 }
3632
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003633 // Scan through the operands to see if only one value is used.
3634 unsigned NumElts = VT.getVectorNumElements();
3635 bool isOnlyLowElement = true;
3636 bool usesOnlyOneValue = true;
3637 bool isConstant = true;
3638 SDValue Value;
3639 for (unsigned i = 0; i < NumElts; ++i) {
3640 SDValue V = Op.getOperand(i);
3641 if (V.getOpcode() == ISD::UNDEF)
3642 continue;
3643 if (i > 0)
3644 isOnlyLowElement = false;
3645 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3646 isConstant = false;
3647
3648 if (!Value.getNode())
3649 Value = V;
3650 else if (V != Value)
3651 usesOnlyOneValue = false;
3652 }
3653
3654 if (!Value.getNode())
3655 return DAG.getUNDEF(VT);
3656
3657 if (isOnlyLowElement)
3658 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3659
Dale Johannesenf630c712010-07-29 20:10:08 +00003660 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3661
Dale Johannesen575cd142010-10-19 20:00:17 +00003662 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3663 // i32 and try again.
3664 if (usesOnlyOneValue && EltSize <= 32) {
3665 if (!isConstant)
3666 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3667 if (VT.getVectorElementType().isFloatingPoint()) {
3668 SmallVector<SDValue, 8> Ops;
3669 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003670 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003671 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003672 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3673 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003674 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3675 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003676 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003677 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003678 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3679 if (Val.getNode())
3680 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003681 }
3682
3683 // If all elements are constants and the case above didn't get hit, fall back
3684 // to the default expansion, which will generate a load from the constant
3685 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003686 if (isConstant)
3687 return SDValue();
3688
Bob Wilson11a1dff2011-01-07 21:37:30 +00003689 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3690 if (NumElts >= 4) {
3691 SDValue shuffle = ReconstructShuffle(Op, DAG);
3692 if (shuffle != SDValue())
3693 return shuffle;
3694 }
3695
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003696 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003697 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3698 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003699 if (EltSize >= 32) {
3700 // Do the expansion with floating-point types, since that is what the VFP
3701 // registers are defined to use, and since i64 is not legal.
3702 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3703 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003704 SmallVector<SDValue, 8> Ops;
3705 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003706 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003707 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003708 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003709 }
3710
3711 return SDValue();
3712}
3713
Bob Wilson11a1dff2011-01-07 21:37:30 +00003714// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003715// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003716SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3717 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003718 DebugLoc dl = Op.getDebugLoc();
3719 EVT VT = Op.getValueType();
3720 unsigned NumElts = VT.getVectorNumElements();
3721
3722 SmallVector<SDValue, 2> SourceVecs;
3723 SmallVector<unsigned, 2> MinElts;
3724 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003725
Bob Wilson11a1dff2011-01-07 21:37:30 +00003726 for (unsigned i = 0; i < NumElts; ++i) {
3727 SDValue V = Op.getOperand(i);
3728 if (V.getOpcode() == ISD::UNDEF)
3729 continue;
3730 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3731 // A shuffle can only come from building a vector from various
3732 // elements of other vectors.
3733 return SDValue();
3734 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003735
Bob Wilson11a1dff2011-01-07 21:37:30 +00003736 // Record this extraction against the appropriate vector if possible...
3737 SDValue SourceVec = V.getOperand(0);
3738 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3739 bool FoundSource = false;
3740 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3741 if (SourceVecs[j] == SourceVec) {
3742 if (MinElts[j] > EltNo)
3743 MinElts[j] = EltNo;
3744 if (MaxElts[j] < EltNo)
3745 MaxElts[j] = EltNo;
3746 FoundSource = true;
3747 break;
3748 }
3749 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003750
Bob Wilson11a1dff2011-01-07 21:37:30 +00003751 // Or record a new source if not...
3752 if (!FoundSource) {
3753 SourceVecs.push_back(SourceVec);
3754 MinElts.push_back(EltNo);
3755 MaxElts.push_back(EltNo);
3756 }
3757 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003758
Bob Wilson11a1dff2011-01-07 21:37:30 +00003759 // Currently only do something sane when at most two source vectors
3760 // involved.
3761 if (SourceVecs.size() > 2)
3762 return SDValue();
3763
3764 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3765 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003766
Bob Wilson11a1dff2011-01-07 21:37:30 +00003767 // This loop extracts the usage patterns of the source vectors
3768 // and prepares appropriate SDValues for a shuffle if possible.
3769 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3770 if (SourceVecs[i].getValueType() == VT) {
3771 // No VEXT necessary
3772 ShuffleSrcs[i] = SourceVecs[i];
3773 VEXTOffsets[i] = 0;
3774 continue;
3775 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3776 // It probably isn't worth padding out a smaller vector just to
3777 // break it down again in a shuffle.
3778 return SDValue();
3779 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003780
Bob Wilson11a1dff2011-01-07 21:37:30 +00003781 // Since only 64-bit and 128-bit vectors are legal on ARM and
3782 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003783 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3784 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003785
Bob Wilson11a1dff2011-01-07 21:37:30 +00003786 if (MaxElts[i] - MinElts[i] >= NumElts) {
3787 // Span too large for a VEXT to cope
3788 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003789 }
3790
Bob Wilson11a1dff2011-01-07 21:37:30 +00003791 if (MinElts[i] >= NumElts) {
3792 // The extraction can just take the second half
3793 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003794 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3795 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003796 DAG.getIntPtrConstant(NumElts));
3797 } else if (MaxElts[i] < NumElts) {
3798 // The extraction can just take the first half
3799 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003800 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3801 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003802 DAG.getIntPtrConstant(0));
3803 } else {
3804 // An actual VEXT is needed
3805 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003806 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3807 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003808 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003809 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3810 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003811 DAG.getIntPtrConstant(NumElts));
3812 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3813 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3814 }
3815 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003816
Bob Wilson11a1dff2011-01-07 21:37:30 +00003817 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003818
Bob Wilson11a1dff2011-01-07 21:37:30 +00003819 for (unsigned i = 0; i < NumElts; ++i) {
3820 SDValue Entry = Op.getOperand(i);
3821 if (Entry.getOpcode() == ISD::UNDEF) {
3822 Mask.push_back(-1);
3823 continue;
3824 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003825
Bob Wilson11a1dff2011-01-07 21:37:30 +00003826 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003827 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3828 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003829 if (ExtractVec == SourceVecs[0]) {
3830 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3831 } else {
3832 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3833 }
3834 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003835
Bob Wilson11a1dff2011-01-07 21:37:30 +00003836 // Final check before we try to produce nonsense...
3837 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00003838 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
3839 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003840
Bob Wilson11a1dff2011-01-07 21:37:30 +00003841 return SDValue();
3842}
3843
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003844/// isShuffleMaskLegal - Targets can use this to indicate that they only
3845/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3846/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3847/// are assumed to be legal.
3848bool
3849ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3850 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003851 if (VT.getVectorNumElements() == 4 &&
3852 (VT.is128BitVector() || VT.is64BitVector())) {
3853 unsigned PFIndexes[4];
3854 for (unsigned i = 0; i != 4; ++i) {
3855 if (M[i] < 0)
3856 PFIndexes[i] = 8;
3857 else
3858 PFIndexes[i] = M[i];
3859 }
3860
3861 // Compute the index in the perfect shuffle table.
3862 unsigned PFTableIndex =
3863 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3864 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3865 unsigned Cost = (PFEntry >> 30);
3866
3867 if (Cost <= 4)
3868 return true;
3869 }
3870
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003871 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003872 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003873
Bob Wilson53dd2452010-06-07 23:53:38 +00003874 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3875 return (EltSize >= 32 ||
3876 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003877 isVREVMask(M, VT, 64) ||
3878 isVREVMask(M, VT, 32) ||
3879 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003880 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3881 isVTRNMask(M, VT, WhichResult) ||
3882 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003883 isVZIPMask(M, VT, WhichResult) ||
3884 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3885 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3886 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003887}
3888
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003889/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3890/// the specified operations to build the shuffle.
3891static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3892 SDValue RHS, SelectionDAG &DAG,
3893 DebugLoc dl) {
3894 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3895 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3896 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3897
3898 enum {
3899 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3900 OP_VREV,
3901 OP_VDUP0,
3902 OP_VDUP1,
3903 OP_VDUP2,
3904 OP_VDUP3,
3905 OP_VEXT1,
3906 OP_VEXT2,
3907 OP_VEXT3,
3908 OP_VUZPL, // VUZP, left result
3909 OP_VUZPR, // VUZP, right result
3910 OP_VZIPL, // VZIP, left result
3911 OP_VZIPR, // VZIP, right result
3912 OP_VTRNL, // VTRN, left result
3913 OP_VTRNR // VTRN, right result
3914 };
3915
3916 if (OpNum == OP_COPY) {
3917 if (LHSID == (1*9+2)*9+3) return LHS;
3918 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3919 return RHS;
3920 }
3921
3922 SDValue OpLHS, OpRHS;
3923 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3924 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3925 EVT VT = OpLHS.getValueType();
3926
3927 switch (OpNum) {
3928 default: llvm_unreachable("Unknown shuffle opcode!");
3929 case OP_VREV:
3930 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3931 case OP_VDUP0:
3932 case OP_VDUP1:
3933 case OP_VDUP2:
3934 case OP_VDUP3:
3935 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003936 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003937 case OP_VEXT1:
3938 case OP_VEXT2:
3939 case OP_VEXT3:
3940 return DAG.getNode(ARMISD::VEXT, dl, VT,
3941 OpLHS, OpRHS,
3942 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3943 case OP_VUZPL:
3944 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003945 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003946 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3947 case OP_VZIPL:
3948 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003949 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003950 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3951 case OP_VTRNL:
3952 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003953 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3954 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003955 }
3956}
3957
Bob Wilson5bafff32009-06-22 23:27:02 +00003958static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003959 SDValue V1 = Op.getOperand(0);
3960 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003961 DebugLoc dl = Op.getDebugLoc();
3962 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003963 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003964 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003965
Bob Wilson28865062009-08-13 02:13:04 +00003966 // Convert shuffles that are directly supported on NEON to target-specific
3967 // DAG nodes, instead of keeping them as shuffles and matching them again
3968 // during code selection. This is more efficient and avoids the possibility
3969 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003970 // FIXME: floating-point vectors should be canonicalized to integer vectors
3971 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003972 SVN->getMask(ShuffleMask);
3973
Bob Wilson53dd2452010-06-07 23:53:38 +00003974 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3975 if (EltSize <= 32) {
3976 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3977 int Lane = SVN->getSplatIndex();
3978 // If this is undef splat, generate it via "just" vdup, if possible.
3979 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003980
Bob Wilson53dd2452010-06-07 23:53:38 +00003981 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3982 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3983 }
3984 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3985 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003986 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003987
3988 bool ReverseVEXT;
3989 unsigned Imm;
3990 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3991 if (ReverseVEXT)
3992 std::swap(V1, V2);
3993 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3994 DAG.getConstant(Imm, MVT::i32));
3995 }
3996
3997 if (isVREVMask(ShuffleMask, VT, 64))
3998 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3999 if (isVREVMask(ShuffleMask, VT, 32))
4000 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4001 if (isVREVMask(ShuffleMask, VT, 16))
4002 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4003
4004 // Check for Neon shuffles that modify both input vectors in place.
4005 // If both results are used, i.e., if there are two shuffles with the same
4006 // source operands and with masks corresponding to both results of one of
4007 // these operations, DAG memoization will ensure that a single node is
4008 // used for both shuffles.
4009 unsigned WhichResult;
4010 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4011 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4012 V1, V2).getValue(WhichResult);
4013 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4014 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4015 V1, V2).getValue(WhichResult);
4016 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4017 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4018 V1, V2).getValue(WhichResult);
4019
4020 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4021 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4022 V1, V1).getValue(WhichResult);
4023 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4024 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4025 V1, V1).getValue(WhichResult);
4026 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4027 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4028 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004029 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004030
Bob Wilsonc692cb72009-08-21 20:54:19 +00004031 // If the shuffle is not directly supported and it has 4 elements, use
4032 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004033 unsigned NumElts = VT.getVectorNumElements();
4034 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004035 unsigned PFIndexes[4];
4036 for (unsigned i = 0; i != 4; ++i) {
4037 if (ShuffleMask[i] < 0)
4038 PFIndexes[i] = 8;
4039 else
4040 PFIndexes[i] = ShuffleMask[i];
4041 }
4042
4043 // Compute the index in the perfect shuffle table.
4044 unsigned PFTableIndex =
4045 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004046 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4047 unsigned Cost = (PFEntry >> 30);
4048
4049 if (Cost <= 4)
4050 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4051 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004052
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004053 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004054 if (EltSize >= 32) {
4055 // Do the expansion with floating-point types, since that is what the VFP
4056 // registers are defined to use, and since i64 is not legal.
4057 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4058 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004059 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4060 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004061 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004062 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004063 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004064 Ops.push_back(DAG.getUNDEF(EltVT));
4065 else
4066 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4067 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4068 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4069 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004070 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004071 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004072 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004073 }
4074
Bob Wilson22cac0d2009-08-14 05:16:33 +00004075 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004076}
4077
Bob Wilson5bafff32009-06-22 23:27:02 +00004078static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004079 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004080 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004081 if (!isa<ConstantSDNode>(Lane))
4082 return SDValue();
4083
4084 SDValue Vec = Op.getOperand(0);
4085 if (Op.getValueType() == MVT::i32 &&
4086 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4087 DebugLoc dl = Op.getDebugLoc();
4088 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4089 }
4090
4091 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004092}
4093
Bob Wilsona6d65862009-08-03 20:36:38 +00004094static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4095 // The only time a CONCAT_VECTORS operation can have legal types is when
4096 // two 64-bit vectors are concatenated to a 128-bit vector.
4097 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4098 "unexpected CONCAT_VECTORS");
4099 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004100 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004101 SDValue Op0 = Op.getOperand(0);
4102 SDValue Op1 = Op.getOperand(1);
4103 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004104 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004105 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004106 DAG.getIntPtrConstant(0));
4107 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004108 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004109 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004110 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004111 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004112}
4113
Bob Wilson626613d2010-11-23 19:38:38 +00004114/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4115/// element has been zero/sign-extended, depending on the isSigned parameter,
4116/// from an integer type half its size.
4117static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4118 bool isSigned) {
4119 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4120 EVT VT = N->getValueType(0);
4121 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4122 SDNode *BVN = N->getOperand(0).getNode();
4123 if (BVN->getValueType(0) != MVT::v4i32 ||
4124 BVN->getOpcode() != ISD::BUILD_VECTOR)
4125 return false;
4126 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4127 unsigned HiElt = 1 - LoElt;
4128 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4129 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4130 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4131 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4132 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4133 return false;
4134 if (isSigned) {
4135 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4136 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4137 return true;
4138 } else {
4139 if (Hi0->isNullValue() && Hi1->isNullValue())
4140 return true;
4141 }
4142 return false;
4143 }
4144
4145 if (N->getOpcode() != ISD::BUILD_VECTOR)
4146 return false;
4147
4148 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4149 SDNode *Elt = N->getOperand(i).getNode();
4150 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4151 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4152 unsigned HalfSize = EltSize / 2;
4153 if (isSigned) {
4154 int64_t SExtVal = C->getSExtValue();
4155 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4156 return false;
4157 } else {
4158 if ((C->getZExtValue() >> HalfSize) != 0)
4159 return false;
4160 }
4161 continue;
4162 }
4163 return false;
4164 }
4165
4166 return true;
4167}
4168
4169/// isSignExtended - Check if a node is a vector value that is sign-extended
4170/// or a constant BUILD_VECTOR with sign-extended elements.
4171static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4172 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4173 return true;
4174 if (isExtendedBUILD_VECTOR(N, DAG, true))
4175 return true;
4176 return false;
4177}
4178
4179/// isZeroExtended - Check if a node is a vector value that is zero-extended
4180/// or a constant BUILD_VECTOR with zero-extended elements.
4181static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4182 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4183 return true;
4184 if (isExtendedBUILD_VECTOR(N, DAG, false))
4185 return true;
4186 return false;
4187}
4188
4189/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4190/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004191static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4192 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4193 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004194 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4195 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4196 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4197 LD->isNonTemporal(), LD->getAlignment());
4198 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4199 // have been legalized as a BITCAST from v4i32.
4200 if (N->getOpcode() == ISD::BITCAST) {
4201 SDNode *BVN = N->getOperand(0).getNode();
4202 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4203 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4204 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4205 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4206 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4207 }
4208 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4209 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4210 EVT VT = N->getValueType(0);
4211 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4212 unsigned NumElts = VT.getVectorNumElements();
4213 MVT TruncVT = MVT::getIntegerVT(EltSize);
4214 SmallVector<SDValue, 8> Ops;
4215 for (unsigned i = 0; i != NumElts; ++i) {
4216 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4217 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004218 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004219 }
4220 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4221 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004222}
4223
4224static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4225 // Multiplications are only custom-lowered for 128-bit vectors so that
4226 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4227 EVT VT = Op.getValueType();
4228 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4229 SDNode *N0 = Op.getOperand(0).getNode();
4230 SDNode *N1 = Op.getOperand(1).getNode();
4231 unsigned NewOpc = 0;
Bob Wilson626613d2010-11-23 19:38:38 +00004232 if (isSignExtended(N0, DAG) && isSignExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004233 NewOpc = ARMISD::VMULLs;
Bob Wilson626613d2010-11-23 19:38:38 +00004234 else if (isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004235 NewOpc = ARMISD::VMULLu;
Bob Wilson626613d2010-11-23 19:38:38 +00004236 else if (VT == MVT::v2i64)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004237 // Fall through to expand this. It is not legal.
4238 return SDValue();
Bob Wilson626613d2010-11-23 19:38:38 +00004239 else
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004240 // Other vector multiplications are legal.
4241 return Op;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004242
4243 // Legalize to a VMULL instruction.
4244 DebugLoc DL = Op.getDebugLoc();
4245 SDValue Op0 = SkipExtension(N0, DAG);
4246 SDValue Op1 = SkipExtension(N1, DAG);
4247
4248 assert(Op0.getValueType().is64BitVector() &&
4249 Op1.getValueType().is64BitVector() &&
4250 "unexpected types for extended operands to VMULL");
4251 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4252}
4253
Dan Gohmand858e902010-04-17 15:26:15 +00004254SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004255 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004256 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004257 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004258 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004259 case ISD::GlobalAddress:
4260 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4261 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00004262 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004263 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004264 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4265 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004266 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004267 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004268 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004269 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004270 case ISD::SINT_TO_FP:
4271 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4272 case ISD::FP_TO_SINT:
4273 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004274 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004275 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004276 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004277 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004278 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004279 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004280 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004281 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4282 Subtarget);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004283 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004284 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004285 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004286 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004287 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004288 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004289 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004290 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004291 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004292 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004293 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004294 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004295 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004296 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004297 case ISD::MUL: return LowerMUL(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004298 }
Dan Gohman475871a2008-07-27 21:46:04 +00004299 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004300}
4301
Duncan Sands1607f052008-12-01 11:39:25 +00004302/// ReplaceNodeResults - Replace the results of node with an illegal result
4303/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004304void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4305 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004306 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004307 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004308 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004309 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004310 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004311 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004312 case ISD::BITCAST:
4313 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004314 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004315 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004316 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004317 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004318 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004319 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004320 if (Res.getNode())
4321 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004322}
Chris Lattner27a6c732007-11-24 07:07:01 +00004323
Evan Chenga8e29892007-01-19 07:51:42 +00004324//===----------------------------------------------------------------------===//
4325// ARM Scheduler Hooks
4326//===----------------------------------------------------------------------===//
4327
4328MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004329ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4330 MachineBasicBlock *BB,
4331 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004332 unsigned dest = MI->getOperand(0).getReg();
4333 unsigned ptr = MI->getOperand(1).getReg();
4334 unsigned oldval = MI->getOperand(2).getReg();
4335 unsigned newval = MI->getOperand(3).getReg();
4336 unsigned scratch = BB->getParent()->getRegInfo()
4337 .createVirtualRegister(ARM::GPRRegisterClass);
4338 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4339 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004340 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004341
4342 unsigned ldrOpc, strOpc;
4343 switch (Size) {
4344 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004345 case 1:
4346 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004347 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004348 break;
4349 case 2:
4350 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4351 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4352 break;
4353 case 4:
4354 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4355 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4356 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004357 }
4358
4359 MachineFunction *MF = BB->getParent();
4360 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4361 MachineFunction::iterator It = BB;
4362 ++It; // insert the new blocks after the current block
4363
4364 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4365 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4366 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4367 MF->insert(It, loop1MBB);
4368 MF->insert(It, loop2MBB);
4369 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004370
4371 // Transfer the remainder of BB and its successor edges to exitMBB.
4372 exitMBB->splice(exitMBB->begin(), BB,
4373 llvm::next(MachineBasicBlock::iterator(MI)),
4374 BB->end());
4375 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004376
4377 // thisMBB:
4378 // ...
4379 // fallthrough --> loop1MBB
4380 BB->addSuccessor(loop1MBB);
4381
4382 // loop1MBB:
4383 // ldrex dest, [ptr]
4384 // cmp dest, oldval
4385 // bne exitMBB
4386 BB = loop1MBB;
4387 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004388 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004389 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004390 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4391 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004392 BB->addSuccessor(loop2MBB);
4393 BB->addSuccessor(exitMBB);
4394
4395 // loop2MBB:
4396 // strex scratch, newval, [ptr]
4397 // cmp scratch, #0
4398 // bne loop1MBB
4399 BB = loop2MBB;
4400 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4401 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004402 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004403 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004404 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4405 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004406 BB->addSuccessor(loop1MBB);
4407 BB->addSuccessor(exitMBB);
4408
4409 // exitMBB:
4410 // ...
4411 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004412
Dan Gohman14152b42010-07-06 20:24:04 +00004413 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004414
Jim Grosbach5278eb82009-12-11 01:42:04 +00004415 return BB;
4416}
4417
4418MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004419ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4420 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004421 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4422 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4423
4424 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004425 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004426 MachineFunction::iterator It = BB;
4427 ++It;
4428
4429 unsigned dest = MI->getOperand(0).getReg();
4430 unsigned ptr = MI->getOperand(1).getReg();
4431 unsigned incr = MI->getOperand(2).getReg();
4432 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004433
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004434 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004435 unsigned ldrOpc, strOpc;
4436 switch (Size) {
4437 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004438 case 1:
4439 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004440 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004441 break;
4442 case 2:
4443 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4444 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4445 break;
4446 case 4:
4447 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4448 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4449 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004450 }
4451
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004452 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4453 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4454 MF->insert(It, loopMBB);
4455 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004456
4457 // Transfer the remainder of BB and its successor edges to exitMBB.
4458 exitMBB->splice(exitMBB->begin(), BB,
4459 llvm::next(MachineBasicBlock::iterator(MI)),
4460 BB->end());
4461 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004462
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004463 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004464 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4465 unsigned scratch2 = (!BinOpcode) ? incr :
4466 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4467
4468 // thisMBB:
4469 // ...
4470 // fallthrough --> loopMBB
4471 BB->addSuccessor(loopMBB);
4472
4473 // loopMBB:
4474 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004475 // <binop> scratch2, dest, incr
4476 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004477 // cmp scratch, #0
4478 // bne- loopMBB
4479 // fallthrough --> exitMBB
4480 BB = loopMBB;
4481 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004482 if (BinOpcode) {
4483 // operand order needs to go the other way for NAND
4484 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4485 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4486 addReg(incr).addReg(dest)).addReg(0);
4487 else
4488 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4489 addReg(dest).addReg(incr)).addReg(0);
4490 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004491
4492 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4493 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004494 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004495 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004496 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4497 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004498
4499 BB->addSuccessor(loopMBB);
4500 BB->addSuccessor(exitMBB);
4501
4502 // exitMBB:
4503 // ...
4504 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004505
Dan Gohman14152b42010-07-06 20:24:04 +00004506 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004507
Jim Grosbachc3c23542009-12-14 04:22:04 +00004508 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004509}
4510
Evan Cheng218977b2010-07-13 19:27:42 +00004511static
4512MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4513 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4514 E = MBB->succ_end(); I != E; ++I)
4515 if (*I != Succ)
4516 return *I;
4517 llvm_unreachable("Expecting a BB with two successors!");
4518}
4519
Jim Grosbache801dc42009-12-12 01:40:06 +00004520MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004521ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004522 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004523 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004524 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004525 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004526 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004527 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004528 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004529 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004530
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004531 case ARM::ATOMIC_LOAD_ADD_I8:
4532 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4533 case ARM::ATOMIC_LOAD_ADD_I16:
4534 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4535 case ARM::ATOMIC_LOAD_ADD_I32:
4536 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004537
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004538 case ARM::ATOMIC_LOAD_AND_I8:
4539 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4540 case ARM::ATOMIC_LOAD_AND_I16:
4541 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4542 case ARM::ATOMIC_LOAD_AND_I32:
4543 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004544
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004545 case ARM::ATOMIC_LOAD_OR_I8:
4546 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4547 case ARM::ATOMIC_LOAD_OR_I16:
4548 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4549 case ARM::ATOMIC_LOAD_OR_I32:
4550 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004551
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004552 case ARM::ATOMIC_LOAD_XOR_I8:
4553 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4554 case ARM::ATOMIC_LOAD_XOR_I16:
4555 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4556 case ARM::ATOMIC_LOAD_XOR_I32:
4557 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004558
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004559 case ARM::ATOMIC_LOAD_NAND_I8:
4560 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4561 case ARM::ATOMIC_LOAD_NAND_I16:
4562 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4563 case ARM::ATOMIC_LOAD_NAND_I32:
4564 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004565
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004566 case ARM::ATOMIC_LOAD_SUB_I8:
4567 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4568 case ARM::ATOMIC_LOAD_SUB_I16:
4569 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4570 case ARM::ATOMIC_LOAD_SUB_I32:
4571 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004572
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004573 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4574 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4575 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004576
4577 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4578 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4579 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004580
Evan Cheng007ea272009-08-12 05:17:19 +00004581 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004582 // To "insert" a SELECT_CC instruction, we actually have to insert the
4583 // diamond control-flow pattern. The incoming instruction knows the
4584 // destination vreg to set, the condition code register to branch on, the
4585 // true/false values to select between, and a branch opcode to use.
4586 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004587 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004588 ++It;
4589
4590 // thisMBB:
4591 // ...
4592 // TrueVal = ...
4593 // cmpTY ccX, r1, r2
4594 // bCC copy1MBB
4595 // fallthrough --> copy0MBB
4596 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004597 MachineFunction *F = BB->getParent();
4598 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4599 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004600 F->insert(It, copy0MBB);
4601 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004602
4603 // Transfer the remainder of BB and its successor edges to sinkMBB.
4604 sinkMBB->splice(sinkMBB->begin(), BB,
4605 llvm::next(MachineBasicBlock::iterator(MI)),
4606 BB->end());
4607 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4608
Dan Gohman258c58c2010-07-06 15:49:48 +00004609 BB->addSuccessor(copy0MBB);
4610 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004611
Dan Gohman14152b42010-07-06 20:24:04 +00004612 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4613 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4614
Evan Chenga8e29892007-01-19 07:51:42 +00004615 // copy0MBB:
4616 // %FalseValue = ...
4617 // # fallthrough to sinkMBB
4618 BB = copy0MBB;
4619
4620 // Update machine-CFG edges
4621 BB->addSuccessor(sinkMBB);
4622
4623 // sinkMBB:
4624 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4625 // ...
4626 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004627 BuildMI(*BB, BB->begin(), dl,
4628 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004629 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4630 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4631
Dan Gohman14152b42010-07-06 20:24:04 +00004632 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004633 return BB;
4634 }
Evan Cheng86198642009-08-07 00:34:42 +00004635
Evan Cheng218977b2010-07-13 19:27:42 +00004636 case ARM::BCCi64:
4637 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00004638 // If there is an unconditional branch to the other successor, remove it.
4639 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004640
Evan Cheng218977b2010-07-13 19:27:42 +00004641 // Compare both parts that make up the double comparison separately for
4642 // equality.
4643 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4644
4645 unsigned LHS1 = MI->getOperand(1).getReg();
4646 unsigned LHS2 = MI->getOperand(2).getReg();
4647 if (RHSisZero) {
4648 AddDefaultPred(BuildMI(BB, dl,
4649 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4650 .addReg(LHS1).addImm(0));
4651 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4652 .addReg(LHS2).addImm(0)
4653 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4654 } else {
4655 unsigned RHS1 = MI->getOperand(3).getReg();
4656 unsigned RHS2 = MI->getOperand(4).getReg();
4657 AddDefaultPred(BuildMI(BB, dl,
4658 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4659 .addReg(LHS1).addReg(RHS1));
4660 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4661 .addReg(LHS2).addReg(RHS2)
4662 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4663 }
4664
4665 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4666 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4667 if (MI->getOperand(0).getImm() == ARMCC::NE)
4668 std::swap(destMBB, exitMBB);
4669
4670 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4671 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4672 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4673 .addMBB(exitMBB);
4674
4675 MI->eraseFromParent(); // The pseudo instruction is gone now.
4676 return BB;
4677 }
Evan Chenga8e29892007-01-19 07:51:42 +00004678 }
4679}
4680
4681//===----------------------------------------------------------------------===//
4682// ARM Optimization Hooks
4683//===----------------------------------------------------------------------===//
4684
Chris Lattnerd1980a52009-03-12 06:52:53 +00004685static
4686SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4687 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004688 SelectionDAG &DAG = DCI.DAG;
4689 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004690 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004691 unsigned Opc = N->getOpcode();
4692 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4693 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4694 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4695 ISD::CondCode CC = ISD::SETCC_INVALID;
4696
4697 if (isSlctCC) {
4698 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4699 } else {
4700 SDValue CCOp = Slct.getOperand(0);
4701 if (CCOp.getOpcode() == ISD::SETCC)
4702 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4703 }
4704
4705 bool DoXform = false;
4706 bool InvCC = false;
4707 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4708 "Bad input!");
4709
4710 if (LHS.getOpcode() == ISD::Constant &&
4711 cast<ConstantSDNode>(LHS)->isNullValue()) {
4712 DoXform = true;
4713 } else if (CC != ISD::SETCC_INVALID &&
4714 RHS.getOpcode() == ISD::Constant &&
4715 cast<ConstantSDNode>(RHS)->isNullValue()) {
4716 std::swap(LHS, RHS);
4717 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004718 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004719 Op0.getOperand(0).getValueType();
4720 bool isInt = OpVT.isInteger();
4721 CC = ISD::getSetCCInverse(CC, isInt);
4722
4723 if (!TLI.isCondCodeLegal(CC, OpVT))
4724 return SDValue(); // Inverse operator isn't legal.
4725
4726 DoXform = true;
4727 InvCC = true;
4728 }
4729
4730 if (DoXform) {
4731 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4732 if (isSlctCC)
4733 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4734 Slct.getOperand(0), Slct.getOperand(1), CC);
4735 SDValue CCOp = Slct.getOperand(0);
4736 if (InvCC)
4737 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4738 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4739 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4740 CCOp, OtherOp, Result);
4741 }
4742 return SDValue();
4743}
4744
Bob Wilson3d5792a2010-07-29 20:34:14 +00004745/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4746/// operands N0 and N1. This is a helper for PerformADDCombine that is
4747/// called with the default operands, and if that fails, with commuted
4748/// operands.
4749static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4750 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004751 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4752 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4753 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4754 if (Result.getNode()) return Result;
4755 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00004756 return SDValue();
4757}
4758
Bob Wilson3d5792a2010-07-29 20:34:14 +00004759/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4760///
4761static SDValue PerformADDCombine(SDNode *N,
4762 TargetLowering::DAGCombinerInfo &DCI) {
4763 SDValue N0 = N->getOperand(0);
4764 SDValue N1 = N->getOperand(1);
4765
4766 // First try with the default operand order.
4767 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4768 if (Result.getNode())
4769 return Result;
4770
4771 // If that didn't work, try again with the operands commuted.
4772 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4773}
4774
Chris Lattnerd1980a52009-03-12 06:52:53 +00004775/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004776///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004777static SDValue PerformSUBCombine(SDNode *N,
4778 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004779 SDValue N0 = N->getOperand(0);
4780 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004781
Chris Lattnerd1980a52009-03-12 06:52:53 +00004782 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4783 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4784 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4785 if (Result.getNode()) return Result;
4786 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004787
Chris Lattnerd1980a52009-03-12 06:52:53 +00004788 return SDValue();
4789}
4790
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004791static SDValue PerformMULCombine(SDNode *N,
4792 TargetLowering::DAGCombinerInfo &DCI,
4793 const ARMSubtarget *Subtarget) {
4794 SelectionDAG &DAG = DCI.DAG;
4795
4796 if (Subtarget->isThumb1Only())
4797 return SDValue();
4798
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004799 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4800 return SDValue();
4801
4802 EVT VT = N->getValueType(0);
4803 if (VT != MVT::i32)
4804 return SDValue();
4805
4806 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4807 if (!C)
4808 return SDValue();
4809
4810 uint64_t MulAmt = C->getZExtValue();
4811 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4812 ShiftAmt = ShiftAmt & (32 - 1);
4813 SDValue V = N->getOperand(0);
4814 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004815
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004816 SDValue Res;
4817 MulAmt >>= ShiftAmt;
4818 if (isPowerOf2_32(MulAmt - 1)) {
4819 // (mul x, 2^N + 1) => (add (shl x, N), x)
4820 Res = DAG.getNode(ISD::ADD, DL, VT,
4821 V, DAG.getNode(ISD::SHL, DL, VT,
4822 V, DAG.getConstant(Log2_32(MulAmt-1),
4823 MVT::i32)));
4824 } else if (isPowerOf2_32(MulAmt + 1)) {
4825 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4826 Res = DAG.getNode(ISD::SUB, DL, VT,
4827 DAG.getNode(ISD::SHL, DL, VT,
4828 V, DAG.getConstant(Log2_32(MulAmt+1),
4829 MVT::i32)),
4830 V);
4831 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004832 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004833
4834 if (ShiftAmt != 0)
4835 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4836 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004837
4838 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004839 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004840 return SDValue();
4841}
4842
Owen Anderson080c0922010-11-05 19:27:46 +00004843static SDValue PerformANDCombine(SDNode *N,
4844 TargetLowering::DAGCombinerInfo &DCI) {
4845 // Attempt to use immediate-form VBIC
4846 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4847 DebugLoc dl = N->getDebugLoc();
4848 EVT VT = N->getValueType(0);
4849 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004850
Owen Anderson080c0922010-11-05 19:27:46 +00004851 APInt SplatBits, SplatUndef;
4852 unsigned SplatBitSize;
4853 bool HasAnyUndefs;
4854 if (BVN &&
4855 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4856 if (SplatBitSize <= 64) {
4857 EVT VbicVT;
4858 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
4859 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004860 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004861 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00004862 if (Val.getNode()) {
4863 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004864 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00004865 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004866 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00004867 }
4868 }
4869 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004870
Owen Anderson080c0922010-11-05 19:27:46 +00004871 return SDValue();
4872}
4873
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004874/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4875static SDValue PerformORCombine(SDNode *N,
4876 TargetLowering::DAGCombinerInfo &DCI,
4877 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00004878 // Attempt to use immediate-form VORR
4879 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4880 DebugLoc dl = N->getDebugLoc();
4881 EVT VT = N->getValueType(0);
4882 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004883
Owen Anderson60f48702010-11-03 23:15:26 +00004884 APInt SplatBits, SplatUndef;
4885 unsigned SplatBitSize;
4886 bool HasAnyUndefs;
4887 if (BVN && Subtarget->hasNEON() &&
4888 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4889 if (SplatBitSize <= 64) {
4890 EVT VorrVT;
4891 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4892 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004893 DAG, VorrVT, VT.is128BitVector(),
4894 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00004895 if (Val.getNode()) {
4896 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004897 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00004898 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004899 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00004900 }
4901 }
4902 }
4903
Jim Grosbach54238562010-07-17 03:30:54 +00004904 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4905 // reasonable.
4906
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004907 // BFI is only available on V6T2+
4908 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4909 return SDValue();
4910
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004911 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004912 DebugLoc DL = N->getDebugLoc();
4913 // 1) or (and A, mask), val => ARMbfi A, val, mask
4914 // iff (val & mask) == val
4915 //
4916 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4917 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4918 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4919 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4920 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4921 // (i.e., copy a bitfield value into another bitfield of the same width)
4922 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004923 return SDValue();
4924
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004925 if (VT != MVT::i32)
4926 return SDValue();
4927
Evan Cheng30fb13f2010-12-13 20:32:54 +00004928 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00004929
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004930 // The value and the mask need to be constants so we can verify this is
4931 // actually a bitfield set. If the mask is 0xffff, we can do better
4932 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00004933 SDValue MaskOp = N0.getOperand(1);
4934 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
4935 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004936 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004937 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004938 if (Mask == 0xffff)
4939 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004940 SDValue Res;
4941 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004942 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4943 if (N1C) {
4944 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004945 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00004946 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004947
Evan Chenga9688c42010-12-11 04:11:38 +00004948 if (ARM::isBitFieldInvertedMask(Mask)) {
4949 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004950
Evan Cheng30fb13f2010-12-13 20:32:54 +00004951 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00004952 DAG.getConstant(Val, MVT::i32),
4953 DAG.getConstant(Mask, MVT::i32));
4954
4955 // Do not add new nodes to DAG combiner worklist.
4956 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004957 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004958 }
Jim Grosbach54238562010-07-17 03:30:54 +00004959 } else if (N1.getOpcode() == ISD::AND) {
4960 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004961 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4962 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00004963 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004964 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004965
4966 if (ARM::isBitFieldInvertedMask(Mask) &&
4967 ARM::isBitFieldInvertedMask(~Mask2) &&
4968 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4969 // The pack halfword instruction works better for masks that fit it,
4970 // so use that when it's available.
4971 if (Subtarget->hasT2ExtractPack() &&
4972 (Mask == 0xffff || Mask == 0xffff0000))
4973 return SDValue();
4974 // 2a
4975 unsigned lsb = CountTrailingZeros_32(Mask2);
4976 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4977 DAG.getConstant(lsb, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00004978 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00004979 DAG.getConstant(Mask, MVT::i32));
4980 // Do not add new nodes to DAG combiner worklist.
4981 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004982 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004983 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4984 ARM::isBitFieldInvertedMask(Mask2) &&
4985 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4986 // The pack halfword instruction works better for masks that fit it,
4987 // so use that when it's available.
4988 if (Subtarget->hasT2ExtractPack() &&
4989 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4990 return SDValue();
4991 // 2b
4992 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004993 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00004994 DAG.getConstant(lsb, MVT::i32));
4995 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4996 DAG.getConstant(Mask2, MVT::i32));
4997 // Do not add new nodes to DAG combiner worklist.
4998 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004999 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005000 }
5001 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005002
Evan Cheng30fb13f2010-12-13 20:32:54 +00005003 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5004 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5005 ARM::isBitFieldInvertedMask(~Mask)) {
5006 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5007 // where lsb(mask) == #shamt and masked bits of B are known zero.
5008 SDValue ShAmt = N00.getOperand(1);
5009 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5010 unsigned LSB = CountTrailingZeros_32(Mask);
5011 if (ShAmtC != LSB)
5012 return SDValue();
5013
5014 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5015 DAG.getConstant(~Mask, MVT::i32));
5016
5017 // Do not add new nodes to DAG combiner worklist.
5018 DCI.CombineTo(N, Res, false);
5019 }
5020
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005021 return SDValue();
5022}
5023
Evan Cheng0c1aec12010-12-14 03:22:07 +00005024/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5025/// C1 & C2 == C1.
5026static SDValue PerformBFICombine(SDNode *N,
5027 TargetLowering::DAGCombinerInfo &DCI) {
5028 SDValue N1 = N->getOperand(1);
5029 if (N1.getOpcode() == ISD::AND) {
5030 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5031 if (!N11C)
5032 return SDValue();
5033 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5034 unsigned Mask2 = N11C->getZExtValue();
5035 if ((Mask & Mask2) == Mask2)
5036 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5037 N->getOperand(0), N1.getOperand(0),
5038 N->getOperand(2));
5039 }
5040 return SDValue();
5041}
5042
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005043/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5044/// ARMISD::VMOVRRD.
5045static SDValue PerformVMOVRRDCombine(SDNode *N,
5046 TargetLowering::DAGCombinerInfo &DCI) {
5047 // vmovrrd(vmovdrr x, y) -> x,y
5048 SDValue InDouble = N->getOperand(0);
5049 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5050 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
5051 return SDValue();
5052}
5053
5054/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5055/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5056static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5057 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5058 SDValue Op0 = N->getOperand(0);
5059 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005060 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005061 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005062 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005063 Op1 = Op1.getOperand(0);
5064 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5065 Op0.getNode() == Op1.getNode() &&
5066 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005067 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005068 N->getValueType(0), Op0.getOperand(0));
5069 return SDValue();
5070}
5071
Bob Wilson31600902010-12-21 06:43:19 +00005072/// PerformSTORECombine - Target-specific dag combine xforms for
5073/// ISD::STORE.
5074static SDValue PerformSTORECombine(SDNode *N,
5075 TargetLowering::DAGCombinerInfo &DCI) {
5076 // Bitcast an i64 store extracted from a vector to f64.
5077 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5078 StoreSDNode *St = cast<StoreSDNode>(N);
5079 SDValue StVal = St->getValue();
5080 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5081 StVal.getValueType() != MVT::i64 ||
5082 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5083 return SDValue();
5084
5085 SelectionDAG &DAG = DCI.DAG;
5086 DebugLoc dl = StVal.getDebugLoc();
5087 SDValue IntVec = StVal.getOperand(0);
5088 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5089 IntVec.getValueType().getVectorNumElements());
5090 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5091 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5092 Vec, StVal.getOperand(1));
5093 dl = N->getDebugLoc();
5094 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5095 // Make the DAGCombiner fold the bitcasts.
5096 DCI.AddToWorklist(Vec.getNode());
5097 DCI.AddToWorklist(ExtElt.getNode());
5098 DCI.AddToWorklist(V.getNode());
5099 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5100 St->getPointerInfo(), St->isVolatile(),
5101 St->isNonTemporal(), St->getAlignment(),
5102 St->getTBAAInfo());
5103}
5104
5105/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5106/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5107/// i64 vector to have f64 elements, since the value can then be loaded
5108/// directly into a VFP register.
5109static bool hasNormalLoadOperand(SDNode *N) {
5110 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5111 for (unsigned i = 0; i < NumElts; ++i) {
5112 SDNode *Elt = N->getOperand(i).getNode();
5113 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5114 return true;
5115 }
5116 return false;
5117}
5118
Bob Wilson75f02882010-09-17 22:59:05 +00005119/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5120/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005121static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5122 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005123 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5124 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5125 // into a pair of GPRs, which is fine when the value is used as a scalar,
5126 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005127 SelectionDAG &DAG = DCI.DAG;
5128 if (N->getNumOperands() == 2) {
5129 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5130 if (RV.getNode())
5131 return RV;
5132 }
Bob Wilson75f02882010-09-17 22:59:05 +00005133
Bob Wilson31600902010-12-21 06:43:19 +00005134 // Load i64 elements as f64 values so that type legalization does not split
5135 // them up into i32 values.
5136 EVT VT = N->getValueType(0);
5137 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5138 return SDValue();
5139 DebugLoc dl = N->getDebugLoc();
5140 SmallVector<SDValue, 8> Ops;
5141 unsigned NumElts = VT.getVectorNumElements();
5142 for (unsigned i = 0; i < NumElts; ++i) {
5143 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5144 Ops.push_back(V);
5145 // Make the DAGCombiner fold the bitcast.
5146 DCI.AddToWorklist(V.getNode());
5147 }
5148 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5149 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5150 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5151}
5152
5153/// PerformInsertEltCombine - Target-specific dag combine xforms for
5154/// ISD::INSERT_VECTOR_ELT.
5155static SDValue PerformInsertEltCombine(SDNode *N,
5156 TargetLowering::DAGCombinerInfo &DCI) {
5157 // Bitcast an i64 load inserted into a vector to f64.
5158 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5159 EVT VT = N->getValueType(0);
5160 SDNode *Elt = N->getOperand(1).getNode();
5161 if (VT.getVectorElementType() != MVT::i64 ||
5162 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5163 return SDValue();
5164
5165 SelectionDAG &DAG = DCI.DAG;
5166 DebugLoc dl = N->getDebugLoc();
5167 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5168 VT.getVectorNumElements());
5169 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5170 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5171 // Make the DAGCombiner fold the bitcasts.
5172 DCI.AddToWorklist(Vec.getNode());
5173 DCI.AddToWorklist(V.getNode());
5174 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5175 Vec, V, N->getOperand(2));
5176 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005177}
5178
Bob Wilsonf20700c2010-10-27 20:38:28 +00005179/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5180/// ISD::VECTOR_SHUFFLE.
5181static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5182 // The LLVM shufflevector instruction does not require the shuffle mask
5183 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5184 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5185 // operands do not match the mask length, they are extended by concatenating
5186 // them with undef vectors. That is probably the right thing for other
5187 // targets, but for NEON it is better to concatenate two double-register
5188 // size vector operands into a single quad-register size vector. Do that
5189 // transformation here:
5190 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5191 // shuffle(concat(v1, v2), undef)
5192 SDValue Op0 = N->getOperand(0);
5193 SDValue Op1 = N->getOperand(1);
5194 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5195 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5196 Op0.getNumOperands() != 2 ||
5197 Op1.getNumOperands() != 2)
5198 return SDValue();
5199 SDValue Concat0Op1 = Op0.getOperand(1);
5200 SDValue Concat1Op1 = Op1.getOperand(1);
5201 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5202 Concat1Op1.getOpcode() != ISD::UNDEF)
5203 return SDValue();
5204 // Skip the transformation if any of the types are illegal.
5205 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5206 EVT VT = N->getValueType(0);
5207 if (!TLI.isTypeLegal(VT) ||
5208 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5209 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5210 return SDValue();
5211
5212 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5213 Op0.getOperand(0), Op1.getOperand(0));
5214 // Translate the shuffle mask.
5215 SmallVector<int, 16> NewMask;
5216 unsigned NumElts = VT.getVectorNumElements();
5217 unsigned HalfElts = NumElts/2;
5218 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5219 for (unsigned n = 0; n < NumElts; ++n) {
5220 int MaskElt = SVN->getMaskElt(n);
5221 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005222 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005223 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005224 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005225 NewElt = HalfElts + MaskElt - NumElts;
5226 NewMask.push_back(NewElt);
5227 }
5228 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5229 DAG.getUNDEF(VT), NewMask.data());
5230}
5231
Bob Wilson1c3ef902011-02-07 17:43:21 +00005232/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5233/// NEON load/store intrinsics to merge base address updates.
5234static SDValue CombineBaseUpdate(SDNode *N,
5235 TargetLowering::DAGCombinerInfo &DCI) {
5236 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5237 return SDValue();
5238
5239 SelectionDAG &DAG = DCI.DAG;
5240 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5241 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5242 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5243 SDValue Addr = N->getOperand(AddrOpIdx);
5244
5245 // Search for a use of the address operand that is an increment.
5246 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5247 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5248 SDNode *User = *UI;
5249 if (User->getOpcode() != ISD::ADD ||
5250 UI.getUse().getResNo() != Addr.getResNo())
5251 continue;
5252
5253 // Check that the add is independent of the load/store. Otherwise, folding
5254 // it would create a cycle.
5255 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5256 continue;
5257
5258 // Find the new opcode for the updating load/store.
5259 bool isLoad = true;
5260 bool isLaneOp = false;
5261 unsigned NewOpc = 0;
5262 unsigned NumVecs = 0;
5263 if (isIntrinsic) {
5264 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5265 switch (IntNo) {
5266 default: assert(0 && "unexpected intrinsic for Neon base update");
5267 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5268 NumVecs = 1; break;
5269 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5270 NumVecs = 2; break;
5271 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5272 NumVecs = 3; break;
5273 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5274 NumVecs = 4; break;
5275 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5276 NumVecs = 2; isLaneOp = true; break;
5277 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5278 NumVecs = 3; isLaneOp = true; break;
5279 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5280 NumVecs = 4; isLaneOp = true; break;
5281 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5282 NumVecs = 1; isLoad = false; break;
5283 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
5284 NumVecs = 2; isLoad = false; break;
5285 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
5286 NumVecs = 3; isLoad = false; break;
5287 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
5288 NumVecs = 4; isLoad = false; break;
5289 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
5290 NumVecs = 2; isLoad = false; isLaneOp = true; break;
5291 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
5292 NumVecs = 3; isLoad = false; isLaneOp = true; break;
5293 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
5294 NumVecs = 4; isLoad = false; isLaneOp = true; break;
5295 }
5296 } else {
5297 isLaneOp = true;
5298 switch (N->getOpcode()) {
5299 default: assert(0 && "unexpected opcode for Neon base update");
5300 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
5301 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
5302 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
5303 }
5304 }
5305
5306 // Find the size of memory referenced by the load/store.
5307 EVT VecTy;
5308 if (isLoad)
5309 VecTy = N->getValueType(0);
5310 else
5311 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
5312 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
5313 if (isLaneOp)
5314 NumBytes /= VecTy.getVectorNumElements();
5315
5316 // If the increment is a constant, it must match the memory ref size.
5317 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
5318 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
5319 uint64_t IncVal = CInc->getZExtValue();
5320 if (IncVal != NumBytes)
5321 continue;
5322 } else if (NumBytes >= 3 * 16) {
5323 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
5324 // separate instructions that make it harder to use a non-constant update.
5325 continue;
5326 }
5327
5328 // Create the new updating load/store node.
5329 EVT Tys[6];
5330 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
5331 unsigned n;
5332 for (n = 0; n < NumResultVecs; ++n)
5333 Tys[n] = VecTy;
5334 Tys[n++] = MVT::i32;
5335 Tys[n] = MVT::Other;
5336 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
5337 SmallVector<SDValue, 8> Ops;
5338 Ops.push_back(N->getOperand(0)); // incoming chain
5339 Ops.push_back(N->getOperand(AddrOpIdx));
5340 Ops.push_back(Inc);
5341 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
5342 Ops.push_back(N->getOperand(i));
5343 }
5344 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
5345 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
5346 Ops.data(), Ops.size(),
5347 MemInt->getMemoryVT(),
5348 MemInt->getMemOperand());
5349
5350 // Update the uses.
5351 std::vector<SDValue> NewResults;
5352 for (unsigned i = 0; i < NumResultVecs; ++i) {
5353 NewResults.push_back(SDValue(UpdN.getNode(), i));
5354 }
5355 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
5356 DCI.CombineTo(N, NewResults);
5357 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
5358
5359 break;
5360 }
5361 return SDValue();
5362}
5363
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005364/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5365/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5366/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5367/// return true.
5368static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5369 SelectionDAG &DAG = DCI.DAG;
5370 EVT VT = N->getValueType(0);
5371 // vldN-dup instructions only support 64-bit vectors for N > 1.
5372 if (!VT.is64BitVector())
5373 return false;
5374
5375 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5376 SDNode *VLD = N->getOperand(0).getNode();
5377 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5378 return false;
5379 unsigned NumVecs = 0;
5380 unsigned NewOpc = 0;
5381 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5382 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5383 NumVecs = 2;
5384 NewOpc = ARMISD::VLD2DUP;
5385 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5386 NumVecs = 3;
5387 NewOpc = ARMISD::VLD3DUP;
5388 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5389 NumVecs = 4;
5390 NewOpc = ARMISD::VLD4DUP;
5391 } else {
5392 return false;
5393 }
5394
5395 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5396 // numbers match the load.
5397 unsigned VLDLaneNo =
5398 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5399 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5400 UI != UE; ++UI) {
5401 // Ignore uses of the chain result.
5402 if (UI.getUse().getResNo() == NumVecs)
5403 continue;
5404 SDNode *User = *UI;
5405 if (User->getOpcode() != ARMISD::VDUPLANE ||
5406 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5407 return false;
5408 }
5409
5410 // Create the vldN-dup node.
5411 EVT Tys[5];
5412 unsigned n;
5413 for (n = 0; n < NumVecs; ++n)
5414 Tys[n] = VT;
5415 Tys[n] = MVT::Other;
5416 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5417 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5418 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5419 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5420 Ops, 2, VLDMemInt->getMemoryVT(),
5421 VLDMemInt->getMemOperand());
5422
5423 // Update the uses.
5424 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5425 UI != UE; ++UI) {
5426 unsigned ResNo = UI.getUse().getResNo();
5427 // Ignore uses of the chain result.
5428 if (ResNo == NumVecs)
5429 continue;
5430 SDNode *User = *UI;
5431 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5432 }
5433
5434 // Now the vldN-lane intrinsic is dead except for its chain result.
5435 // Update uses of the chain.
5436 std::vector<SDValue> VLDDupResults;
5437 for (unsigned n = 0; n < NumVecs; ++n)
5438 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5439 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5440 DCI.CombineTo(VLD, VLDDupResults);
5441
5442 return true;
5443}
5444
Bob Wilson9e82bf12010-07-14 01:22:12 +00005445/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5446/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005447static SDValue PerformVDUPLANECombine(SDNode *N,
5448 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005449 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005450
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005451 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5452 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5453 if (CombineVLDDUP(N, DCI))
5454 return SDValue(N, 0);
5455
5456 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5457 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005458 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005459 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005460 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005461 return SDValue();
5462
5463 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5464 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5465 // The canonical VMOV for a zero vector uses a 32-bit element size.
5466 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5467 unsigned EltBits;
5468 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5469 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005470 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005471 if (EltSize > VT.getVectorElementType().getSizeInBits())
5472 return SDValue();
5473
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005474 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005475}
5476
Bob Wilson5bafff32009-06-22 23:27:02 +00005477/// getVShiftImm - Check if this is a valid build_vector for the immediate
5478/// operand of a vector shift operation, where all the elements of the
5479/// build_vector must have the same constant integer value.
5480static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5481 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005482 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005483 Op = Op.getOperand(0);
5484 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5485 APInt SplatBits, SplatUndef;
5486 unsigned SplatBitSize;
5487 bool HasAnyUndefs;
5488 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5489 HasAnyUndefs, ElementBits) ||
5490 SplatBitSize > ElementBits)
5491 return false;
5492 Cnt = SplatBits.getSExtValue();
5493 return true;
5494}
5495
5496/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5497/// operand of a vector shift left operation. That value must be in the range:
5498/// 0 <= Value < ElementBits for a left shift; or
5499/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005500static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005501 assert(VT.isVector() && "vector shift count is not a vector type");
5502 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5503 if (! getVShiftImm(Op, ElementBits, Cnt))
5504 return false;
5505 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5506}
5507
5508/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5509/// operand of a vector shift right operation. For a shift opcode, the value
5510/// is positive, but for an intrinsic the value count must be negative. The
5511/// absolute value must be in the range:
5512/// 1 <= |Value| <= ElementBits for a right shift; or
5513/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005514static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005515 int64_t &Cnt) {
5516 assert(VT.isVector() && "vector shift count is not a vector type");
5517 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5518 if (! getVShiftImm(Op, ElementBits, Cnt))
5519 return false;
5520 if (isIntrinsic)
5521 Cnt = -Cnt;
5522 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5523}
5524
5525/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5526static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5527 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5528 switch (IntNo) {
5529 default:
5530 // Don't do anything for most intrinsics.
5531 break;
5532
5533 // Vector shifts: check for immediate versions and lower them.
5534 // Note: This is done during DAG combining instead of DAG legalizing because
5535 // the build_vectors for 64-bit vector element shift counts are generally
5536 // not legal, and it is hard to see their values after they get legalized to
5537 // loads from a constant pool.
5538 case Intrinsic::arm_neon_vshifts:
5539 case Intrinsic::arm_neon_vshiftu:
5540 case Intrinsic::arm_neon_vshiftls:
5541 case Intrinsic::arm_neon_vshiftlu:
5542 case Intrinsic::arm_neon_vshiftn:
5543 case Intrinsic::arm_neon_vrshifts:
5544 case Intrinsic::arm_neon_vrshiftu:
5545 case Intrinsic::arm_neon_vrshiftn:
5546 case Intrinsic::arm_neon_vqshifts:
5547 case Intrinsic::arm_neon_vqshiftu:
5548 case Intrinsic::arm_neon_vqshiftsu:
5549 case Intrinsic::arm_neon_vqshiftns:
5550 case Intrinsic::arm_neon_vqshiftnu:
5551 case Intrinsic::arm_neon_vqshiftnsu:
5552 case Intrinsic::arm_neon_vqrshiftns:
5553 case Intrinsic::arm_neon_vqrshiftnu:
5554 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005555 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005556 int64_t Cnt;
5557 unsigned VShiftOpc = 0;
5558
5559 switch (IntNo) {
5560 case Intrinsic::arm_neon_vshifts:
5561 case Intrinsic::arm_neon_vshiftu:
5562 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5563 VShiftOpc = ARMISD::VSHL;
5564 break;
5565 }
5566 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5567 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5568 ARMISD::VSHRs : ARMISD::VSHRu);
5569 break;
5570 }
5571 return SDValue();
5572
5573 case Intrinsic::arm_neon_vshiftls:
5574 case Intrinsic::arm_neon_vshiftlu:
5575 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
5576 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005577 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005578
5579 case Intrinsic::arm_neon_vrshifts:
5580 case Intrinsic::arm_neon_vrshiftu:
5581 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
5582 break;
5583 return SDValue();
5584
5585 case Intrinsic::arm_neon_vqshifts:
5586 case Intrinsic::arm_neon_vqshiftu:
5587 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5588 break;
5589 return SDValue();
5590
5591 case Intrinsic::arm_neon_vqshiftsu:
5592 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5593 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005594 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005595
5596 case Intrinsic::arm_neon_vshiftn:
5597 case Intrinsic::arm_neon_vrshiftn:
5598 case Intrinsic::arm_neon_vqshiftns:
5599 case Intrinsic::arm_neon_vqshiftnu:
5600 case Intrinsic::arm_neon_vqshiftnsu:
5601 case Intrinsic::arm_neon_vqrshiftns:
5602 case Intrinsic::arm_neon_vqrshiftnu:
5603 case Intrinsic::arm_neon_vqrshiftnsu:
5604 // Narrowing shifts require an immediate right shift.
5605 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
5606 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00005607 llvm_unreachable("invalid shift count for narrowing vector shift "
5608 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005609
5610 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005611 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00005612 }
5613
5614 switch (IntNo) {
5615 case Intrinsic::arm_neon_vshifts:
5616 case Intrinsic::arm_neon_vshiftu:
5617 // Opcode already set above.
5618 break;
5619 case Intrinsic::arm_neon_vshiftls:
5620 case Intrinsic::arm_neon_vshiftlu:
5621 if (Cnt == VT.getVectorElementType().getSizeInBits())
5622 VShiftOpc = ARMISD::VSHLLi;
5623 else
5624 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
5625 ARMISD::VSHLLs : ARMISD::VSHLLu);
5626 break;
5627 case Intrinsic::arm_neon_vshiftn:
5628 VShiftOpc = ARMISD::VSHRN; break;
5629 case Intrinsic::arm_neon_vrshifts:
5630 VShiftOpc = ARMISD::VRSHRs; break;
5631 case Intrinsic::arm_neon_vrshiftu:
5632 VShiftOpc = ARMISD::VRSHRu; break;
5633 case Intrinsic::arm_neon_vrshiftn:
5634 VShiftOpc = ARMISD::VRSHRN; break;
5635 case Intrinsic::arm_neon_vqshifts:
5636 VShiftOpc = ARMISD::VQSHLs; break;
5637 case Intrinsic::arm_neon_vqshiftu:
5638 VShiftOpc = ARMISD::VQSHLu; break;
5639 case Intrinsic::arm_neon_vqshiftsu:
5640 VShiftOpc = ARMISD::VQSHLsu; break;
5641 case Intrinsic::arm_neon_vqshiftns:
5642 VShiftOpc = ARMISD::VQSHRNs; break;
5643 case Intrinsic::arm_neon_vqshiftnu:
5644 VShiftOpc = ARMISD::VQSHRNu; break;
5645 case Intrinsic::arm_neon_vqshiftnsu:
5646 VShiftOpc = ARMISD::VQSHRNsu; break;
5647 case Intrinsic::arm_neon_vqrshiftns:
5648 VShiftOpc = ARMISD::VQRSHRNs; break;
5649 case Intrinsic::arm_neon_vqrshiftnu:
5650 VShiftOpc = ARMISD::VQRSHRNu; break;
5651 case Intrinsic::arm_neon_vqrshiftnsu:
5652 VShiftOpc = ARMISD::VQRSHRNsu; break;
5653 }
5654
5655 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005657 }
5658
5659 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00005660 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005661 int64_t Cnt;
5662 unsigned VShiftOpc = 0;
5663
5664 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
5665 VShiftOpc = ARMISD::VSLI;
5666 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
5667 VShiftOpc = ARMISD::VSRI;
5668 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005669 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005670 }
5671
5672 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
5673 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00005674 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005675 }
5676
5677 case Intrinsic::arm_neon_vqrshifts:
5678 case Intrinsic::arm_neon_vqrshiftu:
5679 // No immediate versions of these to check for.
5680 break;
5681 }
5682
5683 return SDValue();
5684}
5685
5686/// PerformShiftCombine - Checks for immediate versions of vector shifts and
5687/// lowers them. As with the vector shift intrinsics, this is done during DAG
5688/// combining instead of DAG legalizing because the build_vectors for 64-bit
5689/// vector element shift counts are generally not legal, and it is hard to see
5690/// their values after they get legalized to loads from a constant pool.
5691static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
5692 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00005693 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00005694
5695 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00005696 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5697 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00005698 return SDValue();
5699
5700 assert(ST->hasNEON() && "unexpected vector shift");
5701 int64_t Cnt;
5702
5703 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005704 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005705
5706 case ISD::SHL:
5707 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
5708 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005709 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005710 break;
5711
5712 case ISD::SRA:
5713 case ISD::SRL:
5714 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
5715 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
5716 ARMISD::VSHRs : ARMISD::VSHRu);
5717 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005719 }
5720 }
5721 return SDValue();
5722}
5723
5724/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
5725/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
5726static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
5727 const ARMSubtarget *ST) {
5728 SDValue N0 = N->getOperand(0);
5729
5730 // Check for sign- and zero-extensions of vector extract operations of 8-
5731 // and 16-bit vector elements. NEON supports these directly. They are
5732 // handled during DAG combining because type legalization will promote them
5733 // to 32-bit types and it is messy to recognize the operations after that.
5734 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5735 SDValue Vec = N0.getOperand(0);
5736 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005737 EVT VT = N->getValueType(0);
5738 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005739 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5740
Owen Anderson825b72b2009-08-11 20:47:22 +00005741 if (VT == MVT::i32 &&
5742 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00005743 TLI.isTypeLegal(Vec.getValueType()) &&
5744 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005745
5746 unsigned Opc = 0;
5747 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005748 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005749 case ISD::SIGN_EXTEND:
5750 Opc = ARMISD::VGETLANEs;
5751 break;
5752 case ISD::ZERO_EXTEND:
5753 case ISD::ANY_EXTEND:
5754 Opc = ARMISD::VGETLANEu;
5755 break;
5756 }
5757 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
5758 }
5759 }
5760
5761 return SDValue();
5762}
5763
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005764/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
5765/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
5766static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
5767 const ARMSubtarget *ST) {
5768 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00005769 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005770 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
5771 // a NaN; only do the transformation when it matches that behavior.
5772
5773 // For now only do this when using NEON for FP operations; if using VFP, it
5774 // is not obvious that the benefit outweighs the cost of switching to the
5775 // NEON pipeline.
5776 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
5777 N->getValueType(0) != MVT::f32)
5778 return SDValue();
5779
5780 SDValue CondLHS = N->getOperand(0);
5781 SDValue CondRHS = N->getOperand(1);
5782 SDValue LHS = N->getOperand(2);
5783 SDValue RHS = N->getOperand(3);
5784 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
5785
5786 unsigned Opcode = 0;
5787 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00005788 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005789 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00005790 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005791 IsReversed = true ; // x CC y ? y : x
5792 } else {
5793 return SDValue();
5794 }
5795
Bob Wilsone742bb52010-02-24 22:15:53 +00005796 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005797 switch (CC) {
5798 default: break;
5799 case ISD::SETOLT:
5800 case ISD::SETOLE:
5801 case ISD::SETLT:
5802 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005803 case ISD::SETULT:
5804 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005805 // If LHS is NaN, an ordered comparison will be false and the result will
5806 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
5807 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5808 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
5809 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5810 break;
5811 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
5812 // will return -0, so vmin can only be used for unsafe math or if one of
5813 // the operands is known to be nonzero.
5814 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
5815 !UnsafeFPMath &&
5816 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5817 break;
5818 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005819 break;
5820
5821 case ISD::SETOGT:
5822 case ISD::SETOGE:
5823 case ISD::SETGT:
5824 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005825 case ISD::SETUGT:
5826 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005827 // If LHS is NaN, an ordered comparison will be false and the result will
5828 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
5829 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5830 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
5831 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5832 break;
5833 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
5834 // will return +0, so vmax can only be used for unsafe math or if one of
5835 // the operands is known to be nonzero.
5836 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
5837 !UnsafeFPMath &&
5838 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5839 break;
5840 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005841 break;
5842 }
5843
5844 if (!Opcode)
5845 return SDValue();
5846 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
5847}
5848
Dan Gohman475871a2008-07-27 21:46:04 +00005849SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005850 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005851 switch (N->getOpcode()) {
5852 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005853 case ISD::ADD: return PerformADDCombine(N, DCI);
5854 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005855 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005856 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00005857 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00005858 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00005859 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005860 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00005861 case ISD::STORE: return PerformSTORECombine(N, DCI);
5862 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
5863 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00005864 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005865 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005866 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005867 case ISD::SHL:
5868 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005869 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005870 case ISD::SIGN_EXTEND:
5871 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005872 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
5873 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00005874 case ARMISD::VLD2DUP:
5875 case ARMISD::VLD3DUP:
5876 case ARMISD::VLD4DUP:
5877 return CombineBaseUpdate(N, DCI);
5878 case ISD::INTRINSIC_VOID:
5879 case ISD::INTRINSIC_W_CHAIN:
5880 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
5881 case Intrinsic::arm_neon_vld1:
5882 case Intrinsic::arm_neon_vld2:
5883 case Intrinsic::arm_neon_vld3:
5884 case Intrinsic::arm_neon_vld4:
5885 case Intrinsic::arm_neon_vld2lane:
5886 case Intrinsic::arm_neon_vld3lane:
5887 case Intrinsic::arm_neon_vld4lane:
5888 case Intrinsic::arm_neon_vst1:
5889 case Intrinsic::arm_neon_vst2:
5890 case Intrinsic::arm_neon_vst3:
5891 case Intrinsic::arm_neon_vst4:
5892 case Intrinsic::arm_neon_vst2lane:
5893 case Intrinsic::arm_neon_vst3lane:
5894 case Intrinsic::arm_neon_vst4lane:
5895 return CombineBaseUpdate(N, DCI);
5896 default: break;
5897 }
5898 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005899 }
Dan Gohman475871a2008-07-27 21:46:04 +00005900 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005901}
5902
Evan Cheng31959b12011-02-02 01:06:55 +00005903bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
5904 EVT VT) const {
5905 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
5906}
5907
Bill Wendlingaf566342009-08-15 21:21:19 +00005908bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00005909 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00005910 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00005911
5912 switch (VT.getSimpleVT().SimpleTy) {
5913 default:
5914 return false;
5915 case MVT::i8:
5916 case MVT::i16:
5917 case MVT::i32:
5918 return true;
5919 // FIXME: VLD1 etc with standard alignment is legal.
5920 }
5921}
5922
Evan Chenge6c835f2009-08-14 20:09:37 +00005923static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
5924 if (V < 0)
5925 return false;
5926
5927 unsigned Scale = 1;
5928 switch (VT.getSimpleVT().SimpleTy) {
5929 default: return false;
5930 case MVT::i1:
5931 case MVT::i8:
5932 // Scale == 1;
5933 break;
5934 case MVT::i16:
5935 // Scale == 2;
5936 Scale = 2;
5937 break;
5938 case MVT::i32:
5939 // Scale == 4;
5940 Scale = 4;
5941 break;
5942 }
5943
5944 if ((V & (Scale - 1)) != 0)
5945 return false;
5946 V /= Scale;
5947 return V == (V & ((1LL << 5) - 1));
5948}
5949
5950static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
5951 const ARMSubtarget *Subtarget) {
5952 bool isNeg = false;
5953 if (V < 0) {
5954 isNeg = true;
5955 V = - V;
5956 }
5957
5958 switch (VT.getSimpleVT().SimpleTy) {
5959 default: return false;
5960 case MVT::i1:
5961 case MVT::i8:
5962 case MVT::i16:
5963 case MVT::i32:
5964 // + imm12 or - imm8
5965 if (isNeg)
5966 return V == (V & ((1LL << 8) - 1));
5967 return V == (V & ((1LL << 12) - 1));
5968 case MVT::f32:
5969 case MVT::f64:
5970 // Same as ARM mode. FIXME: NEON?
5971 if (!Subtarget->hasVFP2())
5972 return false;
5973 if ((V & 3) != 0)
5974 return false;
5975 V >>= 2;
5976 return V == (V & ((1LL << 8) - 1));
5977 }
5978}
5979
Evan Chengb01fad62007-03-12 23:30:29 +00005980/// isLegalAddressImmediate - Return true if the integer value can be used
5981/// as the offset of the target addressing mode for load / store of the
5982/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00005983static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005984 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00005985 if (V == 0)
5986 return true;
5987
Evan Cheng65011532009-03-09 19:15:00 +00005988 if (!VT.isSimple())
5989 return false;
5990
Evan Chenge6c835f2009-08-14 20:09:37 +00005991 if (Subtarget->isThumb1Only())
5992 return isLegalT1AddressImmediate(V, VT);
5993 else if (Subtarget->isThumb2())
5994 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00005995
Evan Chenge6c835f2009-08-14 20:09:37 +00005996 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00005997 if (V < 0)
5998 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00005999 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006000 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 case MVT::i1:
6002 case MVT::i8:
6003 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006004 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006005 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006007 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006008 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 case MVT::f32:
6010 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006011 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006012 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006013 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006014 return false;
6015 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006016 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006017 }
Evan Chenga8e29892007-01-19 07:51:42 +00006018}
6019
Evan Chenge6c835f2009-08-14 20:09:37 +00006020bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6021 EVT VT) const {
6022 int Scale = AM.Scale;
6023 if (Scale < 0)
6024 return false;
6025
6026 switch (VT.getSimpleVT().SimpleTy) {
6027 default: return false;
6028 case MVT::i1:
6029 case MVT::i8:
6030 case MVT::i16:
6031 case MVT::i32:
6032 if (Scale == 1)
6033 return true;
6034 // r + r << imm
6035 Scale = Scale & ~1;
6036 return Scale == 2 || Scale == 4 || Scale == 8;
6037 case MVT::i64:
6038 // r + r
6039 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6040 return true;
6041 return false;
6042 case MVT::isVoid:
6043 // Note, we allow "void" uses (basically, uses that aren't loads or
6044 // stores), because arm allows folding a scale into many arithmetic
6045 // operations. This should be made more precise and revisited later.
6046
6047 // Allow r << imm, but the imm has to be a multiple of two.
6048 if (Scale & 1) return false;
6049 return isPowerOf2_32(Scale);
6050 }
6051}
6052
Chris Lattner37caf8c2007-04-09 23:33:39 +00006053/// isLegalAddressingMode - Return true if the addressing mode represented
6054/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006055bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006056 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006057 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006058 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006059 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006060
Chris Lattner37caf8c2007-04-09 23:33:39 +00006061 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006062 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006063 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006064
Chris Lattner37caf8c2007-04-09 23:33:39 +00006065 switch (AM.Scale) {
6066 case 0: // no scale reg, must be "r+i" or "r", or "i".
6067 break;
6068 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006069 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006070 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006071 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006072 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006073 // ARM doesn't support any R+R*scale+imm addr modes.
6074 if (AM.BaseOffs)
6075 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006076
Bob Wilson2c7dab12009-04-08 17:55:28 +00006077 if (!VT.isSimple())
6078 return false;
6079
Evan Chenge6c835f2009-08-14 20:09:37 +00006080 if (Subtarget->isThumb2())
6081 return isLegalT2ScaledAddressingMode(AM, VT);
6082
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006083 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006084 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006085 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006086 case MVT::i1:
6087 case MVT::i8:
6088 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006089 if (Scale < 0) Scale = -Scale;
6090 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006091 return true;
6092 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006093 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006094 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006095 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006096 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006097 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006098 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006099 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006100
Owen Anderson825b72b2009-08-11 20:47:22 +00006101 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006102 // Note, we allow "void" uses (basically, uses that aren't loads or
6103 // stores), because arm allows folding a scale into many arithmetic
6104 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006105
Chris Lattner37caf8c2007-04-09 23:33:39 +00006106 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006107 if (Scale & 1) return false;
6108 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006109 }
6110 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006111 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006112 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006113}
6114
Evan Cheng77e47512009-11-11 19:05:52 +00006115/// isLegalICmpImmediate - Return true if the specified immediate is legal
6116/// icmp immediate, that is the target has icmp instructions which can compare
6117/// a register against the immediate without having to materialize the
6118/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006119bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006120 if (!Subtarget->isThumb())
6121 return ARM_AM::getSOImmVal(Imm) != -1;
6122 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006123 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006124 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006125}
6126
Owen Andersone50ed302009-08-10 22:56:29 +00006127static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006128 bool isSEXTLoad, SDValue &Base,
6129 SDValue &Offset, bool &isInc,
6130 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006131 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6132 return false;
6133
Owen Anderson825b72b2009-08-11 20:47:22 +00006134 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006135 // AddressingMode 3
6136 Base = Ptr->getOperand(0);
6137 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006138 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006139 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006140 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006141 isInc = false;
6142 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6143 return true;
6144 }
6145 }
6146 isInc = (Ptr->getOpcode() == ISD::ADD);
6147 Offset = Ptr->getOperand(1);
6148 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006149 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006150 // AddressingMode 2
6151 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006152 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006153 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006154 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006155 isInc = false;
6156 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6157 Base = Ptr->getOperand(0);
6158 return true;
6159 }
6160 }
6161
6162 if (Ptr->getOpcode() == ISD::ADD) {
6163 isInc = true;
6164 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6165 if (ShOpcVal != ARM_AM::no_shift) {
6166 Base = Ptr->getOperand(1);
6167 Offset = Ptr->getOperand(0);
6168 } else {
6169 Base = Ptr->getOperand(0);
6170 Offset = Ptr->getOperand(1);
6171 }
6172 return true;
6173 }
6174
6175 isInc = (Ptr->getOpcode() == ISD::ADD);
6176 Base = Ptr->getOperand(0);
6177 Offset = Ptr->getOperand(1);
6178 return true;
6179 }
6180
Jim Grosbache5165492009-11-09 00:11:35 +00006181 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006182 return false;
6183}
6184
Owen Andersone50ed302009-08-10 22:56:29 +00006185static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006186 bool isSEXTLoad, SDValue &Base,
6187 SDValue &Offset, bool &isInc,
6188 SelectionDAG &DAG) {
6189 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6190 return false;
6191
6192 Base = Ptr->getOperand(0);
6193 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6194 int RHSC = (int)RHS->getZExtValue();
6195 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6196 assert(Ptr->getOpcode() == ISD::ADD);
6197 isInc = false;
6198 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6199 return true;
6200 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6201 isInc = Ptr->getOpcode() == ISD::ADD;
6202 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6203 return true;
6204 }
6205 }
6206
6207 return false;
6208}
6209
Evan Chenga8e29892007-01-19 07:51:42 +00006210/// getPreIndexedAddressParts - returns true by value, base pointer and
6211/// offset pointer and addressing mode by reference if the node's address
6212/// can be legally represented as pre-indexed load / store address.
6213bool
Dan Gohman475871a2008-07-27 21:46:04 +00006214ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6215 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006216 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006217 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006218 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006219 return false;
6220
Owen Andersone50ed302009-08-10 22:56:29 +00006221 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006222 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006223 bool isSEXTLoad = false;
6224 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6225 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006226 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006227 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6228 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6229 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006230 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006231 } else
6232 return false;
6233
6234 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006235 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006236 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006237 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6238 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006239 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006240 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006241 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006242 if (!isLegal)
6243 return false;
6244
6245 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6246 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006247}
6248
6249/// getPostIndexedAddressParts - returns true by value, base pointer and
6250/// offset pointer and addressing mode by reference if this node can be
6251/// combined with a load / store to form a post-indexed load / store.
6252bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006253 SDValue &Base,
6254 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006255 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006256 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006257 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006258 return false;
6259
Owen Andersone50ed302009-08-10 22:56:29 +00006260 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006261 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006262 bool isSEXTLoad = false;
6263 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006264 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006265 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006266 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6267 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006268 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006269 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006270 } else
6271 return false;
6272
6273 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006274 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006275 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006276 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006277 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006278 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006279 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6280 isInc, DAG);
6281 if (!isLegal)
6282 return false;
6283
Evan Cheng28dad2a2010-05-18 21:31:17 +00006284 if (Ptr != Base) {
6285 // Swap base ptr and offset to catch more post-index load / store when
6286 // it's legal. In Thumb2 mode, offset must be an immediate.
6287 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6288 !Subtarget->isThumb2())
6289 std::swap(Base, Offset);
6290
6291 // Post-indexed load / store update the base pointer.
6292 if (Ptr != Base)
6293 return false;
6294 }
6295
Evan Chenge88d5ce2009-07-02 07:28:31 +00006296 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6297 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006298}
6299
Dan Gohman475871a2008-07-27 21:46:04 +00006300void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006301 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006302 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006303 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006304 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006305 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006306 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006307 switch (Op.getOpcode()) {
6308 default: break;
6309 case ARMISD::CMOV: {
6310 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006311 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006312 if (KnownZero == 0 && KnownOne == 0) return;
6313
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006314 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006315 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6316 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006317 KnownZero &= KnownZeroRHS;
6318 KnownOne &= KnownOneRHS;
6319 return;
6320 }
6321 }
6322}
6323
6324//===----------------------------------------------------------------------===//
6325// ARM Inline Assembly Support
6326//===----------------------------------------------------------------------===//
6327
Evan Cheng55d42002011-01-08 01:24:27 +00006328bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6329 // Looking for "rev" which is V6+.
6330 if (!Subtarget->hasV6Ops())
6331 return false;
6332
6333 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6334 std::string AsmStr = IA->getAsmString();
6335 SmallVector<StringRef, 4> AsmPieces;
6336 SplitString(AsmStr, AsmPieces, ";\n");
6337
6338 switch (AsmPieces.size()) {
6339 default: return false;
6340 case 1:
6341 AsmStr = AsmPieces[0];
6342 AsmPieces.clear();
6343 SplitString(AsmStr, AsmPieces, " \t,");
6344
6345 // rev $0, $1
6346 if (AsmPieces.size() == 3 &&
6347 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6348 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6349 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6350 if (Ty && Ty->getBitWidth() == 32)
6351 return IntrinsicLowering::LowerToByteSwap(CI);
6352 }
6353 break;
6354 }
6355
6356 return false;
6357}
6358
Evan Chenga8e29892007-01-19 07:51:42 +00006359/// getConstraintType - Given a constraint letter, return the type of
6360/// constraint it is for this target.
6361ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006362ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6363 if (Constraint.size() == 1) {
6364 switch (Constraint[0]) {
6365 default: break;
6366 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006367 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006368 }
Evan Chenga8e29892007-01-19 07:51:42 +00006369 }
Chris Lattner4234f572007-03-25 02:14:49 +00006370 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006371}
6372
John Thompson44ab89e2010-10-29 17:29:13 +00006373/// Examine constraint type and operand type and determine a weight value.
6374/// This object must already have been set up with the operand type
6375/// and the current alternative constraint selected.
6376TargetLowering::ConstraintWeight
6377ARMTargetLowering::getSingleConstraintMatchWeight(
6378 AsmOperandInfo &info, const char *constraint) const {
6379 ConstraintWeight weight = CW_Invalid;
6380 Value *CallOperandVal = info.CallOperandVal;
6381 // If we don't have a value, we can't do a match,
6382 // but allow it at the lowest weight.
6383 if (CallOperandVal == NULL)
6384 return CW_Default;
6385 const Type *type = CallOperandVal->getType();
6386 // Look at the constraint type.
6387 switch (*constraint) {
6388 default:
6389 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6390 break;
6391 case 'l':
6392 if (type->isIntegerTy()) {
6393 if (Subtarget->isThumb())
6394 weight = CW_SpecificReg;
6395 else
6396 weight = CW_Register;
6397 }
6398 break;
6399 case 'w':
6400 if (type->isFloatingPointTy())
6401 weight = CW_Register;
6402 break;
6403 }
6404 return weight;
6405}
6406
Bob Wilson2dc4f542009-03-20 22:42:55 +00006407std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006408ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006409 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006410 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006411 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006412 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006413 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006414 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006415 return std::make_pair(0U, ARM::tGPRRegisterClass);
6416 else
6417 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006418 case 'r':
6419 return std::make_pair(0U, ARM::GPRRegisterClass);
6420 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006421 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006422 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006423 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006424 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006425 if (VT.getSizeInBits() == 128)
6426 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006427 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006428 }
6429 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006430 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006431 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006432
Evan Chenga8e29892007-01-19 07:51:42 +00006433 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6434}
6435
6436std::vector<unsigned> ARMTargetLowering::
6437getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006438 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006439 if (Constraint.size() != 1)
6440 return std::vector<unsigned>();
6441
6442 switch (Constraint[0]) { // GCC ARM Constraint Letters
6443 default: break;
6444 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006445 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6446 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6447 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006448 case 'r':
6449 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6450 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6451 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6452 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006453 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006454 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006455 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6456 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6457 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6458 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6459 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6460 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6461 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6462 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006463 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006464 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6465 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6466 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6467 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006468 if (VT.getSizeInBits() == 128)
6469 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6470 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006471 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006472 }
6473
6474 return std::vector<unsigned>();
6475}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006476
6477/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6478/// vector. If it is invalid, don't add anything to Ops.
6479void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6480 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006481 std::vector<SDValue>&Ops,
6482 SelectionDAG &DAG) const {
6483 SDValue Result(0, 0);
6484
6485 switch (Constraint) {
6486 default: break;
6487 case 'I': case 'J': case 'K': case 'L':
6488 case 'M': case 'N': case 'O':
6489 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6490 if (!C)
6491 return;
6492
6493 int64_t CVal64 = C->getSExtValue();
6494 int CVal = (int) CVal64;
6495 // None of these constraints allow values larger than 32 bits. Check
6496 // that the value fits in an int.
6497 if (CVal != CVal64)
6498 return;
6499
6500 switch (Constraint) {
6501 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006502 if (Subtarget->isThumb1Only()) {
6503 // This must be a constant between 0 and 255, for ADD
6504 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006505 if (CVal >= 0 && CVal <= 255)
6506 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006507 } else if (Subtarget->isThumb2()) {
6508 // A constant that can be used as an immediate value in a
6509 // data-processing instruction.
6510 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6511 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006512 } else {
6513 // A constant that can be used as an immediate value in a
6514 // data-processing instruction.
6515 if (ARM_AM::getSOImmVal(CVal) != -1)
6516 break;
6517 }
6518 return;
6519
6520 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006521 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006522 // This must be a constant between -255 and -1, for negated ADD
6523 // immediates. This can be used in GCC with an "n" modifier that
6524 // prints the negated value, for use with SUB instructions. It is
6525 // not useful otherwise but is implemented for compatibility.
6526 if (CVal >= -255 && CVal <= -1)
6527 break;
6528 } else {
6529 // This must be a constant between -4095 and 4095. It is not clear
6530 // what this constraint is intended for. Implemented for
6531 // compatibility with GCC.
6532 if (CVal >= -4095 && CVal <= 4095)
6533 break;
6534 }
6535 return;
6536
6537 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006538 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006539 // A 32-bit value where only one byte has a nonzero value. Exclude
6540 // zero to match GCC. This constraint is used by GCC internally for
6541 // constants that can be loaded with a move/shift combination.
6542 // It is not useful otherwise but is implemented for compatibility.
6543 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6544 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006545 } else if (Subtarget->isThumb2()) {
6546 // A constant whose bitwise inverse can be used as an immediate
6547 // value in a data-processing instruction. This can be used in GCC
6548 // with a "B" modifier that prints the inverted value, for use with
6549 // BIC and MVN instructions. It is not useful otherwise but is
6550 // implemented for compatibility.
6551 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
6552 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006553 } else {
6554 // A constant whose bitwise inverse can be used as an immediate
6555 // value in a data-processing instruction. This can be used in GCC
6556 // with a "B" modifier that prints the inverted value, for use with
6557 // BIC and MVN instructions. It is not useful otherwise but is
6558 // implemented for compatibility.
6559 if (ARM_AM::getSOImmVal(~CVal) != -1)
6560 break;
6561 }
6562 return;
6563
6564 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006565 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006566 // This must be a constant between -7 and 7,
6567 // for 3-operand ADD/SUB immediate instructions.
6568 if (CVal >= -7 && CVal < 7)
6569 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006570 } else if (Subtarget->isThumb2()) {
6571 // A constant whose negation can be used as an immediate value in a
6572 // data-processing instruction. This can be used in GCC with an "n"
6573 // modifier that prints the negated value, for use with SUB
6574 // instructions. It is not useful otherwise but is implemented for
6575 // compatibility.
6576 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
6577 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006578 } else {
6579 // A constant whose negation can be used as an immediate value in a
6580 // data-processing instruction. This can be used in GCC with an "n"
6581 // modifier that prints the negated value, for use with SUB
6582 // instructions. It is not useful otherwise but is implemented for
6583 // compatibility.
6584 if (ARM_AM::getSOImmVal(-CVal) != -1)
6585 break;
6586 }
6587 return;
6588
6589 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006590 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006591 // This must be a multiple of 4 between 0 and 1020, for
6592 // ADD sp + immediate.
6593 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
6594 break;
6595 } else {
6596 // A power of two or a constant between 0 and 32. This is used in
6597 // GCC for the shift amount on shifted register operands, but it is
6598 // useful in general for any shift amounts.
6599 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
6600 break;
6601 }
6602 return;
6603
6604 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006605 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006606 // This must be a constant between 0 and 31, for shift amounts.
6607 if (CVal >= 0 && CVal <= 31)
6608 break;
6609 }
6610 return;
6611
6612 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006613 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006614 // This must be a multiple of 4 between -508 and 508, for
6615 // ADD/SUB sp = sp + immediate.
6616 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
6617 break;
6618 }
6619 return;
6620 }
6621 Result = DAG.getTargetConstant(CVal, Op.getValueType());
6622 break;
6623 }
6624
6625 if (Result.getNode()) {
6626 Ops.push_back(Result);
6627 return;
6628 }
Dale Johannesen1784d162010-06-25 21:55:36 +00006629 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006630}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00006631
6632bool
6633ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6634 // The ARM target isn't yet aware of offsets.
6635 return false;
6636}
Evan Cheng39382422009-10-28 01:44:26 +00006637
6638int ARM::getVFPf32Imm(const APFloat &FPImm) {
6639 APInt Imm = FPImm.bitcastToAPInt();
6640 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
6641 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
6642 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
6643
6644 // We can handle 4 bits of mantissa.
6645 // mantissa = (16+UInt(e:f:g:h))/16.
6646 if (Mantissa & 0x7ffff)
6647 return -1;
6648 Mantissa >>= 19;
6649 if ((Mantissa & 0xf) != Mantissa)
6650 return -1;
6651
6652 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6653 if (Exp < -3 || Exp > 4)
6654 return -1;
6655 Exp = ((Exp+3) & 0x7) ^ 4;
6656
6657 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6658}
6659
6660int ARM::getVFPf64Imm(const APFloat &FPImm) {
6661 APInt Imm = FPImm.bitcastToAPInt();
6662 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
6663 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
6664 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
6665
6666 // We can handle 4 bits of mantissa.
6667 // mantissa = (16+UInt(e:f:g:h))/16.
6668 if (Mantissa & 0xffffffffffffLL)
6669 return -1;
6670 Mantissa >>= 48;
6671 if ((Mantissa & 0xf) != Mantissa)
6672 return -1;
6673
6674 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6675 if (Exp < -3 || Exp > 4)
6676 return -1;
6677 Exp = ((Exp+3) & 0x7) ^ 4;
6678
6679 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6680}
6681
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006682bool ARM::isBitFieldInvertedMask(unsigned v) {
6683 if (v == 0xffffffff)
6684 return 0;
6685 // there can be 1's on either or both "outsides", all the "inside"
6686 // bits must be 0's
6687 unsigned int lsb = 0, msb = 31;
6688 while (v & (1 << msb)) --msb;
6689 while (v & (1 << lsb)) ++lsb;
6690 for (unsigned int i = lsb; i <= msb; ++i) {
6691 if (v & (1 << i))
6692 return 0;
6693 }
6694 return 1;
6695}
6696
Evan Cheng39382422009-10-28 01:44:26 +00006697/// isFPImmLegal - Returns true if the target can instruction select the
6698/// specified FP immediate natively. If false, the legalizer will
6699/// materialize the FP immediate as a load from a constant pool.
6700bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
6701 if (!Subtarget->hasVFP3())
6702 return false;
6703 if (VT == MVT::f32)
6704 return ARM::getVFPf32Imm(Imm) != -1;
6705 if (VT == MVT::f64)
6706 return ARM::getVFPf64Imm(Imm) != -1;
6707 return false;
6708}
Bob Wilson65ffec42010-09-21 17:56:22 +00006709
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006710/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00006711/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
6712/// specified in the intrinsic calls.
6713bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
6714 const CallInst &I,
6715 unsigned Intrinsic) const {
6716 switch (Intrinsic) {
6717 case Intrinsic::arm_neon_vld1:
6718 case Intrinsic::arm_neon_vld2:
6719 case Intrinsic::arm_neon_vld3:
6720 case Intrinsic::arm_neon_vld4:
6721 case Intrinsic::arm_neon_vld2lane:
6722 case Intrinsic::arm_neon_vld3lane:
6723 case Intrinsic::arm_neon_vld4lane: {
6724 Info.opc = ISD::INTRINSIC_W_CHAIN;
6725 // Conservatively set memVT to the entire set of vectors loaded.
6726 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
6727 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6728 Info.ptrVal = I.getArgOperand(0);
6729 Info.offset = 0;
6730 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6731 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6732 Info.vol = false; // volatile loads with NEON intrinsics not supported
6733 Info.readMem = true;
6734 Info.writeMem = false;
6735 return true;
6736 }
6737 case Intrinsic::arm_neon_vst1:
6738 case Intrinsic::arm_neon_vst2:
6739 case Intrinsic::arm_neon_vst3:
6740 case Intrinsic::arm_neon_vst4:
6741 case Intrinsic::arm_neon_vst2lane:
6742 case Intrinsic::arm_neon_vst3lane:
6743 case Intrinsic::arm_neon_vst4lane: {
6744 Info.opc = ISD::INTRINSIC_VOID;
6745 // Conservatively set memVT to the entire set of vectors stored.
6746 unsigned NumElts = 0;
6747 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
6748 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
6749 if (!ArgTy->isVectorTy())
6750 break;
6751 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
6752 }
6753 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6754 Info.ptrVal = I.getArgOperand(0);
6755 Info.offset = 0;
6756 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6757 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6758 Info.vol = false; // volatile stores with NEON intrinsics not supported
6759 Info.readMem = false;
6760 Info.writeMem = true;
6761 return true;
6762 }
6763 default:
6764 break;
6765 }
6766
6767 return false;
6768}