blob: fe398602717b36d78e1457d4aa0a1550df4f1a42 [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();
Evan Chengc143dd42011-02-11 02:28:55 +00002836 bool F2IisFast = Subtarget->isCortexA9() ||
2837 Tmp0.getOpcode() == ISD::BITCAST || Tmp0.getOpcode() == ARMISD::VMOVDRR;
2838
2839 // Bitcast operand 1 to i32.
2840 if (SrcVT == MVT::f64)
2841 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2842 &Tmp1, 1).getValue(1);
2843 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
2844
2845 // If float to int conversion isn't going to be super expensive, then simply
2846 // or in the signbit.
2847 if (F2IisFast) {
2848 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
2849 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
2850 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
2851 if (VT == MVT::f32) {
2852 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
2853 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
2854 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
2855 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
2856 }
2857
2858 // f64: Or the high part with signbit and then combine two parts.
2859 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2860 &Tmp0, 1);
2861 SDValue Lo = Tmp0.getValue(0);
2862 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
2863 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
2864 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
2865 }
2866
2867 // Remove the signbit of operand 0.
2868 Tmp0 = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2869
2870 // If operand 1 signbit is one, then negate operand 0.
2871 SDValue ARMcc;
2872 SDValue Cmp = getARMCmp(Tmp1, DAG.getConstant(0, MVT::i32),
2873 ISD::SETLT, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002874 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Chengc143dd42011-02-11 02:28:55 +00002875 return DAG.getNode(ARMISD::CNEG, dl, VT, Tmp0, Tmp0, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002876}
2877
Evan Cheng2457f2c2010-05-22 01:47:14 +00002878SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2879 MachineFunction &MF = DAG.getMachineFunction();
2880 MachineFrameInfo *MFI = MF.getFrameInfo();
2881 MFI->setReturnAddressIsTaken(true);
2882
2883 EVT VT = Op.getValueType();
2884 DebugLoc dl = Op.getDebugLoc();
2885 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2886 if (Depth) {
2887 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2888 SDValue Offset = DAG.getConstant(4, MVT::i32);
2889 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2890 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002891 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002892 }
2893
2894 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patele9a7ea62011-01-31 21:38:14 +00002895 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32), dl);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002896 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2897}
2898
Dan Gohmand858e902010-04-17 15:26:15 +00002899SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002900 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2901 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002902
Owen Andersone50ed302009-08-10 22:56:29 +00002903 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002904 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2905 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002906 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002907 ? ARM::R7 : ARM::R11;
2908 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2909 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002910 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
2911 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00002912 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002913 return FrameAddr;
2914}
2915
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002916/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00002917/// expand a bit convert where either the source or destination type is i64 to
2918/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2919/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2920/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002921static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002922 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2923 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002924 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002925
Bob Wilson9f3f0612010-04-17 05:30:19 +00002926 // This function is only supposed to be called for i64 types, either as the
2927 // source or destination of the bit convert.
2928 EVT SrcVT = Op.getValueType();
2929 EVT DstVT = N->getValueType(0);
2930 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002931 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002932
Bob Wilson9f3f0612010-04-17 05:30:19 +00002933 // Turn i64->f64 into VMOVDRR.
2934 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002935 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2936 DAG.getConstant(0, MVT::i32));
2937 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2938 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002939 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00002940 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002941 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002942
Jim Grosbache5165492009-11-09 00:11:35 +00002943 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002944 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2945 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2946 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2947 // Merge the pieces into a single i64 value.
2948 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2949 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002950
Bob Wilson9f3f0612010-04-17 05:30:19 +00002951 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002952}
2953
Bob Wilson5bafff32009-06-22 23:27:02 +00002954/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002955/// Zero vectors are used to represent vector negation and in those cases
2956/// will be implemented with the NEON VNEG instruction. However, VNEG does
2957/// not support i64 elements, so sometimes the zero vectors will need to be
2958/// explicitly constructed. Regardless, use a canonical VMOV to create the
2959/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002960static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002961 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002962 // The canonical modified immediate encoding of a zero vector is....0!
2963 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2964 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2965 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002966 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002967}
2968
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002969/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2970/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002971SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2972 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002973 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2974 EVT VT = Op.getValueType();
2975 unsigned VTBits = VT.getSizeInBits();
2976 DebugLoc dl = Op.getDebugLoc();
2977 SDValue ShOpLo = Op.getOperand(0);
2978 SDValue ShOpHi = Op.getOperand(1);
2979 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002980 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002981 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002982
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002983 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2984
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002985 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, ShAmt);
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, RevShAmt);
2991 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002992 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002993
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 Grosbachbcf2f2c2009-10-31 21:42:19 +00002997 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002998 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002999 CCR, Cmp);
3000
3001 SDValue Ops[2] = { Lo, Hi };
3002 return DAG.getMergeValues(Ops, 2, dl);
3003}
3004
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003005/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3006/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003007SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3008 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003009 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3010 EVT VT = Op.getValueType();
3011 unsigned VTBits = VT.getSizeInBits();
3012 DebugLoc dl = Op.getDebugLoc();
3013 SDValue ShOpLo = Op.getOperand(0);
3014 SDValue ShOpHi = Op.getOperand(1);
3015 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003016 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003017
3018 assert(Op.getOpcode() == ISD::SHL_PARTS);
3019 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3020 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3021 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3022 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3023 DAG.getConstant(VTBits, MVT::i32));
3024 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3025 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3026
3027 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3028 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3029 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003030 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003031 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003032 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003033 CCR, Cmp);
3034
3035 SDValue Ops[2] = { Lo, Hi };
3036 return DAG.getMergeValues(Ops, 2, dl);
3037}
3038
Jim Grosbach4725ca72010-09-08 03:54:02 +00003039SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003040 SelectionDAG &DAG) const {
3041 // The rounding mode is in bits 23:22 of the FPSCR.
3042 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3043 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3044 // so that the shift + and get folded into a bitfield extract.
3045 DebugLoc dl = Op.getDebugLoc();
3046 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3047 DAG.getConstant(Intrinsic::arm_get_fpscr,
3048 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003049 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003050 DAG.getConstant(1U << 22, MVT::i32));
3051 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3052 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003053 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003054 DAG.getConstant(3, MVT::i32));
3055}
3056
Jim Grosbach3482c802010-01-18 19:58:49 +00003057static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3058 const ARMSubtarget *ST) {
3059 EVT VT = N->getValueType(0);
3060 DebugLoc dl = N->getDebugLoc();
3061
3062 if (!ST->hasV6T2Ops())
3063 return SDValue();
3064
3065 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3066 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3067}
3068
Bob Wilson5bafff32009-06-22 23:27:02 +00003069static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3070 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003071 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003072 DebugLoc dl = N->getDebugLoc();
3073
Bob Wilsond5448bb2010-11-18 21:16:28 +00003074 if (!VT.isVector())
3075 return SDValue();
3076
Bob Wilson5bafff32009-06-22 23:27:02 +00003077 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003078 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003079
Bob Wilsond5448bb2010-11-18 21:16:28 +00003080 // Left shifts translate directly to the vshiftu intrinsic.
3081 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003082 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003083 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3084 N->getOperand(0), N->getOperand(1));
3085
3086 assert((N->getOpcode() == ISD::SRA ||
3087 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3088
3089 // NEON uses the same intrinsics for both left and right shifts. For
3090 // right shifts, the shift amounts are negative, so negate the vector of
3091 // shift amounts.
3092 EVT ShiftVT = N->getOperand(1).getValueType();
3093 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3094 getZeroVector(ShiftVT, DAG, dl),
3095 N->getOperand(1));
3096 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3097 Intrinsic::arm_neon_vshifts :
3098 Intrinsic::arm_neon_vshiftu);
3099 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3100 DAG.getConstant(vshiftInt, MVT::i32),
3101 N->getOperand(0), NegatedCount);
3102}
3103
3104static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3105 const ARMSubtarget *ST) {
3106 EVT VT = N->getValueType(0);
3107 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003108
Eli Friedmance392eb2009-08-22 03:13:10 +00003109 // We can get here for a node like i32 = ISD::SHL i32, i64
3110 if (VT != MVT::i64)
3111 return SDValue();
3112
3113 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003114 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003115
Chris Lattner27a6c732007-11-24 07:07:01 +00003116 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3117 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003118 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003119 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003120
Chris Lattner27a6c732007-11-24 07:07:01 +00003121 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003122 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003123
Chris Lattner27a6c732007-11-24 07:07:01 +00003124 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003126 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003128 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003129
Chris Lattner27a6c732007-11-24 07:07:01 +00003130 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3131 // captures the result into a carry flag.
3132 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003133 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003134
Chris Lattner27a6c732007-11-24 07:07:01 +00003135 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003136 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003137
Chris Lattner27a6c732007-11-24 07:07:01 +00003138 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003140}
3141
Bob Wilson5bafff32009-06-22 23:27:02 +00003142static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3143 SDValue TmpOp0, TmpOp1;
3144 bool Invert = false;
3145 bool Swap = false;
3146 unsigned Opc = 0;
3147
3148 SDValue Op0 = Op.getOperand(0);
3149 SDValue Op1 = Op.getOperand(1);
3150 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003151 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003152 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3153 DebugLoc dl = Op.getDebugLoc();
3154
3155 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3156 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003157 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003158 case ISD::SETUNE:
3159 case ISD::SETNE: Invert = true; // Fallthrough
3160 case ISD::SETOEQ:
3161 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3162 case ISD::SETOLT:
3163 case ISD::SETLT: Swap = true; // Fallthrough
3164 case ISD::SETOGT:
3165 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3166 case ISD::SETOLE:
3167 case ISD::SETLE: Swap = true; // Fallthrough
3168 case ISD::SETOGE:
3169 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3170 case ISD::SETUGE: Swap = true; // Fallthrough
3171 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3172 case ISD::SETUGT: Swap = true; // Fallthrough
3173 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3174 case ISD::SETUEQ: Invert = true; // Fallthrough
3175 case ISD::SETONE:
3176 // Expand this to (OLT | OGT).
3177 TmpOp0 = Op0;
3178 TmpOp1 = Op1;
3179 Opc = ISD::OR;
3180 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3181 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3182 break;
3183 case ISD::SETUO: Invert = true; // Fallthrough
3184 case ISD::SETO:
3185 // Expand this to (OLT | OGE).
3186 TmpOp0 = Op0;
3187 TmpOp1 = Op1;
3188 Opc = ISD::OR;
3189 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3190 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3191 break;
3192 }
3193 } else {
3194 // Integer comparisons.
3195 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003196 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003197 case ISD::SETNE: Invert = true;
3198 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3199 case ISD::SETLT: Swap = true;
3200 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3201 case ISD::SETLE: Swap = true;
3202 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3203 case ISD::SETULT: Swap = true;
3204 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3205 case ISD::SETULE: Swap = true;
3206 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3207 }
3208
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003209 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003210 if (Opc == ARMISD::VCEQ) {
3211
3212 SDValue AndOp;
3213 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3214 AndOp = Op0;
3215 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3216 AndOp = Op1;
3217
3218 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003219 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003220 AndOp = AndOp.getOperand(0);
3221
3222 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3223 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003224 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3225 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003226 Invert = !Invert;
3227 }
3228 }
3229 }
3230
3231 if (Swap)
3232 std::swap(Op0, Op1);
3233
Owen Andersonc24cb352010-11-08 23:21:22 +00003234 // If one of the operands is a constant vector zero, attempt to fold the
3235 // comparison to a specialized compare-against-zero form.
3236 SDValue SingleOp;
3237 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3238 SingleOp = Op0;
3239 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3240 if (Opc == ARMISD::VCGE)
3241 Opc = ARMISD::VCLEZ;
3242 else if (Opc == ARMISD::VCGT)
3243 Opc = ARMISD::VCLTZ;
3244 SingleOp = Op1;
3245 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003246
Owen Andersonc24cb352010-11-08 23:21:22 +00003247 SDValue Result;
3248 if (SingleOp.getNode()) {
3249 switch (Opc) {
3250 case ARMISD::VCEQ:
3251 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3252 case ARMISD::VCGE:
3253 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3254 case ARMISD::VCLEZ:
3255 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3256 case ARMISD::VCGT:
3257 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3258 case ARMISD::VCLTZ:
3259 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3260 default:
3261 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3262 }
3263 } else {
3264 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3265 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003266
3267 if (Invert)
3268 Result = DAG.getNOT(dl, Result, VT);
3269
3270 return Result;
3271}
3272
Bob Wilsond3c42842010-06-14 22:19:57 +00003273/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3274/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003275/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003276static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3277 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003278 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003279 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003280
Bob Wilson827b2102010-06-15 19:05:35 +00003281 // SplatBitSize is set to the smallest size that splats the vector, so a
3282 // zero vector will always have SplatBitSize == 8. However, NEON modified
3283 // immediate instructions others than VMOV do not support the 8-bit encoding
3284 // of a zero vector, and the default encoding of zero is supposed to be the
3285 // 32-bit version.
3286 if (SplatBits == 0)
3287 SplatBitSize = 32;
3288
Bob Wilson5bafff32009-06-22 23:27:02 +00003289 switch (SplatBitSize) {
3290 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003291 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003292 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003293 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003294 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003295 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003296 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003297 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003298 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003299
3300 case 16:
3301 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003302 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003303 if ((SplatBits & ~0xff) == 0) {
3304 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003305 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003306 Imm = SplatBits;
3307 break;
3308 }
3309 if ((SplatBits & ~0xff00) == 0) {
3310 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003311 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003312 Imm = SplatBits >> 8;
3313 break;
3314 }
3315 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003316
3317 case 32:
3318 // NEON's 32-bit VMOV supports splat values where:
3319 // * only one byte is nonzero, or
3320 // * the least significant byte is 0xff and the second byte is nonzero, or
3321 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003322 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003323 if ((SplatBits & ~0xff) == 0) {
3324 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003325 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003326 Imm = SplatBits;
3327 break;
3328 }
3329 if ((SplatBits & ~0xff00) == 0) {
3330 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003331 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003332 Imm = SplatBits >> 8;
3333 break;
3334 }
3335 if ((SplatBits & ~0xff0000) == 0) {
3336 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003337 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003338 Imm = SplatBits >> 16;
3339 break;
3340 }
3341 if ((SplatBits & ~0xff000000) == 0) {
3342 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003343 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003344 Imm = SplatBits >> 24;
3345 break;
3346 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003347
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003348 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3349 if (type == OtherModImm) return SDValue();
3350
Bob Wilson5bafff32009-06-22 23:27:02 +00003351 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003352 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3353 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003354 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003355 Imm = SplatBits >> 8;
3356 SplatBits |= 0xff;
3357 break;
3358 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003359
3360 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003361 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3362 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003363 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003364 Imm = SplatBits >> 16;
3365 SplatBits |= 0xffff;
3366 break;
3367 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003368
3369 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3370 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3371 // VMOV.I32. A (very) minor optimization would be to replicate the value
3372 // and fall through here to test for a valid 64-bit splat. But, then the
3373 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003374 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003375
3376 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003377 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003378 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003379 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003380 uint64_t BitMask = 0xff;
3381 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003382 unsigned ImmMask = 1;
3383 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003384 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003385 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003386 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003387 Imm |= ImmMask;
3388 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003389 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003390 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003391 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003392 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003393 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003394 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003395 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003396 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003397 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003398 break;
3399 }
3400
Bob Wilson1a913ed2010-06-11 21:34:50 +00003401 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003402 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003403 return SDValue();
3404 }
3405
Bob Wilsoncba270d2010-07-13 21:16:48 +00003406 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3407 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003408}
3409
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003410static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3411 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003412 unsigned NumElts = VT.getVectorNumElements();
3413 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003414
3415 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3416 if (M[0] < 0)
3417 return false;
3418
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003419 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003420
3421 // If this is a VEXT shuffle, the immediate value is the index of the first
3422 // element. The other shuffle indices must be the successive elements after
3423 // the first one.
3424 unsigned ExpectedElt = Imm;
3425 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003426 // Increment the expected index. If it wraps around, it may still be
3427 // a VEXT but the source vectors must be swapped.
3428 ExpectedElt += 1;
3429 if (ExpectedElt == NumElts * 2) {
3430 ExpectedElt = 0;
3431 ReverseVEXT = true;
3432 }
3433
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003434 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003435 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003436 return false;
3437 }
3438
3439 // Adjust the index value if the source operands will be swapped.
3440 if (ReverseVEXT)
3441 Imm -= NumElts;
3442
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003443 return true;
3444}
3445
Bob Wilson8bb9e482009-07-26 00:39:34 +00003446/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3447/// instruction with the specified blocksize. (The order of the elements
3448/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003449static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3450 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003451 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3452 "Only possible block sizes for VREV are: 16, 32, 64");
3453
Bob Wilson8bb9e482009-07-26 00:39:34 +00003454 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003455 if (EltSz == 64)
3456 return false;
3457
3458 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003459 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003460 // If the first shuffle index is UNDEF, be optimistic.
3461 if (M[0] < 0)
3462 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003463
3464 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3465 return false;
3466
3467 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003468 if (M[i] < 0) continue; // ignore UNDEF indices
3469 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003470 return false;
3471 }
3472
3473 return true;
3474}
3475
Bob Wilsonc692cb72009-08-21 20:54:19 +00003476static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3477 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003478 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3479 if (EltSz == 64)
3480 return false;
3481
Bob Wilsonc692cb72009-08-21 20:54:19 +00003482 unsigned NumElts = VT.getVectorNumElements();
3483 WhichResult = (M[0] == 0 ? 0 : 1);
3484 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003485 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3486 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003487 return false;
3488 }
3489 return true;
3490}
3491
Bob Wilson324f4f12009-12-03 06:40:55 +00003492/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3493/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3494/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3495static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3496 unsigned &WhichResult) {
3497 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3498 if (EltSz == 64)
3499 return false;
3500
3501 unsigned NumElts = VT.getVectorNumElements();
3502 WhichResult = (M[0] == 0 ? 0 : 1);
3503 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003504 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3505 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003506 return false;
3507 }
3508 return true;
3509}
3510
Bob Wilsonc692cb72009-08-21 20:54:19 +00003511static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3512 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003513 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3514 if (EltSz == 64)
3515 return false;
3516
Bob Wilsonc692cb72009-08-21 20:54:19 +00003517 unsigned NumElts = VT.getVectorNumElements();
3518 WhichResult = (M[0] == 0 ? 0 : 1);
3519 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003520 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003521 if ((unsigned) M[i] != 2 * i + WhichResult)
3522 return false;
3523 }
3524
3525 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003526 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003527 return false;
3528
3529 return true;
3530}
3531
Bob Wilson324f4f12009-12-03 06:40:55 +00003532/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3533/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3534/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3535static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3536 unsigned &WhichResult) {
3537 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3538 if (EltSz == 64)
3539 return false;
3540
3541 unsigned Half = VT.getVectorNumElements() / 2;
3542 WhichResult = (M[0] == 0 ? 0 : 1);
3543 for (unsigned j = 0; j != 2; ++j) {
3544 unsigned Idx = WhichResult;
3545 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003546 int MIdx = M[i + j * Half];
3547 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003548 return false;
3549 Idx += 2;
3550 }
3551 }
3552
3553 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3554 if (VT.is64BitVector() && EltSz == 32)
3555 return false;
3556
3557 return true;
3558}
3559
Bob Wilsonc692cb72009-08-21 20:54:19 +00003560static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3561 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003562 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3563 if (EltSz == 64)
3564 return false;
3565
Bob Wilsonc692cb72009-08-21 20:54:19 +00003566 unsigned NumElts = VT.getVectorNumElements();
3567 WhichResult = (M[0] == 0 ? 0 : 1);
3568 unsigned Idx = WhichResult * NumElts / 2;
3569 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003570 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3571 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003572 return false;
3573 Idx += 1;
3574 }
3575
3576 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003577 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003578 return false;
3579
3580 return true;
3581}
3582
Bob Wilson324f4f12009-12-03 06:40:55 +00003583/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3584/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3585/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3586static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3587 unsigned &WhichResult) {
3588 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3589 if (EltSz == 64)
3590 return false;
3591
3592 unsigned NumElts = VT.getVectorNumElements();
3593 WhichResult = (M[0] == 0 ? 0 : 1);
3594 unsigned Idx = WhichResult * NumElts / 2;
3595 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003596 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3597 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003598 return false;
3599 Idx += 1;
3600 }
3601
3602 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3603 if (VT.is64BitVector() && EltSz == 32)
3604 return false;
3605
3606 return true;
3607}
3608
Dale Johannesenf630c712010-07-29 20:10:08 +00003609// If N is an integer constant that can be moved into a register in one
3610// instruction, return an SDValue of such a constant (will become a MOV
3611// instruction). Otherwise return null.
3612static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3613 const ARMSubtarget *ST, DebugLoc dl) {
3614 uint64_t Val;
3615 if (!isa<ConstantSDNode>(N))
3616 return SDValue();
3617 Val = cast<ConstantSDNode>(N)->getZExtValue();
3618
3619 if (ST->isThumb1Only()) {
3620 if (Val <= 255 || ~Val <= 255)
3621 return DAG.getConstant(Val, MVT::i32);
3622 } else {
3623 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3624 return DAG.getConstant(Val, MVT::i32);
3625 }
3626 return SDValue();
3627}
3628
Bob Wilson5bafff32009-06-22 23:27:02 +00003629// If this is a case we can't handle, return null and let the default
3630// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003631SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3632 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003633 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003634 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003635 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003636
3637 APInt SplatBits, SplatUndef;
3638 unsigned SplatBitSize;
3639 bool HasAnyUndefs;
3640 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003641 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003642 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003643 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003644 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003645 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003646 DAG, VmovVT, VT.is128BitVector(),
3647 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003648 if (Val.getNode()) {
3649 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003650 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003651 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003652
3653 // Try an immediate VMVN.
3654 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3655 ((1LL << SplatBitSize) - 1));
3656 Val = isNEONModifiedImm(NegatedImm,
3657 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003658 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003659 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003660 if (Val.getNode()) {
3661 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003662 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003663 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003664 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003665 }
3666
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003667 // Scan through the operands to see if only one value is used.
3668 unsigned NumElts = VT.getVectorNumElements();
3669 bool isOnlyLowElement = true;
3670 bool usesOnlyOneValue = true;
3671 bool isConstant = true;
3672 SDValue Value;
3673 for (unsigned i = 0; i < NumElts; ++i) {
3674 SDValue V = Op.getOperand(i);
3675 if (V.getOpcode() == ISD::UNDEF)
3676 continue;
3677 if (i > 0)
3678 isOnlyLowElement = false;
3679 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3680 isConstant = false;
3681
3682 if (!Value.getNode())
3683 Value = V;
3684 else if (V != Value)
3685 usesOnlyOneValue = false;
3686 }
3687
3688 if (!Value.getNode())
3689 return DAG.getUNDEF(VT);
3690
3691 if (isOnlyLowElement)
3692 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3693
Dale Johannesenf630c712010-07-29 20:10:08 +00003694 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3695
Dale Johannesen575cd142010-10-19 20:00:17 +00003696 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3697 // i32 and try again.
3698 if (usesOnlyOneValue && EltSize <= 32) {
3699 if (!isConstant)
3700 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3701 if (VT.getVectorElementType().isFloatingPoint()) {
3702 SmallVector<SDValue, 8> Ops;
3703 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003704 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003705 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003706 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3707 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003708 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3709 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003710 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003711 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003712 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3713 if (Val.getNode())
3714 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003715 }
3716
3717 // If all elements are constants and the case above didn't get hit, fall back
3718 // to the default expansion, which will generate a load from the constant
3719 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003720 if (isConstant)
3721 return SDValue();
3722
Bob Wilson11a1dff2011-01-07 21:37:30 +00003723 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3724 if (NumElts >= 4) {
3725 SDValue shuffle = ReconstructShuffle(Op, DAG);
3726 if (shuffle != SDValue())
3727 return shuffle;
3728 }
3729
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003730 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003731 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3732 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003733 if (EltSize >= 32) {
3734 // Do the expansion with floating-point types, since that is what the VFP
3735 // registers are defined to use, and since i64 is not legal.
3736 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3737 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003738 SmallVector<SDValue, 8> Ops;
3739 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003740 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003741 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003742 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003743 }
3744
3745 return SDValue();
3746}
3747
Bob Wilson11a1dff2011-01-07 21:37:30 +00003748// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003749// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003750SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3751 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003752 DebugLoc dl = Op.getDebugLoc();
3753 EVT VT = Op.getValueType();
3754 unsigned NumElts = VT.getVectorNumElements();
3755
3756 SmallVector<SDValue, 2> SourceVecs;
3757 SmallVector<unsigned, 2> MinElts;
3758 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003759
Bob Wilson11a1dff2011-01-07 21:37:30 +00003760 for (unsigned i = 0; i < NumElts; ++i) {
3761 SDValue V = Op.getOperand(i);
3762 if (V.getOpcode() == ISD::UNDEF)
3763 continue;
3764 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3765 // A shuffle can only come from building a vector from various
3766 // elements of other vectors.
3767 return SDValue();
3768 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003769
Bob Wilson11a1dff2011-01-07 21:37:30 +00003770 // Record this extraction against the appropriate vector if possible...
3771 SDValue SourceVec = V.getOperand(0);
3772 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3773 bool FoundSource = false;
3774 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3775 if (SourceVecs[j] == SourceVec) {
3776 if (MinElts[j] > EltNo)
3777 MinElts[j] = EltNo;
3778 if (MaxElts[j] < EltNo)
3779 MaxElts[j] = EltNo;
3780 FoundSource = true;
3781 break;
3782 }
3783 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003784
Bob Wilson11a1dff2011-01-07 21:37:30 +00003785 // Or record a new source if not...
3786 if (!FoundSource) {
3787 SourceVecs.push_back(SourceVec);
3788 MinElts.push_back(EltNo);
3789 MaxElts.push_back(EltNo);
3790 }
3791 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003792
Bob Wilson11a1dff2011-01-07 21:37:30 +00003793 // Currently only do something sane when at most two source vectors
3794 // involved.
3795 if (SourceVecs.size() > 2)
3796 return SDValue();
3797
3798 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3799 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003800
Bob Wilson11a1dff2011-01-07 21:37:30 +00003801 // This loop extracts the usage patterns of the source vectors
3802 // and prepares appropriate SDValues for a shuffle if possible.
3803 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3804 if (SourceVecs[i].getValueType() == VT) {
3805 // No VEXT necessary
3806 ShuffleSrcs[i] = SourceVecs[i];
3807 VEXTOffsets[i] = 0;
3808 continue;
3809 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3810 // It probably isn't worth padding out a smaller vector just to
3811 // break it down again in a shuffle.
3812 return SDValue();
3813 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003814
Bob Wilson11a1dff2011-01-07 21:37:30 +00003815 // Since only 64-bit and 128-bit vectors are legal on ARM and
3816 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003817 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3818 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003819
Bob Wilson11a1dff2011-01-07 21:37:30 +00003820 if (MaxElts[i] - MinElts[i] >= NumElts) {
3821 // Span too large for a VEXT to cope
3822 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003823 }
3824
Bob Wilson11a1dff2011-01-07 21:37:30 +00003825 if (MinElts[i] >= NumElts) {
3826 // The extraction can just take the second half
3827 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003828 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3829 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003830 DAG.getIntPtrConstant(NumElts));
3831 } else if (MaxElts[i] < NumElts) {
3832 // The extraction can just take the first half
3833 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003834 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3835 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003836 DAG.getIntPtrConstant(0));
3837 } else {
3838 // An actual VEXT is needed
3839 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003840 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3841 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003842 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003843 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3844 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003845 DAG.getIntPtrConstant(NumElts));
3846 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3847 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3848 }
3849 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003850
Bob Wilson11a1dff2011-01-07 21:37:30 +00003851 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003852
Bob Wilson11a1dff2011-01-07 21:37:30 +00003853 for (unsigned i = 0; i < NumElts; ++i) {
3854 SDValue Entry = Op.getOperand(i);
3855 if (Entry.getOpcode() == ISD::UNDEF) {
3856 Mask.push_back(-1);
3857 continue;
3858 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003859
Bob Wilson11a1dff2011-01-07 21:37:30 +00003860 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003861 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3862 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003863 if (ExtractVec == SourceVecs[0]) {
3864 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3865 } else {
3866 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3867 }
3868 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003869
Bob Wilson11a1dff2011-01-07 21:37:30 +00003870 // Final check before we try to produce nonsense...
3871 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00003872 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
3873 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003874
Bob Wilson11a1dff2011-01-07 21:37:30 +00003875 return SDValue();
3876}
3877
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003878/// isShuffleMaskLegal - Targets can use this to indicate that they only
3879/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3880/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3881/// are assumed to be legal.
3882bool
3883ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3884 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003885 if (VT.getVectorNumElements() == 4 &&
3886 (VT.is128BitVector() || VT.is64BitVector())) {
3887 unsigned PFIndexes[4];
3888 for (unsigned i = 0; i != 4; ++i) {
3889 if (M[i] < 0)
3890 PFIndexes[i] = 8;
3891 else
3892 PFIndexes[i] = M[i];
3893 }
3894
3895 // Compute the index in the perfect shuffle table.
3896 unsigned PFTableIndex =
3897 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3898 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3899 unsigned Cost = (PFEntry >> 30);
3900
3901 if (Cost <= 4)
3902 return true;
3903 }
3904
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003905 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003906 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003907
Bob Wilson53dd2452010-06-07 23:53:38 +00003908 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3909 return (EltSize >= 32 ||
3910 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003911 isVREVMask(M, VT, 64) ||
3912 isVREVMask(M, VT, 32) ||
3913 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003914 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3915 isVTRNMask(M, VT, WhichResult) ||
3916 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003917 isVZIPMask(M, VT, WhichResult) ||
3918 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3919 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3920 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003921}
3922
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003923/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3924/// the specified operations to build the shuffle.
3925static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3926 SDValue RHS, SelectionDAG &DAG,
3927 DebugLoc dl) {
3928 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3929 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3930 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3931
3932 enum {
3933 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3934 OP_VREV,
3935 OP_VDUP0,
3936 OP_VDUP1,
3937 OP_VDUP2,
3938 OP_VDUP3,
3939 OP_VEXT1,
3940 OP_VEXT2,
3941 OP_VEXT3,
3942 OP_VUZPL, // VUZP, left result
3943 OP_VUZPR, // VUZP, right result
3944 OP_VZIPL, // VZIP, left result
3945 OP_VZIPR, // VZIP, right result
3946 OP_VTRNL, // VTRN, left result
3947 OP_VTRNR // VTRN, right result
3948 };
3949
3950 if (OpNum == OP_COPY) {
3951 if (LHSID == (1*9+2)*9+3) return LHS;
3952 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3953 return RHS;
3954 }
3955
3956 SDValue OpLHS, OpRHS;
3957 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3958 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3959 EVT VT = OpLHS.getValueType();
3960
3961 switch (OpNum) {
3962 default: llvm_unreachable("Unknown shuffle opcode!");
3963 case OP_VREV:
3964 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3965 case OP_VDUP0:
3966 case OP_VDUP1:
3967 case OP_VDUP2:
3968 case OP_VDUP3:
3969 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003970 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003971 case OP_VEXT1:
3972 case OP_VEXT2:
3973 case OP_VEXT3:
3974 return DAG.getNode(ARMISD::VEXT, dl, VT,
3975 OpLHS, OpRHS,
3976 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3977 case OP_VUZPL:
3978 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003979 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003980 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3981 case OP_VZIPL:
3982 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003983 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003984 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3985 case OP_VTRNL:
3986 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003987 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3988 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003989 }
3990}
3991
Bob Wilson5bafff32009-06-22 23:27:02 +00003992static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003993 SDValue V1 = Op.getOperand(0);
3994 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003995 DebugLoc dl = Op.getDebugLoc();
3996 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003997 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003998 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003999
Bob Wilson28865062009-08-13 02:13:04 +00004000 // Convert shuffles that are directly supported on NEON to target-specific
4001 // DAG nodes, instead of keeping them as shuffles and matching them again
4002 // during code selection. This is more efficient and avoids the possibility
4003 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004004 // FIXME: floating-point vectors should be canonicalized to integer vectors
4005 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004006 SVN->getMask(ShuffleMask);
4007
Bob Wilson53dd2452010-06-07 23:53:38 +00004008 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4009 if (EltSize <= 32) {
4010 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4011 int Lane = SVN->getSplatIndex();
4012 // If this is undef splat, generate it via "just" vdup, if possible.
4013 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004014
Bob Wilson53dd2452010-06-07 23:53:38 +00004015 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4016 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4017 }
4018 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4019 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004020 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004021
4022 bool ReverseVEXT;
4023 unsigned Imm;
4024 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4025 if (ReverseVEXT)
4026 std::swap(V1, V2);
4027 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4028 DAG.getConstant(Imm, MVT::i32));
4029 }
4030
4031 if (isVREVMask(ShuffleMask, VT, 64))
4032 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4033 if (isVREVMask(ShuffleMask, VT, 32))
4034 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4035 if (isVREVMask(ShuffleMask, VT, 16))
4036 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4037
4038 // Check for Neon shuffles that modify both input vectors in place.
4039 // If both results are used, i.e., if there are two shuffles with the same
4040 // source operands and with masks corresponding to both results of one of
4041 // these operations, DAG memoization will ensure that a single node is
4042 // used for both shuffles.
4043 unsigned WhichResult;
4044 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4045 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4046 V1, V2).getValue(WhichResult);
4047 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4048 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4049 V1, V2).getValue(WhichResult);
4050 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4051 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4052 V1, V2).getValue(WhichResult);
4053
4054 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4055 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4056 V1, V1).getValue(WhichResult);
4057 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4058 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4059 V1, V1).getValue(WhichResult);
4060 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4061 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4062 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004063 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004064
Bob Wilsonc692cb72009-08-21 20:54:19 +00004065 // If the shuffle is not directly supported and it has 4 elements, use
4066 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004067 unsigned NumElts = VT.getVectorNumElements();
4068 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004069 unsigned PFIndexes[4];
4070 for (unsigned i = 0; i != 4; ++i) {
4071 if (ShuffleMask[i] < 0)
4072 PFIndexes[i] = 8;
4073 else
4074 PFIndexes[i] = ShuffleMask[i];
4075 }
4076
4077 // Compute the index in the perfect shuffle table.
4078 unsigned PFTableIndex =
4079 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004080 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4081 unsigned Cost = (PFEntry >> 30);
4082
4083 if (Cost <= 4)
4084 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4085 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004086
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004087 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004088 if (EltSize >= 32) {
4089 // Do the expansion with floating-point types, since that is what the VFP
4090 // registers are defined to use, and since i64 is not legal.
4091 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4092 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004093 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4094 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004095 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004096 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004097 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004098 Ops.push_back(DAG.getUNDEF(EltVT));
4099 else
4100 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4101 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4102 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4103 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004104 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004105 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004106 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004107 }
4108
Bob Wilson22cac0d2009-08-14 05:16:33 +00004109 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004110}
4111
Bob Wilson5bafff32009-06-22 23:27:02 +00004112static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004113 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004114 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004115 if (!isa<ConstantSDNode>(Lane))
4116 return SDValue();
4117
4118 SDValue Vec = Op.getOperand(0);
4119 if (Op.getValueType() == MVT::i32 &&
4120 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4121 DebugLoc dl = Op.getDebugLoc();
4122 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4123 }
4124
4125 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004126}
4127
Bob Wilsona6d65862009-08-03 20:36:38 +00004128static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4129 // The only time a CONCAT_VECTORS operation can have legal types is when
4130 // two 64-bit vectors are concatenated to a 128-bit vector.
4131 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4132 "unexpected CONCAT_VECTORS");
4133 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004134 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004135 SDValue Op0 = Op.getOperand(0);
4136 SDValue Op1 = Op.getOperand(1);
4137 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004139 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004140 DAG.getIntPtrConstant(0));
4141 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004143 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004144 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004145 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004146}
4147
Bob Wilson626613d2010-11-23 19:38:38 +00004148/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4149/// element has been zero/sign-extended, depending on the isSigned parameter,
4150/// from an integer type half its size.
4151static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4152 bool isSigned) {
4153 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4154 EVT VT = N->getValueType(0);
4155 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4156 SDNode *BVN = N->getOperand(0).getNode();
4157 if (BVN->getValueType(0) != MVT::v4i32 ||
4158 BVN->getOpcode() != ISD::BUILD_VECTOR)
4159 return false;
4160 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4161 unsigned HiElt = 1 - LoElt;
4162 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4163 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4164 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4165 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4166 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4167 return false;
4168 if (isSigned) {
4169 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4170 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4171 return true;
4172 } else {
4173 if (Hi0->isNullValue() && Hi1->isNullValue())
4174 return true;
4175 }
4176 return false;
4177 }
4178
4179 if (N->getOpcode() != ISD::BUILD_VECTOR)
4180 return false;
4181
4182 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4183 SDNode *Elt = N->getOperand(i).getNode();
4184 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4185 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4186 unsigned HalfSize = EltSize / 2;
4187 if (isSigned) {
4188 int64_t SExtVal = C->getSExtValue();
4189 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4190 return false;
4191 } else {
4192 if ((C->getZExtValue() >> HalfSize) != 0)
4193 return false;
4194 }
4195 continue;
4196 }
4197 return false;
4198 }
4199
4200 return true;
4201}
4202
4203/// isSignExtended - Check if a node is a vector value that is sign-extended
4204/// or a constant BUILD_VECTOR with sign-extended elements.
4205static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4206 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4207 return true;
4208 if (isExtendedBUILD_VECTOR(N, DAG, true))
4209 return true;
4210 return false;
4211}
4212
4213/// isZeroExtended - Check if a node is a vector value that is zero-extended
4214/// or a constant BUILD_VECTOR with zero-extended elements.
4215static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4216 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4217 return true;
4218 if (isExtendedBUILD_VECTOR(N, DAG, false))
4219 return true;
4220 return false;
4221}
4222
4223/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4224/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004225static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4226 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4227 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004228 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4229 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4230 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4231 LD->isNonTemporal(), LD->getAlignment());
4232 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4233 // have been legalized as a BITCAST from v4i32.
4234 if (N->getOpcode() == ISD::BITCAST) {
4235 SDNode *BVN = N->getOperand(0).getNode();
4236 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4237 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4238 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4239 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4240 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4241 }
4242 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4243 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4244 EVT VT = N->getValueType(0);
4245 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4246 unsigned NumElts = VT.getVectorNumElements();
4247 MVT TruncVT = MVT::getIntegerVT(EltSize);
4248 SmallVector<SDValue, 8> Ops;
4249 for (unsigned i = 0; i != NumElts; ++i) {
4250 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4251 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004252 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004253 }
4254 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4255 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004256}
4257
4258static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4259 // Multiplications are only custom-lowered for 128-bit vectors so that
4260 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4261 EVT VT = Op.getValueType();
4262 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4263 SDNode *N0 = Op.getOperand(0).getNode();
4264 SDNode *N1 = Op.getOperand(1).getNode();
4265 unsigned NewOpc = 0;
Bob Wilson626613d2010-11-23 19:38:38 +00004266 if (isSignExtended(N0, DAG) && isSignExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004267 NewOpc = ARMISD::VMULLs;
Bob Wilson626613d2010-11-23 19:38:38 +00004268 else if (isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004269 NewOpc = ARMISD::VMULLu;
Bob Wilson626613d2010-11-23 19:38:38 +00004270 else if (VT == MVT::v2i64)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004271 // Fall through to expand this. It is not legal.
4272 return SDValue();
Bob Wilson626613d2010-11-23 19:38:38 +00004273 else
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004274 // Other vector multiplications are legal.
4275 return Op;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004276
4277 // Legalize to a VMULL instruction.
4278 DebugLoc DL = Op.getDebugLoc();
4279 SDValue Op0 = SkipExtension(N0, DAG);
4280 SDValue Op1 = SkipExtension(N1, DAG);
4281
4282 assert(Op0.getValueType().is64BitVector() &&
4283 Op1.getValueType().is64BitVector() &&
4284 "unexpected types for extended operands to VMULL");
4285 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4286}
4287
Dan Gohmand858e902010-04-17 15:26:15 +00004288SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004289 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004290 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004291 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004292 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004293 case ISD::GlobalAddress:
4294 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4295 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00004296 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004297 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004298 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4299 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004300 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004301 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004302 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004303 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004304 case ISD::SINT_TO_FP:
4305 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4306 case ISD::FP_TO_SINT:
4307 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004308 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004309 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004310 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004311 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004312 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004313 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004314 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004315 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4316 Subtarget);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004317 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004318 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004319 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004320 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004321 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004322 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004323 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004324 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004325 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004326 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004327 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004328 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004329 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004330 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004331 case ISD::MUL: return LowerMUL(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004332 }
Dan Gohman475871a2008-07-27 21:46:04 +00004333 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004334}
4335
Duncan Sands1607f052008-12-01 11:39:25 +00004336/// ReplaceNodeResults - Replace the results of node with an illegal result
4337/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004338void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4339 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004340 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004341 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004342 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004343 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004344 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004345 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004346 case ISD::BITCAST:
4347 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004348 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004349 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004350 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004351 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004352 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004353 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004354 if (Res.getNode())
4355 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004356}
Chris Lattner27a6c732007-11-24 07:07:01 +00004357
Evan Chenga8e29892007-01-19 07:51:42 +00004358//===----------------------------------------------------------------------===//
4359// ARM Scheduler Hooks
4360//===----------------------------------------------------------------------===//
4361
4362MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004363ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4364 MachineBasicBlock *BB,
4365 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004366 unsigned dest = MI->getOperand(0).getReg();
4367 unsigned ptr = MI->getOperand(1).getReg();
4368 unsigned oldval = MI->getOperand(2).getReg();
4369 unsigned newval = MI->getOperand(3).getReg();
4370 unsigned scratch = BB->getParent()->getRegInfo()
4371 .createVirtualRegister(ARM::GPRRegisterClass);
4372 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4373 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004374 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004375
4376 unsigned ldrOpc, strOpc;
4377 switch (Size) {
4378 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004379 case 1:
4380 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004381 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004382 break;
4383 case 2:
4384 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4385 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4386 break;
4387 case 4:
4388 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4389 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4390 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004391 }
4392
4393 MachineFunction *MF = BB->getParent();
4394 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4395 MachineFunction::iterator It = BB;
4396 ++It; // insert the new blocks after the current block
4397
4398 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4399 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4400 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4401 MF->insert(It, loop1MBB);
4402 MF->insert(It, loop2MBB);
4403 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004404
4405 // Transfer the remainder of BB and its successor edges to exitMBB.
4406 exitMBB->splice(exitMBB->begin(), BB,
4407 llvm::next(MachineBasicBlock::iterator(MI)),
4408 BB->end());
4409 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004410
4411 // thisMBB:
4412 // ...
4413 // fallthrough --> loop1MBB
4414 BB->addSuccessor(loop1MBB);
4415
4416 // loop1MBB:
4417 // ldrex dest, [ptr]
4418 // cmp dest, oldval
4419 // bne exitMBB
4420 BB = loop1MBB;
4421 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004422 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004423 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004424 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4425 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004426 BB->addSuccessor(loop2MBB);
4427 BB->addSuccessor(exitMBB);
4428
4429 // loop2MBB:
4430 // strex scratch, newval, [ptr]
4431 // cmp scratch, #0
4432 // bne loop1MBB
4433 BB = loop2MBB;
4434 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4435 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004436 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004437 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004438 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4439 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004440 BB->addSuccessor(loop1MBB);
4441 BB->addSuccessor(exitMBB);
4442
4443 // exitMBB:
4444 // ...
4445 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004446
Dan Gohman14152b42010-07-06 20:24:04 +00004447 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004448
Jim Grosbach5278eb82009-12-11 01:42:04 +00004449 return BB;
4450}
4451
4452MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004453ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4454 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004455 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4456 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4457
4458 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004459 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004460 MachineFunction::iterator It = BB;
4461 ++It;
4462
4463 unsigned dest = MI->getOperand(0).getReg();
4464 unsigned ptr = MI->getOperand(1).getReg();
4465 unsigned incr = MI->getOperand(2).getReg();
4466 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004467
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004468 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004469 unsigned ldrOpc, strOpc;
4470 switch (Size) {
4471 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004472 case 1:
4473 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004474 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004475 break;
4476 case 2:
4477 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4478 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4479 break;
4480 case 4:
4481 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4482 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4483 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004484 }
4485
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004486 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4487 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4488 MF->insert(It, loopMBB);
4489 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004490
4491 // Transfer the remainder of BB and its successor edges to exitMBB.
4492 exitMBB->splice(exitMBB->begin(), BB,
4493 llvm::next(MachineBasicBlock::iterator(MI)),
4494 BB->end());
4495 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004496
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004497 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004498 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4499 unsigned scratch2 = (!BinOpcode) ? incr :
4500 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4501
4502 // thisMBB:
4503 // ...
4504 // fallthrough --> loopMBB
4505 BB->addSuccessor(loopMBB);
4506
4507 // loopMBB:
4508 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004509 // <binop> scratch2, dest, incr
4510 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004511 // cmp scratch, #0
4512 // bne- loopMBB
4513 // fallthrough --> exitMBB
4514 BB = loopMBB;
4515 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004516 if (BinOpcode) {
4517 // operand order needs to go the other way for NAND
4518 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4519 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4520 addReg(incr).addReg(dest)).addReg(0);
4521 else
4522 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4523 addReg(dest).addReg(incr)).addReg(0);
4524 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004525
4526 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4527 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004528 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004529 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004530 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4531 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004532
4533 BB->addSuccessor(loopMBB);
4534 BB->addSuccessor(exitMBB);
4535
4536 // exitMBB:
4537 // ...
4538 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004539
Dan Gohman14152b42010-07-06 20:24:04 +00004540 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004541
Jim Grosbachc3c23542009-12-14 04:22:04 +00004542 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004543}
4544
Evan Cheng218977b2010-07-13 19:27:42 +00004545static
4546MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4547 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4548 E = MBB->succ_end(); I != E; ++I)
4549 if (*I != Succ)
4550 return *I;
4551 llvm_unreachable("Expecting a BB with two successors!");
4552}
4553
Jim Grosbache801dc42009-12-12 01:40:06 +00004554MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004555ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004556 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004557 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004558 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004559 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004560 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004561 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004562 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004563 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004564
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004565 case ARM::ATOMIC_LOAD_ADD_I8:
4566 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4567 case ARM::ATOMIC_LOAD_ADD_I16:
4568 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4569 case ARM::ATOMIC_LOAD_ADD_I32:
4570 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004571
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004572 case ARM::ATOMIC_LOAD_AND_I8:
4573 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4574 case ARM::ATOMIC_LOAD_AND_I16:
4575 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4576 case ARM::ATOMIC_LOAD_AND_I32:
4577 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004578
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004579 case ARM::ATOMIC_LOAD_OR_I8:
4580 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4581 case ARM::ATOMIC_LOAD_OR_I16:
4582 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4583 case ARM::ATOMIC_LOAD_OR_I32:
4584 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004585
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004586 case ARM::ATOMIC_LOAD_XOR_I8:
4587 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4588 case ARM::ATOMIC_LOAD_XOR_I16:
4589 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4590 case ARM::ATOMIC_LOAD_XOR_I32:
4591 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004592
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004593 case ARM::ATOMIC_LOAD_NAND_I8:
4594 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4595 case ARM::ATOMIC_LOAD_NAND_I16:
4596 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4597 case ARM::ATOMIC_LOAD_NAND_I32:
4598 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004599
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004600 case ARM::ATOMIC_LOAD_SUB_I8:
4601 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4602 case ARM::ATOMIC_LOAD_SUB_I16:
4603 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4604 case ARM::ATOMIC_LOAD_SUB_I32:
4605 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004606
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004607 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4608 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4609 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004610
4611 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4612 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4613 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004614
Evan Cheng007ea272009-08-12 05:17:19 +00004615 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004616 // To "insert" a SELECT_CC instruction, we actually have to insert the
4617 // diamond control-flow pattern. The incoming instruction knows the
4618 // destination vreg to set, the condition code register to branch on, the
4619 // true/false values to select between, and a branch opcode to use.
4620 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004621 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004622 ++It;
4623
4624 // thisMBB:
4625 // ...
4626 // TrueVal = ...
4627 // cmpTY ccX, r1, r2
4628 // bCC copy1MBB
4629 // fallthrough --> copy0MBB
4630 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004631 MachineFunction *F = BB->getParent();
4632 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4633 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004634 F->insert(It, copy0MBB);
4635 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004636
4637 // Transfer the remainder of BB and its successor edges to sinkMBB.
4638 sinkMBB->splice(sinkMBB->begin(), BB,
4639 llvm::next(MachineBasicBlock::iterator(MI)),
4640 BB->end());
4641 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4642
Dan Gohman258c58c2010-07-06 15:49:48 +00004643 BB->addSuccessor(copy0MBB);
4644 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004645
Dan Gohman14152b42010-07-06 20:24:04 +00004646 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4647 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4648
Evan Chenga8e29892007-01-19 07:51:42 +00004649 // copy0MBB:
4650 // %FalseValue = ...
4651 // # fallthrough to sinkMBB
4652 BB = copy0MBB;
4653
4654 // Update machine-CFG edges
4655 BB->addSuccessor(sinkMBB);
4656
4657 // sinkMBB:
4658 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4659 // ...
4660 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004661 BuildMI(*BB, BB->begin(), dl,
4662 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004663 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4664 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4665
Dan Gohman14152b42010-07-06 20:24:04 +00004666 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004667 return BB;
4668 }
Evan Cheng86198642009-08-07 00:34:42 +00004669
Evan Cheng218977b2010-07-13 19:27:42 +00004670 case ARM::BCCi64:
4671 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00004672 // If there is an unconditional branch to the other successor, remove it.
4673 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004674
Evan Cheng218977b2010-07-13 19:27:42 +00004675 // Compare both parts that make up the double comparison separately for
4676 // equality.
4677 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4678
4679 unsigned LHS1 = MI->getOperand(1).getReg();
4680 unsigned LHS2 = MI->getOperand(2).getReg();
4681 if (RHSisZero) {
4682 AddDefaultPred(BuildMI(BB, dl,
4683 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4684 .addReg(LHS1).addImm(0));
4685 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4686 .addReg(LHS2).addImm(0)
4687 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4688 } else {
4689 unsigned RHS1 = MI->getOperand(3).getReg();
4690 unsigned RHS2 = MI->getOperand(4).getReg();
4691 AddDefaultPred(BuildMI(BB, dl,
4692 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4693 .addReg(LHS1).addReg(RHS1));
4694 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4695 .addReg(LHS2).addReg(RHS2)
4696 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4697 }
4698
4699 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4700 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4701 if (MI->getOperand(0).getImm() == ARMCC::NE)
4702 std::swap(destMBB, exitMBB);
4703
4704 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4705 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4706 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4707 .addMBB(exitMBB);
4708
4709 MI->eraseFromParent(); // The pseudo instruction is gone now.
4710 return BB;
4711 }
Evan Chenga8e29892007-01-19 07:51:42 +00004712 }
4713}
4714
4715//===----------------------------------------------------------------------===//
4716// ARM Optimization Hooks
4717//===----------------------------------------------------------------------===//
4718
Chris Lattnerd1980a52009-03-12 06:52:53 +00004719static
4720SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4721 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004722 SelectionDAG &DAG = DCI.DAG;
4723 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004724 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004725 unsigned Opc = N->getOpcode();
4726 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4727 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4728 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4729 ISD::CondCode CC = ISD::SETCC_INVALID;
4730
4731 if (isSlctCC) {
4732 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4733 } else {
4734 SDValue CCOp = Slct.getOperand(0);
4735 if (CCOp.getOpcode() == ISD::SETCC)
4736 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4737 }
4738
4739 bool DoXform = false;
4740 bool InvCC = false;
4741 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4742 "Bad input!");
4743
4744 if (LHS.getOpcode() == ISD::Constant &&
4745 cast<ConstantSDNode>(LHS)->isNullValue()) {
4746 DoXform = true;
4747 } else if (CC != ISD::SETCC_INVALID &&
4748 RHS.getOpcode() == ISD::Constant &&
4749 cast<ConstantSDNode>(RHS)->isNullValue()) {
4750 std::swap(LHS, RHS);
4751 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004752 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004753 Op0.getOperand(0).getValueType();
4754 bool isInt = OpVT.isInteger();
4755 CC = ISD::getSetCCInverse(CC, isInt);
4756
4757 if (!TLI.isCondCodeLegal(CC, OpVT))
4758 return SDValue(); // Inverse operator isn't legal.
4759
4760 DoXform = true;
4761 InvCC = true;
4762 }
4763
4764 if (DoXform) {
4765 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4766 if (isSlctCC)
4767 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4768 Slct.getOperand(0), Slct.getOperand(1), CC);
4769 SDValue CCOp = Slct.getOperand(0);
4770 if (InvCC)
4771 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4772 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4773 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4774 CCOp, OtherOp, Result);
4775 }
4776 return SDValue();
4777}
4778
Bob Wilson3d5792a2010-07-29 20:34:14 +00004779/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4780/// operands N0 and N1. This is a helper for PerformADDCombine that is
4781/// called with the default operands, and if that fails, with commuted
4782/// operands.
4783static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4784 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004785 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4786 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4787 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4788 if (Result.getNode()) return Result;
4789 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00004790 return SDValue();
4791}
4792
Bob Wilson3d5792a2010-07-29 20:34:14 +00004793/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4794///
4795static SDValue PerformADDCombine(SDNode *N,
4796 TargetLowering::DAGCombinerInfo &DCI) {
4797 SDValue N0 = N->getOperand(0);
4798 SDValue N1 = N->getOperand(1);
4799
4800 // First try with the default operand order.
4801 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4802 if (Result.getNode())
4803 return Result;
4804
4805 // If that didn't work, try again with the operands commuted.
4806 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4807}
4808
Chris Lattnerd1980a52009-03-12 06:52:53 +00004809/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004810///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004811static SDValue PerformSUBCombine(SDNode *N,
4812 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004813 SDValue N0 = N->getOperand(0);
4814 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004815
Chris Lattnerd1980a52009-03-12 06:52:53 +00004816 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4817 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4818 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4819 if (Result.getNode()) return Result;
4820 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004821
Chris Lattnerd1980a52009-03-12 06:52:53 +00004822 return SDValue();
4823}
4824
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004825static SDValue PerformMULCombine(SDNode *N,
4826 TargetLowering::DAGCombinerInfo &DCI,
4827 const ARMSubtarget *Subtarget) {
4828 SelectionDAG &DAG = DCI.DAG;
4829
4830 if (Subtarget->isThumb1Only())
4831 return SDValue();
4832
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004833 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4834 return SDValue();
4835
4836 EVT VT = N->getValueType(0);
4837 if (VT != MVT::i32)
4838 return SDValue();
4839
4840 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4841 if (!C)
4842 return SDValue();
4843
4844 uint64_t MulAmt = C->getZExtValue();
4845 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4846 ShiftAmt = ShiftAmt & (32 - 1);
4847 SDValue V = N->getOperand(0);
4848 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004849
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004850 SDValue Res;
4851 MulAmt >>= ShiftAmt;
4852 if (isPowerOf2_32(MulAmt - 1)) {
4853 // (mul x, 2^N + 1) => (add (shl x, N), x)
4854 Res = DAG.getNode(ISD::ADD, DL, VT,
4855 V, DAG.getNode(ISD::SHL, DL, VT,
4856 V, DAG.getConstant(Log2_32(MulAmt-1),
4857 MVT::i32)));
4858 } else if (isPowerOf2_32(MulAmt + 1)) {
4859 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4860 Res = DAG.getNode(ISD::SUB, DL, VT,
4861 DAG.getNode(ISD::SHL, DL, VT,
4862 V, DAG.getConstant(Log2_32(MulAmt+1),
4863 MVT::i32)),
4864 V);
4865 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004866 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004867
4868 if (ShiftAmt != 0)
4869 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4870 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004871
4872 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004873 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004874 return SDValue();
4875}
4876
Owen Anderson080c0922010-11-05 19:27:46 +00004877static SDValue PerformANDCombine(SDNode *N,
4878 TargetLowering::DAGCombinerInfo &DCI) {
4879 // Attempt to use immediate-form VBIC
4880 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4881 DebugLoc dl = N->getDebugLoc();
4882 EVT VT = N->getValueType(0);
4883 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004884
Owen Anderson080c0922010-11-05 19:27:46 +00004885 APInt SplatBits, SplatUndef;
4886 unsigned SplatBitSize;
4887 bool HasAnyUndefs;
4888 if (BVN &&
4889 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4890 if (SplatBitSize <= 64) {
4891 EVT VbicVT;
4892 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
4893 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004894 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004895 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00004896 if (Val.getNode()) {
4897 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004898 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00004899 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004900 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00004901 }
4902 }
4903 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004904
Owen Anderson080c0922010-11-05 19:27:46 +00004905 return SDValue();
4906}
4907
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004908/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4909static SDValue PerformORCombine(SDNode *N,
4910 TargetLowering::DAGCombinerInfo &DCI,
4911 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00004912 // Attempt to use immediate-form VORR
4913 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4914 DebugLoc dl = N->getDebugLoc();
4915 EVT VT = N->getValueType(0);
4916 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004917
Owen Anderson60f48702010-11-03 23:15:26 +00004918 APInt SplatBits, SplatUndef;
4919 unsigned SplatBitSize;
4920 bool HasAnyUndefs;
4921 if (BVN && Subtarget->hasNEON() &&
4922 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4923 if (SplatBitSize <= 64) {
4924 EVT VorrVT;
4925 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4926 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004927 DAG, VorrVT, VT.is128BitVector(),
4928 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00004929 if (Val.getNode()) {
4930 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004931 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00004932 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004933 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00004934 }
4935 }
4936 }
4937
Jim Grosbach54238562010-07-17 03:30:54 +00004938 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4939 // reasonable.
4940
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004941 // BFI is only available on V6T2+
4942 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4943 return SDValue();
4944
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004945 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004946 DebugLoc DL = N->getDebugLoc();
4947 // 1) or (and A, mask), val => ARMbfi A, val, mask
4948 // iff (val & mask) == val
4949 //
4950 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4951 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4952 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4953 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4954 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4955 // (i.e., copy a bitfield value into another bitfield of the same width)
4956 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004957 return SDValue();
4958
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004959 if (VT != MVT::i32)
4960 return SDValue();
4961
Evan Cheng30fb13f2010-12-13 20:32:54 +00004962 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00004963
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004964 // The value and the mask need to be constants so we can verify this is
4965 // actually a bitfield set. If the mask is 0xffff, we can do better
4966 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00004967 SDValue MaskOp = N0.getOperand(1);
4968 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
4969 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004970 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004971 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004972 if (Mask == 0xffff)
4973 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004974 SDValue Res;
4975 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004976 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4977 if (N1C) {
4978 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004979 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00004980 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004981
Evan Chenga9688c42010-12-11 04:11:38 +00004982 if (ARM::isBitFieldInvertedMask(Mask)) {
4983 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004984
Evan Cheng30fb13f2010-12-13 20:32:54 +00004985 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00004986 DAG.getConstant(Val, MVT::i32),
4987 DAG.getConstant(Mask, MVT::i32));
4988
4989 // Do not add new nodes to DAG combiner worklist.
4990 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004991 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004992 }
Jim Grosbach54238562010-07-17 03:30:54 +00004993 } else if (N1.getOpcode() == ISD::AND) {
4994 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004995 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4996 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00004997 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004998 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004999
5000 if (ARM::isBitFieldInvertedMask(Mask) &&
5001 ARM::isBitFieldInvertedMask(~Mask2) &&
5002 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
5003 // The pack halfword instruction works better for masks that fit it,
5004 // so use that when it's available.
5005 if (Subtarget->hasT2ExtractPack() &&
5006 (Mask == 0xffff || Mask == 0xffff0000))
5007 return SDValue();
5008 // 2a
5009 unsigned lsb = CountTrailingZeros_32(Mask2);
5010 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
5011 DAG.getConstant(lsb, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005012 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005013 DAG.getConstant(Mask, MVT::i32));
5014 // Do not add new nodes to DAG combiner worklist.
5015 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005016 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005017 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
5018 ARM::isBitFieldInvertedMask(Mask2) &&
5019 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
5020 // The pack halfword instruction works better for masks that fit it,
5021 // so use that when it's available.
5022 if (Subtarget->hasT2ExtractPack() &&
5023 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5024 return SDValue();
5025 // 2b
5026 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005027 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005028 DAG.getConstant(lsb, MVT::i32));
5029 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
5030 DAG.getConstant(Mask2, MVT::i32));
5031 // Do not add new nodes to DAG combiner worklist.
5032 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005033 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005034 }
5035 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005036
Evan Cheng30fb13f2010-12-13 20:32:54 +00005037 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5038 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5039 ARM::isBitFieldInvertedMask(~Mask)) {
5040 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5041 // where lsb(mask) == #shamt and masked bits of B are known zero.
5042 SDValue ShAmt = N00.getOperand(1);
5043 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5044 unsigned LSB = CountTrailingZeros_32(Mask);
5045 if (ShAmtC != LSB)
5046 return SDValue();
5047
5048 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5049 DAG.getConstant(~Mask, MVT::i32));
5050
5051 // Do not add new nodes to DAG combiner worklist.
5052 DCI.CombineTo(N, Res, false);
5053 }
5054
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005055 return SDValue();
5056}
5057
Evan Cheng0c1aec12010-12-14 03:22:07 +00005058/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5059/// C1 & C2 == C1.
5060static SDValue PerformBFICombine(SDNode *N,
5061 TargetLowering::DAGCombinerInfo &DCI) {
5062 SDValue N1 = N->getOperand(1);
5063 if (N1.getOpcode() == ISD::AND) {
5064 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5065 if (!N11C)
5066 return SDValue();
5067 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5068 unsigned Mask2 = N11C->getZExtValue();
5069 if ((Mask & Mask2) == Mask2)
5070 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5071 N->getOperand(0), N1.getOperand(0),
5072 N->getOperand(2));
5073 }
5074 return SDValue();
5075}
5076
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005077/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5078/// ARMISD::VMOVRRD.
5079static SDValue PerformVMOVRRDCombine(SDNode *N,
5080 TargetLowering::DAGCombinerInfo &DCI) {
5081 // vmovrrd(vmovdrr x, y) -> x,y
5082 SDValue InDouble = N->getOperand(0);
5083 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5084 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
5085 return SDValue();
5086}
5087
5088/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5089/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5090static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5091 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5092 SDValue Op0 = N->getOperand(0);
5093 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005094 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005095 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005096 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005097 Op1 = Op1.getOperand(0);
5098 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5099 Op0.getNode() == Op1.getNode() &&
5100 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005101 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005102 N->getValueType(0), Op0.getOperand(0));
5103 return SDValue();
5104}
5105
Bob Wilson31600902010-12-21 06:43:19 +00005106/// PerformSTORECombine - Target-specific dag combine xforms for
5107/// ISD::STORE.
5108static SDValue PerformSTORECombine(SDNode *N,
5109 TargetLowering::DAGCombinerInfo &DCI) {
5110 // Bitcast an i64 store extracted from a vector to f64.
5111 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5112 StoreSDNode *St = cast<StoreSDNode>(N);
5113 SDValue StVal = St->getValue();
5114 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5115 StVal.getValueType() != MVT::i64 ||
5116 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5117 return SDValue();
5118
5119 SelectionDAG &DAG = DCI.DAG;
5120 DebugLoc dl = StVal.getDebugLoc();
5121 SDValue IntVec = StVal.getOperand(0);
5122 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5123 IntVec.getValueType().getVectorNumElements());
5124 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5125 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5126 Vec, StVal.getOperand(1));
5127 dl = N->getDebugLoc();
5128 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5129 // Make the DAGCombiner fold the bitcasts.
5130 DCI.AddToWorklist(Vec.getNode());
5131 DCI.AddToWorklist(ExtElt.getNode());
5132 DCI.AddToWorklist(V.getNode());
5133 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5134 St->getPointerInfo(), St->isVolatile(),
5135 St->isNonTemporal(), St->getAlignment(),
5136 St->getTBAAInfo());
5137}
5138
5139/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5140/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5141/// i64 vector to have f64 elements, since the value can then be loaded
5142/// directly into a VFP register.
5143static bool hasNormalLoadOperand(SDNode *N) {
5144 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5145 for (unsigned i = 0; i < NumElts; ++i) {
5146 SDNode *Elt = N->getOperand(i).getNode();
5147 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5148 return true;
5149 }
5150 return false;
5151}
5152
Bob Wilson75f02882010-09-17 22:59:05 +00005153/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5154/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005155static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5156 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005157 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5158 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5159 // into a pair of GPRs, which is fine when the value is used as a scalar,
5160 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005161 SelectionDAG &DAG = DCI.DAG;
5162 if (N->getNumOperands() == 2) {
5163 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5164 if (RV.getNode())
5165 return RV;
5166 }
Bob Wilson75f02882010-09-17 22:59:05 +00005167
Bob Wilson31600902010-12-21 06:43:19 +00005168 // Load i64 elements as f64 values so that type legalization does not split
5169 // them up into i32 values.
5170 EVT VT = N->getValueType(0);
5171 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5172 return SDValue();
5173 DebugLoc dl = N->getDebugLoc();
5174 SmallVector<SDValue, 8> Ops;
5175 unsigned NumElts = VT.getVectorNumElements();
5176 for (unsigned i = 0; i < NumElts; ++i) {
5177 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5178 Ops.push_back(V);
5179 // Make the DAGCombiner fold the bitcast.
5180 DCI.AddToWorklist(V.getNode());
5181 }
5182 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5183 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5184 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5185}
5186
5187/// PerformInsertEltCombine - Target-specific dag combine xforms for
5188/// ISD::INSERT_VECTOR_ELT.
5189static SDValue PerformInsertEltCombine(SDNode *N,
5190 TargetLowering::DAGCombinerInfo &DCI) {
5191 // Bitcast an i64 load inserted into a vector to f64.
5192 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5193 EVT VT = N->getValueType(0);
5194 SDNode *Elt = N->getOperand(1).getNode();
5195 if (VT.getVectorElementType() != MVT::i64 ||
5196 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5197 return SDValue();
5198
5199 SelectionDAG &DAG = DCI.DAG;
5200 DebugLoc dl = N->getDebugLoc();
5201 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5202 VT.getVectorNumElements());
5203 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5204 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5205 // Make the DAGCombiner fold the bitcasts.
5206 DCI.AddToWorklist(Vec.getNode());
5207 DCI.AddToWorklist(V.getNode());
5208 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5209 Vec, V, N->getOperand(2));
5210 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005211}
5212
Bob Wilsonf20700c2010-10-27 20:38:28 +00005213/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5214/// ISD::VECTOR_SHUFFLE.
5215static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5216 // The LLVM shufflevector instruction does not require the shuffle mask
5217 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5218 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5219 // operands do not match the mask length, they are extended by concatenating
5220 // them with undef vectors. That is probably the right thing for other
5221 // targets, but for NEON it is better to concatenate two double-register
5222 // size vector operands into a single quad-register size vector. Do that
5223 // transformation here:
5224 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5225 // shuffle(concat(v1, v2), undef)
5226 SDValue Op0 = N->getOperand(0);
5227 SDValue Op1 = N->getOperand(1);
5228 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5229 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5230 Op0.getNumOperands() != 2 ||
5231 Op1.getNumOperands() != 2)
5232 return SDValue();
5233 SDValue Concat0Op1 = Op0.getOperand(1);
5234 SDValue Concat1Op1 = Op1.getOperand(1);
5235 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5236 Concat1Op1.getOpcode() != ISD::UNDEF)
5237 return SDValue();
5238 // Skip the transformation if any of the types are illegal.
5239 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5240 EVT VT = N->getValueType(0);
5241 if (!TLI.isTypeLegal(VT) ||
5242 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5243 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5244 return SDValue();
5245
5246 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5247 Op0.getOperand(0), Op1.getOperand(0));
5248 // Translate the shuffle mask.
5249 SmallVector<int, 16> NewMask;
5250 unsigned NumElts = VT.getVectorNumElements();
5251 unsigned HalfElts = NumElts/2;
5252 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5253 for (unsigned n = 0; n < NumElts; ++n) {
5254 int MaskElt = SVN->getMaskElt(n);
5255 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005256 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005257 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005258 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005259 NewElt = HalfElts + MaskElt - NumElts;
5260 NewMask.push_back(NewElt);
5261 }
5262 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5263 DAG.getUNDEF(VT), NewMask.data());
5264}
5265
Bob Wilson1c3ef902011-02-07 17:43:21 +00005266/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5267/// NEON load/store intrinsics to merge base address updates.
5268static SDValue CombineBaseUpdate(SDNode *N,
5269 TargetLowering::DAGCombinerInfo &DCI) {
5270 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5271 return SDValue();
5272
5273 SelectionDAG &DAG = DCI.DAG;
5274 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5275 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5276 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5277 SDValue Addr = N->getOperand(AddrOpIdx);
5278
5279 // Search for a use of the address operand that is an increment.
5280 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5281 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5282 SDNode *User = *UI;
5283 if (User->getOpcode() != ISD::ADD ||
5284 UI.getUse().getResNo() != Addr.getResNo())
5285 continue;
5286
5287 // Check that the add is independent of the load/store. Otherwise, folding
5288 // it would create a cycle.
5289 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5290 continue;
5291
5292 // Find the new opcode for the updating load/store.
5293 bool isLoad = true;
5294 bool isLaneOp = false;
5295 unsigned NewOpc = 0;
5296 unsigned NumVecs = 0;
5297 if (isIntrinsic) {
5298 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5299 switch (IntNo) {
5300 default: assert(0 && "unexpected intrinsic for Neon base update");
5301 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5302 NumVecs = 1; break;
5303 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5304 NumVecs = 2; break;
5305 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5306 NumVecs = 3; break;
5307 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5308 NumVecs = 4; break;
5309 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5310 NumVecs = 2; isLaneOp = true; break;
5311 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5312 NumVecs = 3; isLaneOp = true; break;
5313 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5314 NumVecs = 4; isLaneOp = true; break;
5315 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5316 NumVecs = 1; isLoad = false; break;
5317 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
5318 NumVecs = 2; isLoad = false; break;
5319 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
5320 NumVecs = 3; isLoad = false; break;
5321 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
5322 NumVecs = 4; isLoad = false; break;
5323 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
5324 NumVecs = 2; isLoad = false; isLaneOp = true; break;
5325 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
5326 NumVecs = 3; isLoad = false; isLaneOp = true; break;
5327 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
5328 NumVecs = 4; isLoad = false; isLaneOp = true; break;
5329 }
5330 } else {
5331 isLaneOp = true;
5332 switch (N->getOpcode()) {
5333 default: assert(0 && "unexpected opcode for Neon base update");
5334 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
5335 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
5336 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
5337 }
5338 }
5339
5340 // Find the size of memory referenced by the load/store.
5341 EVT VecTy;
5342 if (isLoad)
5343 VecTy = N->getValueType(0);
5344 else
5345 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
5346 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
5347 if (isLaneOp)
5348 NumBytes /= VecTy.getVectorNumElements();
5349
5350 // If the increment is a constant, it must match the memory ref size.
5351 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
5352 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
5353 uint64_t IncVal = CInc->getZExtValue();
5354 if (IncVal != NumBytes)
5355 continue;
5356 } else if (NumBytes >= 3 * 16) {
5357 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
5358 // separate instructions that make it harder to use a non-constant update.
5359 continue;
5360 }
5361
5362 // Create the new updating load/store node.
5363 EVT Tys[6];
5364 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
5365 unsigned n;
5366 for (n = 0; n < NumResultVecs; ++n)
5367 Tys[n] = VecTy;
5368 Tys[n++] = MVT::i32;
5369 Tys[n] = MVT::Other;
5370 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
5371 SmallVector<SDValue, 8> Ops;
5372 Ops.push_back(N->getOperand(0)); // incoming chain
5373 Ops.push_back(N->getOperand(AddrOpIdx));
5374 Ops.push_back(Inc);
5375 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
5376 Ops.push_back(N->getOperand(i));
5377 }
5378 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
5379 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
5380 Ops.data(), Ops.size(),
5381 MemInt->getMemoryVT(),
5382 MemInt->getMemOperand());
5383
5384 // Update the uses.
5385 std::vector<SDValue> NewResults;
5386 for (unsigned i = 0; i < NumResultVecs; ++i) {
5387 NewResults.push_back(SDValue(UpdN.getNode(), i));
5388 }
5389 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
5390 DCI.CombineTo(N, NewResults);
5391 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
5392
5393 break;
5394 }
5395 return SDValue();
5396}
5397
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005398/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5399/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5400/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5401/// return true.
5402static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5403 SelectionDAG &DAG = DCI.DAG;
5404 EVT VT = N->getValueType(0);
5405 // vldN-dup instructions only support 64-bit vectors for N > 1.
5406 if (!VT.is64BitVector())
5407 return false;
5408
5409 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5410 SDNode *VLD = N->getOperand(0).getNode();
5411 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5412 return false;
5413 unsigned NumVecs = 0;
5414 unsigned NewOpc = 0;
5415 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5416 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5417 NumVecs = 2;
5418 NewOpc = ARMISD::VLD2DUP;
5419 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5420 NumVecs = 3;
5421 NewOpc = ARMISD::VLD3DUP;
5422 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5423 NumVecs = 4;
5424 NewOpc = ARMISD::VLD4DUP;
5425 } else {
5426 return false;
5427 }
5428
5429 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5430 // numbers match the load.
5431 unsigned VLDLaneNo =
5432 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5433 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5434 UI != UE; ++UI) {
5435 // Ignore uses of the chain result.
5436 if (UI.getUse().getResNo() == NumVecs)
5437 continue;
5438 SDNode *User = *UI;
5439 if (User->getOpcode() != ARMISD::VDUPLANE ||
5440 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5441 return false;
5442 }
5443
5444 // Create the vldN-dup node.
5445 EVT Tys[5];
5446 unsigned n;
5447 for (n = 0; n < NumVecs; ++n)
5448 Tys[n] = VT;
5449 Tys[n] = MVT::Other;
5450 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5451 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5452 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5453 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5454 Ops, 2, VLDMemInt->getMemoryVT(),
5455 VLDMemInt->getMemOperand());
5456
5457 // Update the uses.
5458 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5459 UI != UE; ++UI) {
5460 unsigned ResNo = UI.getUse().getResNo();
5461 // Ignore uses of the chain result.
5462 if (ResNo == NumVecs)
5463 continue;
5464 SDNode *User = *UI;
5465 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5466 }
5467
5468 // Now the vldN-lane intrinsic is dead except for its chain result.
5469 // Update uses of the chain.
5470 std::vector<SDValue> VLDDupResults;
5471 for (unsigned n = 0; n < NumVecs; ++n)
5472 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5473 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5474 DCI.CombineTo(VLD, VLDDupResults);
5475
5476 return true;
5477}
5478
Bob Wilson9e82bf12010-07-14 01:22:12 +00005479/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5480/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005481static SDValue PerformVDUPLANECombine(SDNode *N,
5482 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005483 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005484
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005485 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5486 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5487 if (CombineVLDDUP(N, DCI))
5488 return SDValue(N, 0);
5489
5490 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5491 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005492 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005493 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005494 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005495 return SDValue();
5496
5497 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5498 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5499 // The canonical VMOV for a zero vector uses a 32-bit element size.
5500 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5501 unsigned EltBits;
5502 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5503 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005504 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005505 if (EltSize > VT.getVectorElementType().getSizeInBits())
5506 return SDValue();
5507
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005508 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005509}
5510
Bob Wilson5bafff32009-06-22 23:27:02 +00005511/// getVShiftImm - Check if this is a valid build_vector for the immediate
5512/// operand of a vector shift operation, where all the elements of the
5513/// build_vector must have the same constant integer value.
5514static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5515 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005516 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005517 Op = Op.getOperand(0);
5518 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5519 APInt SplatBits, SplatUndef;
5520 unsigned SplatBitSize;
5521 bool HasAnyUndefs;
5522 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5523 HasAnyUndefs, ElementBits) ||
5524 SplatBitSize > ElementBits)
5525 return false;
5526 Cnt = SplatBits.getSExtValue();
5527 return true;
5528}
5529
5530/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5531/// operand of a vector shift left operation. That value must be in the range:
5532/// 0 <= Value < ElementBits for a left shift; or
5533/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005534static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005535 assert(VT.isVector() && "vector shift count is not a vector type");
5536 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5537 if (! getVShiftImm(Op, ElementBits, Cnt))
5538 return false;
5539 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5540}
5541
5542/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5543/// operand of a vector shift right operation. For a shift opcode, the value
5544/// is positive, but for an intrinsic the value count must be negative. The
5545/// absolute value must be in the range:
5546/// 1 <= |Value| <= ElementBits for a right shift; or
5547/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005548static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005549 int64_t &Cnt) {
5550 assert(VT.isVector() && "vector shift count is not a vector type");
5551 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5552 if (! getVShiftImm(Op, ElementBits, Cnt))
5553 return false;
5554 if (isIntrinsic)
5555 Cnt = -Cnt;
5556 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5557}
5558
5559/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5560static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5561 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5562 switch (IntNo) {
5563 default:
5564 // Don't do anything for most intrinsics.
5565 break;
5566
5567 // Vector shifts: check for immediate versions and lower them.
5568 // Note: This is done during DAG combining instead of DAG legalizing because
5569 // the build_vectors for 64-bit vector element shift counts are generally
5570 // not legal, and it is hard to see their values after they get legalized to
5571 // loads from a constant pool.
5572 case Intrinsic::arm_neon_vshifts:
5573 case Intrinsic::arm_neon_vshiftu:
5574 case Intrinsic::arm_neon_vshiftls:
5575 case Intrinsic::arm_neon_vshiftlu:
5576 case Intrinsic::arm_neon_vshiftn:
5577 case Intrinsic::arm_neon_vrshifts:
5578 case Intrinsic::arm_neon_vrshiftu:
5579 case Intrinsic::arm_neon_vrshiftn:
5580 case Intrinsic::arm_neon_vqshifts:
5581 case Intrinsic::arm_neon_vqshiftu:
5582 case Intrinsic::arm_neon_vqshiftsu:
5583 case Intrinsic::arm_neon_vqshiftns:
5584 case Intrinsic::arm_neon_vqshiftnu:
5585 case Intrinsic::arm_neon_vqshiftnsu:
5586 case Intrinsic::arm_neon_vqrshiftns:
5587 case Intrinsic::arm_neon_vqrshiftnu:
5588 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005589 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005590 int64_t Cnt;
5591 unsigned VShiftOpc = 0;
5592
5593 switch (IntNo) {
5594 case Intrinsic::arm_neon_vshifts:
5595 case Intrinsic::arm_neon_vshiftu:
5596 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5597 VShiftOpc = ARMISD::VSHL;
5598 break;
5599 }
5600 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5601 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5602 ARMISD::VSHRs : ARMISD::VSHRu);
5603 break;
5604 }
5605 return SDValue();
5606
5607 case Intrinsic::arm_neon_vshiftls:
5608 case Intrinsic::arm_neon_vshiftlu:
5609 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
5610 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005611 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005612
5613 case Intrinsic::arm_neon_vrshifts:
5614 case Intrinsic::arm_neon_vrshiftu:
5615 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
5616 break;
5617 return SDValue();
5618
5619 case Intrinsic::arm_neon_vqshifts:
5620 case Intrinsic::arm_neon_vqshiftu:
5621 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5622 break;
5623 return SDValue();
5624
5625 case Intrinsic::arm_neon_vqshiftsu:
5626 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5627 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005628 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005629
5630 case Intrinsic::arm_neon_vshiftn:
5631 case Intrinsic::arm_neon_vrshiftn:
5632 case Intrinsic::arm_neon_vqshiftns:
5633 case Intrinsic::arm_neon_vqshiftnu:
5634 case Intrinsic::arm_neon_vqshiftnsu:
5635 case Intrinsic::arm_neon_vqrshiftns:
5636 case Intrinsic::arm_neon_vqrshiftnu:
5637 case Intrinsic::arm_neon_vqrshiftnsu:
5638 // Narrowing shifts require an immediate right shift.
5639 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
5640 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00005641 llvm_unreachable("invalid shift count for narrowing vector shift "
5642 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005643
5644 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005645 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00005646 }
5647
5648 switch (IntNo) {
5649 case Intrinsic::arm_neon_vshifts:
5650 case Intrinsic::arm_neon_vshiftu:
5651 // Opcode already set above.
5652 break;
5653 case Intrinsic::arm_neon_vshiftls:
5654 case Intrinsic::arm_neon_vshiftlu:
5655 if (Cnt == VT.getVectorElementType().getSizeInBits())
5656 VShiftOpc = ARMISD::VSHLLi;
5657 else
5658 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
5659 ARMISD::VSHLLs : ARMISD::VSHLLu);
5660 break;
5661 case Intrinsic::arm_neon_vshiftn:
5662 VShiftOpc = ARMISD::VSHRN; break;
5663 case Intrinsic::arm_neon_vrshifts:
5664 VShiftOpc = ARMISD::VRSHRs; break;
5665 case Intrinsic::arm_neon_vrshiftu:
5666 VShiftOpc = ARMISD::VRSHRu; break;
5667 case Intrinsic::arm_neon_vrshiftn:
5668 VShiftOpc = ARMISD::VRSHRN; break;
5669 case Intrinsic::arm_neon_vqshifts:
5670 VShiftOpc = ARMISD::VQSHLs; break;
5671 case Intrinsic::arm_neon_vqshiftu:
5672 VShiftOpc = ARMISD::VQSHLu; break;
5673 case Intrinsic::arm_neon_vqshiftsu:
5674 VShiftOpc = ARMISD::VQSHLsu; break;
5675 case Intrinsic::arm_neon_vqshiftns:
5676 VShiftOpc = ARMISD::VQSHRNs; break;
5677 case Intrinsic::arm_neon_vqshiftnu:
5678 VShiftOpc = ARMISD::VQSHRNu; break;
5679 case Intrinsic::arm_neon_vqshiftnsu:
5680 VShiftOpc = ARMISD::VQSHRNsu; break;
5681 case Intrinsic::arm_neon_vqrshiftns:
5682 VShiftOpc = ARMISD::VQRSHRNs; break;
5683 case Intrinsic::arm_neon_vqrshiftnu:
5684 VShiftOpc = ARMISD::VQRSHRNu; break;
5685 case Intrinsic::arm_neon_vqrshiftnsu:
5686 VShiftOpc = ARMISD::VQRSHRNsu; break;
5687 }
5688
5689 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005690 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005691 }
5692
5693 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00005694 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005695 int64_t Cnt;
5696 unsigned VShiftOpc = 0;
5697
5698 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
5699 VShiftOpc = ARMISD::VSLI;
5700 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
5701 VShiftOpc = ARMISD::VSRI;
5702 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005703 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005704 }
5705
5706 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
5707 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00005708 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005709 }
5710
5711 case Intrinsic::arm_neon_vqrshifts:
5712 case Intrinsic::arm_neon_vqrshiftu:
5713 // No immediate versions of these to check for.
5714 break;
5715 }
5716
5717 return SDValue();
5718}
5719
5720/// PerformShiftCombine - Checks for immediate versions of vector shifts and
5721/// lowers them. As with the vector shift intrinsics, this is done during DAG
5722/// combining instead of DAG legalizing because the build_vectors for 64-bit
5723/// vector element shift counts are generally not legal, and it is hard to see
5724/// their values after they get legalized to loads from a constant pool.
5725static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
5726 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00005727 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00005728
5729 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00005730 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5731 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00005732 return SDValue();
5733
5734 assert(ST->hasNEON() && "unexpected vector shift");
5735 int64_t Cnt;
5736
5737 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005738 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005739
5740 case ISD::SHL:
5741 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
5742 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005743 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005744 break;
5745
5746 case ISD::SRA:
5747 case ISD::SRL:
5748 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
5749 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
5750 ARMISD::VSHRs : ARMISD::VSHRu);
5751 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005753 }
5754 }
5755 return SDValue();
5756}
5757
5758/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
5759/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
5760static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
5761 const ARMSubtarget *ST) {
5762 SDValue N0 = N->getOperand(0);
5763
5764 // Check for sign- and zero-extensions of vector extract operations of 8-
5765 // and 16-bit vector elements. NEON supports these directly. They are
5766 // handled during DAG combining because type legalization will promote them
5767 // to 32-bit types and it is messy to recognize the operations after that.
5768 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5769 SDValue Vec = N0.getOperand(0);
5770 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005771 EVT VT = N->getValueType(0);
5772 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005773 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5774
Owen Anderson825b72b2009-08-11 20:47:22 +00005775 if (VT == MVT::i32 &&
5776 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00005777 TLI.isTypeLegal(Vec.getValueType()) &&
5778 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005779
5780 unsigned Opc = 0;
5781 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005782 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005783 case ISD::SIGN_EXTEND:
5784 Opc = ARMISD::VGETLANEs;
5785 break;
5786 case ISD::ZERO_EXTEND:
5787 case ISD::ANY_EXTEND:
5788 Opc = ARMISD::VGETLANEu;
5789 break;
5790 }
5791 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
5792 }
5793 }
5794
5795 return SDValue();
5796}
5797
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005798/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
5799/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
5800static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
5801 const ARMSubtarget *ST) {
5802 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00005803 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005804 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
5805 // a NaN; only do the transformation when it matches that behavior.
5806
5807 // For now only do this when using NEON for FP operations; if using VFP, it
5808 // is not obvious that the benefit outweighs the cost of switching to the
5809 // NEON pipeline.
5810 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
5811 N->getValueType(0) != MVT::f32)
5812 return SDValue();
5813
5814 SDValue CondLHS = N->getOperand(0);
5815 SDValue CondRHS = N->getOperand(1);
5816 SDValue LHS = N->getOperand(2);
5817 SDValue RHS = N->getOperand(3);
5818 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
5819
5820 unsigned Opcode = 0;
5821 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00005822 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005823 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00005824 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005825 IsReversed = true ; // x CC y ? y : x
5826 } else {
5827 return SDValue();
5828 }
5829
Bob Wilsone742bb52010-02-24 22:15:53 +00005830 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005831 switch (CC) {
5832 default: break;
5833 case ISD::SETOLT:
5834 case ISD::SETOLE:
5835 case ISD::SETLT:
5836 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005837 case ISD::SETULT:
5838 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005839 // If LHS is NaN, an ordered comparison will be false and the result will
5840 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
5841 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5842 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
5843 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5844 break;
5845 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
5846 // will return -0, so vmin can only be used for unsafe math or if one of
5847 // the operands is known to be nonzero.
5848 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
5849 !UnsafeFPMath &&
5850 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5851 break;
5852 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005853 break;
5854
5855 case ISD::SETOGT:
5856 case ISD::SETOGE:
5857 case ISD::SETGT:
5858 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005859 case ISD::SETUGT:
5860 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005861 // If LHS is NaN, an ordered comparison will be false and the result will
5862 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
5863 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5864 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
5865 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5866 break;
5867 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
5868 // will return +0, so vmax can only be used for unsafe math or if one of
5869 // the operands is known to be nonzero.
5870 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
5871 !UnsafeFPMath &&
5872 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5873 break;
5874 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005875 break;
5876 }
5877
5878 if (!Opcode)
5879 return SDValue();
5880 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
5881}
5882
Dan Gohman475871a2008-07-27 21:46:04 +00005883SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005884 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005885 switch (N->getOpcode()) {
5886 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005887 case ISD::ADD: return PerformADDCombine(N, DCI);
5888 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005889 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005890 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00005891 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00005892 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00005893 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005894 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00005895 case ISD::STORE: return PerformSTORECombine(N, DCI);
5896 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
5897 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00005898 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005899 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005900 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005901 case ISD::SHL:
5902 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005903 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005904 case ISD::SIGN_EXTEND:
5905 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005906 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
5907 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00005908 case ARMISD::VLD2DUP:
5909 case ARMISD::VLD3DUP:
5910 case ARMISD::VLD4DUP:
5911 return CombineBaseUpdate(N, DCI);
5912 case ISD::INTRINSIC_VOID:
5913 case ISD::INTRINSIC_W_CHAIN:
5914 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
5915 case Intrinsic::arm_neon_vld1:
5916 case Intrinsic::arm_neon_vld2:
5917 case Intrinsic::arm_neon_vld3:
5918 case Intrinsic::arm_neon_vld4:
5919 case Intrinsic::arm_neon_vld2lane:
5920 case Intrinsic::arm_neon_vld3lane:
5921 case Intrinsic::arm_neon_vld4lane:
5922 case Intrinsic::arm_neon_vst1:
5923 case Intrinsic::arm_neon_vst2:
5924 case Intrinsic::arm_neon_vst3:
5925 case Intrinsic::arm_neon_vst4:
5926 case Intrinsic::arm_neon_vst2lane:
5927 case Intrinsic::arm_neon_vst3lane:
5928 case Intrinsic::arm_neon_vst4lane:
5929 return CombineBaseUpdate(N, DCI);
5930 default: break;
5931 }
5932 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005933 }
Dan Gohman475871a2008-07-27 21:46:04 +00005934 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005935}
5936
Evan Cheng31959b12011-02-02 01:06:55 +00005937bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
5938 EVT VT) const {
5939 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
5940}
5941
Bill Wendlingaf566342009-08-15 21:21:19 +00005942bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00005943 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00005944 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00005945
5946 switch (VT.getSimpleVT().SimpleTy) {
5947 default:
5948 return false;
5949 case MVT::i8:
5950 case MVT::i16:
5951 case MVT::i32:
5952 return true;
5953 // FIXME: VLD1 etc with standard alignment is legal.
5954 }
5955}
5956
Evan Chenge6c835f2009-08-14 20:09:37 +00005957static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
5958 if (V < 0)
5959 return false;
5960
5961 unsigned Scale = 1;
5962 switch (VT.getSimpleVT().SimpleTy) {
5963 default: return false;
5964 case MVT::i1:
5965 case MVT::i8:
5966 // Scale == 1;
5967 break;
5968 case MVT::i16:
5969 // Scale == 2;
5970 Scale = 2;
5971 break;
5972 case MVT::i32:
5973 // Scale == 4;
5974 Scale = 4;
5975 break;
5976 }
5977
5978 if ((V & (Scale - 1)) != 0)
5979 return false;
5980 V /= Scale;
5981 return V == (V & ((1LL << 5) - 1));
5982}
5983
5984static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
5985 const ARMSubtarget *Subtarget) {
5986 bool isNeg = false;
5987 if (V < 0) {
5988 isNeg = true;
5989 V = - V;
5990 }
5991
5992 switch (VT.getSimpleVT().SimpleTy) {
5993 default: return false;
5994 case MVT::i1:
5995 case MVT::i8:
5996 case MVT::i16:
5997 case MVT::i32:
5998 // + imm12 or - imm8
5999 if (isNeg)
6000 return V == (V & ((1LL << 8) - 1));
6001 return V == (V & ((1LL << 12) - 1));
6002 case MVT::f32:
6003 case MVT::f64:
6004 // Same as ARM mode. FIXME: NEON?
6005 if (!Subtarget->hasVFP2())
6006 return false;
6007 if ((V & 3) != 0)
6008 return false;
6009 V >>= 2;
6010 return V == (V & ((1LL << 8) - 1));
6011 }
6012}
6013
Evan Chengb01fad62007-03-12 23:30:29 +00006014/// isLegalAddressImmediate - Return true if the integer value can be used
6015/// as the offset of the target addressing mode for load / store of the
6016/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006017static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006018 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006019 if (V == 0)
6020 return true;
6021
Evan Cheng65011532009-03-09 19:15:00 +00006022 if (!VT.isSimple())
6023 return false;
6024
Evan Chenge6c835f2009-08-14 20:09:37 +00006025 if (Subtarget->isThumb1Only())
6026 return isLegalT1AddressImmediate(V, VT);
6027 else if (Subtarget->isThumb2())
6028 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006029
Evan Chenge6c835f2009-08-14 20:09:37 +00006030 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006031 if (V < 0)
6032 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006033 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006034 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 case MVT::i1:
6036 case MVT::i8:
6037 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006038 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006039 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006040 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006041 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006042 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006043 case MVT::f32:
6044 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006045 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006046 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006047 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006048 return false;
6049 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006050 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006051 }
Evan Chenga8e29892007-01-19 07:51:42 +00006052}
6053
Evan Chenge6c835f2009-08-14 20:09:37 +00006054bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6055 EVT VT) const {
6056 int Scale = AM.Scale;
6057 if (Scale < 0)
6058 return false;
6059
6060 switch (VT.getSimpleVT().SimpleTy) {
6061 default: return false;
6062 case MVT::i1:
6063 case MVT::i8:
6064 case MVT::i16:
6065 case MVT::i32:
6066 if (Scale == 1)
6067 return true;
6068 // r + r << imm
6069 Scale = Scale & ~1;
6070 return Scale == 2 || Scale == 4 || Scale == 8;
6071 case MVT::i64:
6072 // r + r
6073 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6074 return true;
6075 return false;
6076 case MVT::isVoid:
6077 // Note, we allow "void" uses (basically, uses that aren't loads or
6078 // stores), because arm allows folding a scale into many arithmetic
6079 // operations. This should be made more precise and revisited later.
6080
6081 // Allow r << imm, but the imm has to be a multiple of two.
6082 if (Scale & 1) return false;
6083 return isPowerOf2_32(Scale);
6084 }
6085}
6086
Chris Lattner37caf8c2007-04-09 23:33:39 +00006087/// isLegalAddressingMode - Return true if the addressing mode represented
6088/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006089bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006090 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006091 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006092 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006093 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006094
Chris Lattner37caf8c2007-04-09 23:33:39 +00006095 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006096 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006097 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006098
Chris Lattner37caf8c2007-04-09 23:33:39 +00006099 switch (AM.Scale) {
6100 case 0: // no scale reg, must be "r+i" or "r", or "i".
6101 break;
6102 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006103 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006104 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006105 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006106 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006107 // ARM doesn't support any R+R*scale+imm addr modes.
6108 if (AM.BaseOffs)
6109 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006110
Bob Wilson2c7dab12009-04-08 17:55:28 +00006111 if (!VT.isSimple())
6112 return false;
6113
Evan Chenge6c835f2009-08-14 20:09:37 +00006114 if (Subtarget->isThumb2())
6115 return isLegalT2ScaledAddressingMode(AM, VT);
6116
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006117 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006118 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006119 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006120 case MVT::i1:
6121 case MVT::i8:
6122 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006123 if (Scale < 0) Scale = -Scale;
6124 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006125 return true;
6126 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006127 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006128 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006129 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006130 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006131 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006132 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006133 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006134
Owen Anderson825b72b2009-08-11 20:47:22 +00006135 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006136 // Note, we allow "void" uses (basically, uses that aren't loads or
6137 // stores), because arm allows folding a scale into many arithmetic
6138 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006139
Chris Lattner37caf8c2007-04-09 23:33:39 +00006140 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006141 if (Scale & 1) return false;
6142 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006143 }
6144 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006145 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006146 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006147}
6148
Evan Cheng77e47512009-11-11 19:05:52 +00006149/// isLegalICmpImmediate - Return true if the specified immediate is legal
6150/// icmp immediate, that is the target has icmp instructions which can compare
6151/// a register against the immediate without having to materialize the
6152/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006153bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006154 if (!Subtarget->isThumb())
6155 return ARM_AM::getSOImmVal(Imm) != -1;
6156 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006157 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006158 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006159}
6160
Owen Andersone50ed302009-08-10 22:56:29 +00006161static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006162 bool isSEXTLoad, SDValue &Base,
6163 SDValue &Offset, bool &isInc,
6164 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006165 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6166 return false;
6167
Owen Anderson825b72b2009-08-11 20:47:22 +00006168 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006169 // AddressingMode 3
6170 Base = Ptr->getOperand(0);
6171 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006172 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006173 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006174 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006175 isInc = false;
6176 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6177 return true;
6178 }
6179 }
6180 isInc = (Ptr->getOpcode() == ISD::ADD);
6181 Offset = Ptr->getOperand(1);
6182 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006183 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006184 // AddressingMode 2
6185 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006186 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006187 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006188 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006189 isInc = false;
6190 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6191 Base = Ptr->getOperand(0);
6192 return true;
6193 }
6194 }
6195
6196 if (Ptr->getOpcode() == ISD::ADD) {
6197 isInc = true;
6198 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6199 if (ShOpcVal != ARM_AM::no_shift) {
6200 Base = Ptr->getOperand(1);
6201 Offset = Ptr->getOperand(0);
6202 } else {
6203 Base = Ptr->getOperand(0);
6204 Offset = Ptr->getOperand(1);
6205 }
6206 return true;
6207 }
6208
6209 isInc = (Ptr->getOpcode() == ISD::ADD);
6210 Base = Ptr->getOperand(0);
6211 Offset = Ptr->getOperand(1);
6212 return true;
6213 }
6214
Jim Grosbache5165492009-11-09 00:11:35 +00006215 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006216 return false;
6217}
6218
Owen Andersone50ed302009-08-10 22:56:29 +00006219static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006220 bool isSEXTLoad, SDValue &Base,
6221 SDValue &Offset, bool &isInc,
6222 SelectionDAG &DAG) {
6223 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6224 return false;
6225
6226 Base = Ptr->getOperand(0);
6227 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6228 int RHSC = (int)RHS->getZExtValue();
6229 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6230 assert(Ptr->getOpcode() == ISD::ADD);
6231 isInc = false;
6232 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6233 return true;
6234 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6235 isInc = Ptr->getOpcode() == ISD::ADD;
6236 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6237 return true;
6238 }
6239 }
6240
6241 return false;
6242}
6243
Evan Chenga8e29892007-01-19 07:51:42 +00006244/// getPreIndexedAddressParts - returns true by value, base pointer and
6245/// offset pointer and addressing mode by reference if the node's address
6246/// can be legally represented as pre-indexed load / store address.
6247bool
Dan Gohman475871a2008-07-27 21:46:04 +00006248ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6249 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006250 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006251 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006252 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006253 return false;
6254
Owen Andersone50ed302009-08-10 22:56:29 +00006255 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006256 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006257 bool isSEXTLoad = false;
6258 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6259 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006260 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006261 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6262 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6263 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006264 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006265 } else
6266 return false;
6267
6268 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006269 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006270 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006271 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6272 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006273 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006274 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006275 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006276 if (!isLegal)
6277 return false;
6278
6279 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6280 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006281}
6282
6283/// getPostIndexedAddressParts - returns true by value, base pointer and
6284/// offset pointer and addressing mode by reference if this node can be
6285/// combined with a load / store to form a post-indexed load / store.
6286bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006287 SDValue &Base,
6288 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006289 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006290 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006291 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006292 return false;
6293
Owen Andersone50ed302009-08-10 22:56:29 +00006294 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006295 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006296 bool isSEXTLoad = false;
6297 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006298 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006299 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006300 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6301 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006302 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006303 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006304 } else
6305 return false;
6306
6307 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006308 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006309 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006310 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006311 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006312 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006313 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6314 isInc, DAG);
6315 if (!isLegal)
6316 return false;
6317
Evan Cheng28dad2a2010-05-18 21:31:17 +00006318 if (Ptr != Base) {
6319 // Swap base ptr and offset to catch more post-index load / store when
6320 // it's legal. In Thumb2 mode, offset must be an immediate.
6321 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6322 !Subtarget->isThumb2())
6323 std::swap(Base, Offset);
6324
6325 // Post-indexed load / store update the base pointer.
6326 if (Ptr != Base)
6327 return false;
6328 }
6329
Evan Chenge88d5ce2009-07-02 07:28:31 +00006330 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6331 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006332}
6333
Dan Gohman475871a2008-07-27 21:46:04 +00006334void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006335 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006336 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006337 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006338 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006339 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006340 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006341 switch (Op.getOpcode()) {
6342 default: break;
6343 case ARMISD::CMOV: {
6344 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006345 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006346 if (KnownZero == 0 && KnownOne == 0) return;
6347
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006348 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006349 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6350 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006351 KnownZero &= KnownZeroRHS;
6352 KnownOne &= KnownOneRHS;
6353 return;
6354 }
6355 }
6356}
6357
6358//===----------------------------------------------------------------------===//
6359// ARM Inline Assembly Support
6360//===----------------------------------------------------------------------===//
6361
Evan Cheng55d42002011-01-08 01:24:27 +00006362bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6363 // Looking for "rev" which is V6+.
6364 if (!Subtarget->hasV6Ops())
6365 return false;
6366
6367 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6368 std::string AsmStr = IA->getAsmString();
6369 SmallVector<StringRef, 4> AsmPieces;
6370 SplitString(AsmStr, AsmPieces, ";\n");
6371
6372 switch (AsmPieces.size()) {
6373 default: return false;
6374 case 1:
6375 AsmStr = AsmPieces[0];
6376 AsmPieces.clear();
6377 SplitString(AsmStr, AsmPieces, " \t,");
6378
6379 // rev $0, $1
6380 if (AsmPieces.size() == 3 &&
6381 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6382 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6383 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6384 if (Ty && Ty->getBitWidth() == 32)
6385 return IntrinsicLowering::LowerToByteSwap(CI);
6386 }
6387 break;
6388 }
6389
6390 return false;
6391}
6392
Evan Chenga8e29892007-01-19 07:51:42 +00006393/// getConstraintType - Given a constraint letter, return the type of
6394/// constraint it is for this target.
6395ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006396ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6397 if (Constraint.size() == 1) {
6398 switch (Constraint[0]) {
6399 default: break;
6400 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006401 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006402 }
Evan Chenga8e29892007-01-19 07:51:42 +00006403 }
Chris Lattner4234f572007-03-25 02:14:49 +00006404 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006405}
6406
John Thompson44ab89e2010-10-29 17:29:13 +00006407/// Examine constraint type and operand type and determine a weight value.
6408/// This object must already have been set up with the operand type
6409/// and the current alternative constraint selected.
6410TargetLowering::ConstraintWeight
6411ARMTargetLowering::getSingleConstraintMatchWeight(
6412 AsmOperandInfo &info, const char *constraint) const {
6413 ConstraintWeight weight = CW_Invalid;
6414 Value *CallOperandVal = info.CallOperandVal;
6415 // If we don't have a value, we can't do a match,
6416 // but allow it at the lowest weight.
6417 if (CallOperandVal == NULL)
6418 return CW_Default;
6419 const Type *type = CallOperandVal->getType();
6420 // Look at the constraint type.
6421 switch (*constraint) {
6422 default:
6423 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6424 break;
6425 case 'l':
6426 if (type->isIntegerTy()) {
6427 if (Subtarget->isThumb())
6428 weight = CW_SpecificReg;
6429 else
6430 weight = CW_Register;
6431 }
6432 break;
6433 case 'w':
6434 if (type->isFloatingPointTy())
6435 weight = CW_Register;
6436 break;
6437 }
6438 return weight;
6439}
6440
Bob Wilson2dc4f542009-03-20 22:42:55 +00006441std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006442ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006443 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006444 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006445 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006446 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006447 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006448 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006449 return std::make_pair(0U, ARM::tGPRRegisterClass);
6450 else
6451 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006452 case 'r':
6453 return std::make_pair(0U, ARM::GPRRegisterClass);
6454 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006455 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006456 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006457 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006458 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006459 if (VT.getSizeInBits() == 128)
6460 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006461 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006462 }
6463 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006464 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006465 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006466
Evan Chenga8e29892007-01-19 07:51:42 +00006467 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6468}
6469
6470std::vector<unsigned> ARMTargetLowering::
6471getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006472 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006473 if (Constraint.size() != 1)
6474 return std::vector<unsigned>();
6475
6476 switch (Constraint[0]) { // GCC ARM Constraint Letters
6477 default: break;
6478 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006479 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6480 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6481 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006482 case 'r':
6483 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6484 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6485 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6486 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006487 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006488 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006489 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6490 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6491 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6492 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6493 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6494 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6495 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6496 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006497 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006498 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6499 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6500 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6501 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006502 if (VT.getSizeInBits() == 128)
6503 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6504 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006505 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006506 }
6507
6508 return std::vector<unsigned>();
6509}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006510
6511/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6512/// vector. If it is invalid, don't add anything to Ops.
6513void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6514 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006515 std::vector<SDValue>&Ops,
6516 SelectionDAG &DAG) const {
6517 SDValue Result(0, 0);
6518
6519 switch (Constraint) {
6520 default: break;
6521 case 'I': case 'J': case 'K': case 'L':
6522 case 'M': case 'N': case 'O':
6523 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6524 if (!C)
6525 return;
6526
6527 int64_t CVal64 = C->getSExtValue();
6528 int CVal = (int) CVal64;
6529 // None of these constraints allow values larger than 32 bits. Check
6530 // that the value fits in an int.
6531 if (CVal != CVal64)
6532 return;
6533
6534 switch (Constraint) {
6535 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006536 if (Subtarget->isThumb1Only()) {
6537 // This must be a constant between 0 and 255, for ADD
6538 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006539 if (CVal >= 0 && CVal <= 255)
6540 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006541 } else if (Subtarget->isThumb2()) {
6542 // A constant that can be used as an immediate value in a
6543 // data-processing instruction.
6544 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6545 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006546 } else {
6547 // A constant that can be used as an immediate value in a
6548 // data-processing instruction.
6549 if (ARM_AM::getSOImmVal(CVal) != -1)
6550 break;
6551 }
6552 return;
6553
6554 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006555 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006556 // This must be a constant between -255 and -1, for negated ADD
6557 // immediates. This can be used in GCC with an "n" modifier that
6558 // prints the negated value, for use with SUB instructions. It is
6559 // not useful otherwise but is implemented for compatibility.
6560 if (CVal >= -255 && CVal <= -1)
6561 break;
6562 } else {
6563 // This must be a constant between -4095 and 4095. It is not clear
6564 // what this constraint is intended for. Implemented for
6565 // compatibility with GCC.
6566 if (CVal >= -4095 && CVal <= 4095)
6567 break;
6568 }
6569 return;
6570
6571 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006572 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006573 // A 32-bit value where only one byte has a nonzero value. Exclude
6574 // zero to match GCC. This constraint is used by GCC internally for
6575 // constants that can be loaded with a move/shift combination.
6576 // It is not useful otherwise but is implemented for compatibility.
6577 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6578 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006579 } else if (Subtarget->isThumb2()) {
6580 // A constant whose bitwise inverse can be used as an immediate
6581 // value in a data-processing instruction. This can be used in GCC
6582 // with a "B" modifier that prints the inverted value, for use with
6583 // BIC and MVN instructions. It is not useful otherwise but is
6584 // implemented for compatibility.
6585 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
6586 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006587 } else {
6588 // A constant whose bitwise inverse can be used as an immediate
6589 // value in a data-processing instruction. This can be used in GCC
6590 // with a "B" modifier that prints the inverted value, for use with
6591 // BIC and MVN instructions. It is not useful otherwise but is
6592 // implemented for compatibility.
6593 if (ARM_AM::getSOImmVal(~CVal) != -1)
6594 break;
6595 }
6596 return;
6597
6598 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006599 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006600 // This must be a constant between -7 and 7,
6601 // for 3-operand ADD/SUB immediate instructions.
6602 if (CVal >= -7 && CVal < 7)
6603 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006604 } else if (Subtarget->isThumb2()) {
6605 // A constant whose negation can be used as an immediate value in a
6606 // data-processing instruction. This can be used in GCC with an "n"
6607 // modifier that prints the negated value, for use with SUB
6608 // instructions. It is not useful otherwise but is implemented for
6609 // compatibility.
6610 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
6611 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006612 } else {
6613 // A constant whose negation can be used as an immediate value in a
6614 // data-processing instruction. This can be used in GCC with an "n"
6615 // modifier that prints the negated value, for use with SUB
6616 // instructions. It is not useful otherwise but is implemented for
6617 // compatibility.
6618 if (ARM_AM::getSOImmVal(-CVal) != -1)
6619 break;
6620 }
6621 return;
6622
6623 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006624 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006625 // This must be a multiple of 4 between 0 and 1020, for
6626 // ADD sp + immediate.
6627 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
6628 break;
6629 } else {
6630 // A power of two or a constant between 0 and 32. This is used in
6631 // GCC for the shift amount on shifted register operands, but it is
6632 // useful in general for any shift amounts.
6633 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
6634 break;
6635 }
6636 return;
6637
6638 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006639 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006640 // This must be a constant between 0 and 31, for shift amounts.
6641 if (CVal >= 0 && CVal <= 31)
6642 break;
6643 }
6644 return;
6645
6646 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006647 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006648 // This must be a multiple of 4 between -508 and 508, for
6649 // ADD/SUB sp = sp + immediate.
6650 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
6651 break;
6652 }
6653 return;
6654 }
6655 Result = DAG.getTargetConstant(CVal, Op.getValueType());
6656 break;
6657 }
6658
6659 if (Result.getNode()) {
6660 Ops.push_back(Result);
6661 return;
6662 }
Dale Johannesen1784d162010-06-25 21:55:36 +00006663 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006664}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00006665
6666bool
6667ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6668 // The ARM target isn't yet aware of offsets.
6669 return false;
6670}
Evan Cheng39382422009-10-28 01:44:26 +00006671
6672int ARM::getVFPf32Imm(const APFloat &FPImm) {
6673 APInt Imm = FPImm.bitcastToAPInt();
6674 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
6675 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
6676 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
6677
6678 // We can handle 4 bits of mantissa.
6679 // mantissa = (16+UInt(e:f:g:h))/16.
6680 if (Mantissa & 0x7ffff)
6681 return -1;
6682 Mantissa >>= 19;
6683 if ((Mantissa & 0xf) != Mantissa)
6684 return -1;
6685
6686 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6687 if (Exp < -3 || Exp > 4)
6688 return -1;
6689 Exp = ((Exp+3) & 0x7) ^ 4;
6690
6691 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6692}
6693
6694int ARM::getVFPf64Imm(const APFloat &FPImm) {
6695 APInt Imm = FPImm.bitcastToAPInt();
6696 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
6697 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
6698 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
6699
6700 // We can handle 4 bits of mantissa.
6701 // mantissa = (16+UInt(e:f:g:h))/16.
6702 if (Mantissa & 0xffffffffffffLL)
6703 return -1;
6704 Mantissa >>= 48;
6705 if ((Mantissa & 0xf) != Mantissa)
6706 return -1;
6707
6708 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6709 if (Exp < -3 || Exp > 4)
6710 return -1;
6711 Exp = ((Exp+3) & 0x7) ^ 4;
6712
6713 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6714}
6715
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006716bool ARM::isBitFieldInvertedMask(unsigned v) {
6717 if (v == 0xffffffff)
6718 return 0;
6719 // there can be 1's on either or both "outsides", all the "inside"
6720 // bits must be 0's
6721 unsigned int lsb = 0, msb = 31;
6722 while (v & (1 << msb)) --msb;
6723 while (v & (1 << lsb)) ++lsb;
6724 for (unsigned int i = lsb; i <= msb; ++i) {
6725 if (v & (1 << i))
6726 return 0;
6727 }
6728 return 1;
6729}
6730
Evan Cheng39382422009-10-28 01:44:26 +00006731/// isFPImmLegal - Returns true if the target can instruction select the
6732/// specified FP immediate natively. If false, the legalizer will
6733/// materialize the FP immediate as a load from a constant pool.
6734bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
6735 if (!Subtarget->hasVFP3())
6736 return false;
6737 if (VT == MVT::f32)
6738 return ARM::getVFPf32Imm(Imm) != -1;
6739 if (VT == MVT::f64)
6740 return ARM::getVFPf64Imm(Imm) != -1;
6741 return false;
6742}
Bob Wilson65ffec42010-09-21 17:56:22 +00006743
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006744/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00006745/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
6746/// specified in the intrinsic calls.
6747bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
6748 const CallInst &I,
6749 unsigned Intrinsic) const {
6750 switch (Intrinsic) {
6751 case Intrinsic::arm_neon_vld1:
6752 case Intrinsic::arm_neon_vld2:
6753 case Intrinsic::arm_neon_vld3:
6754 case Intrinsic::arm_neon_vld4:
6755 case Intrinsic::arm_neon_vld2lane:
6756 case Intrinsic::arm_neon_vld3lane:
6757 case Intrinsic::arm_neon_vld4lane: {
6758 Info.opc = ISD::INTRINSIC_W_CHAIN;
6759 // Conservatively set memVT to the entire set of vectors loaded.
6760 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
6761 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6762 Info.ptrVal = I.getArgOperand(0);
6763 Info.offset = 0;
6764 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6765 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6766 Info.vol = false; // volatile loads with NEON intrinsics not supported
6767 Info.readMem = true;
6768 Info.writeMem = false;
6769 return true;
6770 }
6771 case Intrinsic::arm_neon_vst1:
6772 case Intrinsic::arm_neon_vst2:
6773 case Intrinsic::arm_neon_vst3:
6774 case Intrinsic::arm_neon_vst4:
6775 case Intrinsic::arm_neon_vst2lane:
6776 case Intrinsic::arm_neon_vst3lane:
6777 case Intrinsic::arm_neon_vst4lane: {
6778 Info.opc = ISD::INTRINSIC_VOID;
6779 // Conservatively set memVT to the entire set of vectors stored.
6780 unsigned NumElts = 0;
6781 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
6782 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
6783 if (!ArgTy->isVectorTy())
6784 break;
6785 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
6786 }
6787 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6788 Info.ptrVal = I.getArgOperand(0);
6789 Info.offset = 0;
6790 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6791 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6792 Info.vol = false; // volatile stores with NEON intrinsics not supported
6793 Info.readMem = false;
6794 Info.writeMem = true;
6795 return true;
6796 }
6797 default:
6798 break;
6799 }
6800
6801 return false;
6802}