blob: 50557671ab511d1e101886a43c16412bc1c31e44 [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 Wilson5bafff32009-06-22 23:27:02 +0000460 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
461 setTargetDAGCombine(ISD::SHL);
462 setTargetDAGCombine(ISD::SRL);
463 setTargetDAGCombine(ISD::SRA);
464 setTargetDAGCombine(ISD::SIGN_EXTEND);
465 setTargetDAGCombine(ISD::ZERO_EXTEND);
466 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000467 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000468 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000469 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000470 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
471 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000472 }
473
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000474 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000475
476 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000477 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000478
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000479 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000481
Evan Chenga8e29892007-01-19 07:51:42 +0000482 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000483 if (!Subtarget->isThumb1Only()) {
484 for (unsigned im = (unsigned)ISD::PRE_INC;
485 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setIndexedLoadAction(im, MVT::i1, Legal);
487 setIndexedLoadAction(im, MVT::i8, Legal);
488 setIndexedLoadAction(im, MVT::i16, Legal);
489 setIndexedLoadAction(im, MVT::i32, Legal);
490 setIndexedStoreAction(im, MVT::i1, Legal);
491 setIndexedStoreAction(im, MVT::i8, Legal);
492 setIndexedStoreAction(im, MVT::i16, Legal);
493 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000494 }
Evan Chenga8e29892007-01-19 07:51:42 +0000495 }
496
497 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000498 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::MUL, MVT::i64, Expand);
500 setOperationAction(ISD::MULHU, MVT::i32, Expand);
501 setOperationAction(ISD::MULHS, MVT::i32, Expand);
502 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
503 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000504 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::MUL, MVT::i64, Expand);
506 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000507 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000509 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000510 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000511 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000512 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 setOperationAction(ISD::SRL, MVT::i64, Custom);
514 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000515
516 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000518 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000520 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000522
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000523 // Only ARMv6 has BSWAP.
524 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000526
Evan Chenga8e29892007-01-19 07:51:42 +0000527 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000528 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000529 // v7M has a hardware divider
530 setOperationAction(ISD::SDIV, MVT::i32, Expand);
531 setOperationAction(ISD::UDIV, MVT::i32, Expand);
532 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::SREM, MVT::i32, Expand);
534 setOperationAction(ISD::UREM, MVT::i32, Expand);
535 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
536 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
539 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
540 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
541 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000542 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000543
Evan Chengfb3611d2010-05-11 07:26:32 +0000544 setOperationAction(ISD::TRAP, MVT::Other, Legal);
545
Evan Chenga8e29892007-01-19 07:51:42 +0000546 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::VASTART, MVT::Other, Custom);
548 setOperationAction(ISD::VAARG, MVT::Other, Expand);
549 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
550 setOperationAction(ISD::VAEND, MVT::Other, Expand);
551 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
552 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000553 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000554 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
555 setExceptionPointerRegister(ARM::R0);
556 setExceptionSelectorRegister(ARM::R1);
557
Evan Cheng3a1588a2010-04-15 22:20:34 +0000558 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000559 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
560 // the default expansion.
561 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000562 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000563 // membarrier needs custom lowering; the rest are legal and handled
564 // normally.
565 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
566 } else {
567 // Set them all for expansion, which will force libcalls.
568 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
569 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
570 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
571 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000572 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
573 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
574 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000575 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
576 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
577 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
578 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
579 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
580 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
581 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
582 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
583 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
584 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
585 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
586 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
587 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
588 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
589 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
590 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
591 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
592 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000593 // Since the libcalls include locking, fold in the fences
594 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000595 }
596 // 64-bit versions are always libcalls (for now)
597 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000598 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000599 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000605
Evan Cheng416941d2010-11-04 05:19:35 +0000606 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000607
Eli Friedmana2c6f452010-06-26 04:36:50 +0000608 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
609 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
611 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000612 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000614
Nate Begemand1fb5832010-08-03 21:31:55 +0000615 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000616 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
617 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000618 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000619 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
620 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000621
622 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000624 if (Subtarget->isTargetDarwin()) {
625 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
626 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000627 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000628 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::SETCC, MVT::i32, Expand);
631 setOperationAction(ISD::SETCC, MVT::f32, Expand);
632 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000633 setOperationAction(ISD::SELECT, MVT::i32, Custom);
634 setOperationAction(ISD::SELECT, MVT::f32, Custom);
635 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
637 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
638 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000639
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
641 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
642 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
643 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
644 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000645
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000646 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::FSIN, MVT::f64, Expand);
648 setOperationAction(ISD::FSIN, MVT::f32, Expand);
649 setOperationAction(ISD::FCOS, MVT::f32, Expand);
650 setOperationAction(ISD::FCOS, MVT::f64, Expand);
651 setOperationAction(ISD::FREM, MVT::f64, Expand);
652 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000653 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
655 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000656 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::FPOW, MVT::f64, Expand);
658 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000659
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000660 // Various VFP goodness
661 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000662 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
663 if (Subtarget->hasVFP2()) {
664 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
665 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
666 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
667 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
668 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000669 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000670 if (!Subtarget->hasFP16()) {
671 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
672 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000673 }
Evan Cheng110cf482008-04-01 01:50:16 +0000674 }
Evan Chenga8e29892007-01-19 07:51:42 +0000675
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000676 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000677 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000678 setTargetDAGCombine(ISD::ADD);
679 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000680 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000681
Owen Anderson080c0922010-11-05 19:27:46 +0000682 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000683 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000684 if (Subtarget->hasNEON())
685 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000686
Evan Chenga8e29892007-01-19 07:51:42 +0000687 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000688
Evan Chengf7d87ee2010-05-21 00:43:17 +0000689 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
690 setSchedulingPreference(Sched::RegPressure);
691 else
692 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000693
Evan Cheng05219282011-01-06 06:52:41 +0000694 //// temporary - rewrite interface to use type
695 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000696
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000697 // On ARM arguments smaller than 4 bytes are extended, so all arguments
698 // are at least 4 bytes aligned.
699 setMinStackArgumentAlignment(4);
700
Evan Chengfff606d2010-09-24 19:07:23 +0000701 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000702}
703
Andrew Trick32cec0a2011-01-19 02:35:27 +0000704// FIXME: It might make sense to define the representative register class as the
705// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
706// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
707// SPR's representative would be DPR_VFP2. This should work well if register
708// pressure tracking were modified such that a register use would increment the
709// pressure of the register class's representative and all of it's super
710// classes' representatives transitively. We have not implemented this because
711// of the difficulty prior to coalescing of modeling operand register classes
712// due to the common occurence of cross class copies and subregister insertions
713// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000714std::pair<const TargetRegisterClass*, uint8_t>
715ARMTargetLowering::findRepresentativeClass(EVT VT) const{
716 const TargetRegisterClass *RRC = 0;
717 uint8_t Cost = 1;
718 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000719 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000720 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000721 // Use DPR as representative register class for all floating point
722 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
723 // the cost is 1 for both f32 and f64.
724 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000725 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000726 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000727 // When NEON is used for SP, only half of the register file is available
728 // because operations that define both SP and DP results will be constrained
729 // to the VFP2 class (D0-D15). We currently model this constraint prior to
730 // coalescing by double-counting the SP regs. See the FIXME above.
731 if (Subtarget->useNEONForSinglePrecisionFP())
732 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000733 break;
734 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
735 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000736 RRC = ARM::DPRRegisterClass;
737 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000738 break;
739 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000740 RRC = ARM::DPRRegisterClass;
741 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000742 break;
743 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000744 RRC = ARM::DPRRegisterClass;
745 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000746 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000747 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000748 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000749}
750
Evan Chenga8e29892007-01-19 07:51:42 +0000751const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
752 switch (Opcode) {
753 default: return 0;
754 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000755 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000756 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000757 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
758 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000759 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000760 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
761 case ARMISD::tCALL: return "ARMISD::tCALL";
762 case ARMISD::BRCOND: return "ARMISD::BRCOND";
763 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000764 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000765 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
766 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
767 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000768 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000769 case ARMISD::CMPFP: return "ARMISD::CMPFP";
770 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000771 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000772 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
773 case ARMISD::CMOV: return "ARMISD::CMOV";
774 case ARMISD::CNEG: return "ARMISD::CNEG";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000775
Jim Grosbach3482c802010-01-18 19:58:49 +0000776 case ARMISD::RBIT: return "ARMISD::RBIT";
777
Bob Wilson76a312b2010-03-19 22:51:32 +0000778 case ARMISD::FTOSI: return "ARMISD::FTOSI";
779 case ARMISD::FTOUI: return "ARMISD::FTOUI";
780 case ARMISD::SITOF: return "ARMISD::SITOF";
781 case ARMISD::UITOF: return "ARMISD::UITOF";
782
Evan Chenga8e29892007-01-19 07:51:42 +0000783 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
784 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
785 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000786
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000787 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
788 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000789
Evan Chengc5942082009-10-28 06:55:03 +0000790 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
791 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000792 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000793
Dale Johannesen51e28e62010-06-03 21:09:53 +0000794 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000795
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000796 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000797
Evan Cheng86198642009-08-07 00:34:42 +0000798 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
799
Jim Grosbach3728e962009-12-10 00:11:09 +0000800 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000801 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000802
Evan Chengdfed19f2010-11-03 06:34:55 +0000803 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
804
Bob Wilson5bafff32009-06-22 23:27:02 +0000805 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000806 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000807 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000808 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
809 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000810 case ARMISD::VCGEU: return "ARMISD::VCGEU";
811 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000812 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
813 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000814 case ARMISD::VCGTU: return "ARMISD::VCGTU";
815 case ARMISD::VTST: return "ARMISD::VTST";
816
817 case ARMISD::VSHL: return "ARMISD::VSHL";
818 case ARMISD::VSHRs: return "ARMISD::VSHRs";
819 case ARMISD::VSHRu: return "ARMISD::VSHRu";
820 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
821 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
822 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
823 case ARMISD::VSHRN: return "ARMISD::VSHRN";
824 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
825 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
826 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
827 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
828 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
829 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
830 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
831 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
832 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
833 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
834 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
835 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
836 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
837 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000838 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000839 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000840 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000841 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000842 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000843 case ARMISD::VREV64: return "ARMISD::VREV64";
844 case ARMISD::VREV32: return "ARMISD::VREV32";
845 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000846 case ARMISD::VZIP: return "ARMISD::VZIP";
847 case ARMISD::VUZP: return "ARMISD::VUZP";
848 case ARMISD::VTRN: return "ARMISD::VTRN";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000849 case ARMISD::VMULLs: return "ARMISD::VMULLs";
850 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000851 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000852 case ARMISD::FMAX: return "ARMISD::FMAX";
853 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000854 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000855 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
856 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000857 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
858 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
859 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Evan Chenga8e29892007-01-19 07:51:42 +0000860 }
861}
862
Evan Cheng06b666c2010-05-15 02:18:07 +0000863/// getRegClassFor - Return the register class that should be used for the
864/// specified value type.
865TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
866 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
867 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
868 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000869 if (Subtarget->hasNEON()) {
870 if (VT == MVT::v4i64)
871 return ARM::QQPRRegisterClass;
872 else if (VT == MVT::v8i64)
873 return ARM::QQQQPRRegisterClass;
874 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000875 return TargetLowering::getRegClassFor(VT);
876}
877
Eric Christopherab695882010-07-21 22:26:11 +0000878// Create a fast isel object.
879FastISel *
880ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
881 return ARM::createFastISel(funcInfo);
882}
883
Bill Wendlingb4202b82009-07-01 18:50:55 +0000884/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000885unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000886 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000887}
888
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000889/// getMaximalGlobalOffset - Returns the maximal possible offset which can
890/// be used for loads / stores from the global.
891unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
892 return (Subtarget->isThumb1Only() ? 127 : 4095);
893}
894
Evan Cheng1cc39842010-05-20 23:26:43 +0000895Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000896 unsigned NumVals = N->getNumValues();
897 if (!NumVals)
898 return Sched::RegPressure;
899
900 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000901 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000902 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000903 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000904 if (VT.isFloatingPoint() || VT.isVector())
905 return Sched::Latency;
906 }
Evan Chengc10f5432010-05-28 23:25:23 +0000907
908 if (!N->isMachineOpcode())
909 return Sched::RegPressure;
910
911 // Load are scheduled for latency even if there instruction itinerary
912 // is not available.
913 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
914 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000915
916 if (TID.getNumDefs() == 0)
917 return Sched::RegPressure;
918 if (!Itins->isEmpty() &&
919 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000920 return Sched::Latency;
921
Evan Cheng1cc39842010-05-20 23:26:43 +0000922 return Sched::RegPressure;
923}
924
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000925// FIXME: Move to RegInfo
Evan Cheng31446872010-07-23 22:39:59 +0000926unsigned
927ARMTargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
928 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000929 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000930
Evan Cheng31446872010-07-23 22:39:59 +0000931 switch (RC->getID()) {
932 default:
933 return 0;
934 case ARM::tGPRRegClassID:
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000935 return TFI->hasFP(MF) ? 4 : 5;
Evan Chengac096802010-08-10 19:30:19 +0000936 case ARM::GPRRegClassID: {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000937 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
Evan Chengac096802010-08-10 19:30:19 +0000938 return 10 - FP - (Subtarget->isR9Reserved() ? 1 : 0);
939 }
Evan Cheng31446872010-07-23 22:39:59 +0000940 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
941 case ARM::DPRRegClassID:
942 return 32 - 10;
943 }
944}
945
Evan Chenga8e29892007-01-19 07:51:42 +0000946//===----------------------------------------------------------------------===//
947// Lowering Code
948//===----------------------------------------------------------------------===//
949
Evan Chenga8e29892007-01-19 07:51:42 +0000950/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
951static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
952 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000953 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000954 case ISD::SETNE: return ARMCC::NE;
955 case ISD::SETEQ: return ARMCC::EQ;
956 case ISD::SETGT: return ARMCC::GT;
957 case ISD::SETGE: return ARMCC::GE;
958 case ISD::SETLT: return ARMCC::LT;
959 case ISD::SETLE: return ARMCC::LE;
960 case ISD::SETUGT: return ARMCC::HI;
961 case ISD::SETUGE: return ARMCC::HS;
962 case ISD::SETULT: return ARMCC::LO;
963 case ISD::SETULE: return ARMCC::LS;
964 }
965}
966
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000967/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
968static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000969 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000970 CondCode2 = ARMCC::AL;
971 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000972 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000973 case ISD::SETEQ:
974 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
975 case ISD::SETGT:
976 case ISD::SETOGT: CondCode = ARMCC::GT; break;
977 case ISD::SETGE:
978 case ISD::SETOGE: CondCode = ARMCC::GE; break;
979 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000980 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000981 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
982 case ISD::SETO: CondCode = ARMCC::VC; break;
983 case ISD::SETUO: CondCode = ARMCC::VS; break;
984 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
985 case ISD::SETUGT: CondCode = ARMCC::HI; break;
986 case ISD::SETUGE: CondCode = ARMCC::PL; break;
987 case ISD::SETLT:
988 case ISD::SETULT: CondCode = ARMCC::LT; break;
989 case ISD::SETLE:
990 case ISD::SETULE: CondCode = ARMCC::LE; break;
991 case ISD::SETNE:
992 case ISD::SETUNE: CondCode = ARMCC::NE; break;
993 }
Evan Chenga8e29892007-01-19 07:51:42 +0000994}
995
Bob Wilson1f595bb2009-04-17 19:07:39 +0000996//===----------------------------------------------------------------------===//
997// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +0000998//===----------------------------------------------------------------------===//
999
1000#include "ARMGenCallingConv.inc"
1001
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001002/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1003/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001004CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001005 bool Return,
1006 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001007 switch (CC) {
1008 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001009 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001010 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001011 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001012 if (!Subtarget->isAAPCS_ABI())
1013 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1014 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1015 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1016 }
1017 // Fallthrough
1018 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001019 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001020 if (!Subtarget->isAAPCS_ABI())
1021 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1022 else if (Subtarget->hasVFP2() &&
1023 FloatABIType == FloatABI::Hard && !isVarArg)
1024 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1025 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1026 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001027 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001028 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001029 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001030 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001031 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001032 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001033 }
1034}
1035
Dan Gohman98ca4f22009-08-05 01:29:28 +00001036/// LowerCallResult - Lower the result values of a call into the
1037/// appropriate copies out of appropriate physical registers.
1038SDValue
1039ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001040 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001041 const SmallVectorImpl<ISD::InputArg> &Ins,
1042 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001043 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001044
Bob Wilson1f595bb2009-04-17 19:07:39 +00001045 // Assign locations to each value returned by this call.
1046 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001047 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001048 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001049 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001050 CCAssignFnForNode(CallConv, /* Return*/ true,
1051 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001052
1053 // Copy all of the result registers out of their specified physreg.
1054 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1055 CCValAssign VA = RVLocs[i];
1056
Bob Wilson80915242009-04-25 00:33:20 +00001057 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001058 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001059 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001061 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001062 Chain = Lo.getValue(1);
1063 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001064 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001065 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001066 InFlag);
1067 Chain = Hi.getValue(1);
1068 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001069 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001070
Owen Anderson825b72b2009-08-11 20:47:22 +00001071 if (VA.getLocVT() == MVT::v2f64) {
1072 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1073 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1074 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001075
1076 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001077 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001078 Chain = Lo.getValue(1);
1079 InFlag = Lo.getValue(2);
1080 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001081 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001082 Chain = Hi.getValue(1);
1083 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001084 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1086 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001087 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001088 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001089 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1090 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001091 Chain = Val.getValue(1);
1092 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001093 }
Bob Wilson80915242009-04-25 00:33:20 +00001094
1095 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001096 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001097 case CCValAssign::Full: break;
1098 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001099 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001100 break;
1101 }
1102
Dan Gohman98ca4f22009-08-05 01:29:28 +00001103 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104 }
1105
Dan Gohman98ca4f22009-08-05 01:29:28 +00001106 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107}
1108
1109/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1110/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001111/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112/// a byval function parameter.
1113/// Sometimes what we are copying is the end of a larger object, the part that
1114/// does not fit in registers.
1115static SDValue
1116CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1117 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1118 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001119 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001120 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001121 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001122 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001123}
1124
Bob Wilsondee46d72009-04-17 20:35:10 +00001125/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001127ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1128 SDValue StackPtr, SDValue Arg,
1129 DebugLoc dl, SelectionDAG &DAG,
1130 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001131 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132 unsigned LocMemOffset = VA.getLocMemOffset();
1133 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1134 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001135 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001137
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001139 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001140 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001141}
1142
Dan Gohman98ca4f22009-08-05 01:29:28 +00001143void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001144 SDValue Chain, SDValue &Arg,
1145 RegsToPassVector &RegsToPass,
1146 CCValAssign &VA, CCValAssign &NextVA,
1147 SDValue &StackPtr,
1148 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001149 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001150
Jim Grosbache5165492009-11-09 00:11:35 +00001151 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001152 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001153 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1154
1155 if (NextVA.isRegLoc())
1156 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1157 else {
1158 assert(NextVA.isMemLoc());
1159 if (StackPtr.getNode() == 0)
1160 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1161
Dan Gohman98ca4f22009-08-05 01:29:28 +00001162 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1163 dl, DAG, NextVA,
1164 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001165 }
1166}
1167
Dan Gohman98ca4f22009-08-05 01:29:28 +00001168/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001169/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1170/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001171SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001172ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001173 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001174 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001176 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177 const SmallVectorImpl<ISD::InputArg> &Ins,
1178 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001179 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001180 MachineFunction &MF = DAG.getMachineFunction();
1181 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1182 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001183 // Temporarily disable tail calls so things don't break.
1184 if (!EnableARMTailCalls)
1185 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001186 if (isTailCall) {
1187 // Check if it's really possible to do a tail call.
1188 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1189 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001190 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001191 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1192 // detected sibcalls.
1193 if (isTailCall) {
1194 ++NumTailCalls;
1195 IsSibCall = true;
1196 }
1197 }
Evan Chenga8e29892007-01-19 07:51:42 +00001198
Bob Wilson1f595bb2009-04-17 19:07:39 +00001199 // Analyze operands of the call, assigning locations to each operand.
1200 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001201 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1202 *DAG.getContext());
1203 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001204 CCAssignFnForNode(CallConv, /* Return*/ false,
1205 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001206
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 // Get a count of how many bytes are to be pushed on the stack.
1208 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001209
Dale Johannesen51e28e62010-06-03 21:09:53 +00001210 // For tail calls, memory operands are available in our caller's stack.
1211 if (IsSibCall)
1212 NumBytes = 0;
1213
Evan Chenga8e29892007-01-19 07:51:42 +00001214 // Adjust the stack pointer for the new arguments...
1215 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001216 if (!IsSibCall)
1217 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001218
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001219 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001220
Bob Wilson5bafff32009-06-22 23:27:02 +00001221 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001223
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001225 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001226 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1227 i != e;
1228 ++i, ++realArgIdx) {
1229 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001230 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001231 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Evan Chenga8e29892007-01-19 07:51:42 +00001232
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 // Promote the value if needed.
1234 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001235 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236 case CCValAssign::Full: break;
1237 case CCValAssign::SExt:
1238 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1239 break;
1240 case CCValAssign::ZExt:
1241 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1242 break;
1243 case CCValAssign::AExt:
1244 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1245 break;
1246 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001247 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001248 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001249 }
1250
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001251 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 if (VA.getLocVT() == MVT::v2f64) {
1254 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1255 DAG.getConstant(0, MVT::i32));
1256 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1257 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258
Dan Gohman98ca4f22009-08-05 01:29:28 +00001259 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001260 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1261
1262 VA = ArgLocs[++i]; // skip ahead to next loc
1263 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001264 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001265 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1266 } else {
1267 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001268
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1270 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001271 }
1272 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001275 }
1276 } else if (VA.isRegLoc()) {
1277 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001278 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001280
Dan Gohman98ca4f22009-08-05 01:29:28 +00001281 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1282 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001283 }
Evan Chenga8e29892007-01-19 07:51:42 +00001284 }
1285
1286 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001287 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001288 &MemOpChains[0], MemOpChains.size());
1289
1290 // Build a sequence of copy-to-reg nodes chained together with token chain
1291 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001292 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001293 // Tail call byval lowering might overwrite argument registers so in case of
1294 // tail call optimization the copies to registers are lowered later.
1295 if (!isTailCall)
1296 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1297 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1298 RegsToPass[i].second, InFlag);
1299 InFlag = Chain.getValue(1);
1300 }
Evan Chenga8e29892007-01-19 07:51:42 +00001301
Dale Johannesen51e28e62010-06-03 21:09:53 +00001302 // For tail calls lower the arguments to the 'real' stack slot.
1303 if (isTailCall) {
1304 // Force all the incoming stack arguments to be loaded from the stack
1305 // before any new outgoing arguments are stored to the stack, because the
1306 // outgoing stack slots may alias the incoming argument stack slots, and
1307 // the alias isn't otherwise explicit. This is slightly more conservative
1308 // than necessary, because it means that each store effectively depends
1309 // on every argument instead of just those arguments it would clobber.
1310
1311 // Do not flag preceeding copytoreg stuff together with the following stuff.
1312 InFlag = SDValue();
1313 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1314 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1315 RegsToPass[i].second, InFlag);
1316 InFlag = Chain.getValue(1);
1317 }
1318 InFlag =SDValue();
1319 }
1320
Bill Wendling056292f2008-09-16 21:48:12 +00001321 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1322 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1323 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001324 bool isDirect = false;
1325 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001326 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001327 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001328
1329 if (EnableARMLongCalls) {
1330 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1331 && "long-calls with non-static relocation model!");
1332 // Handle a global address or an external symbol. If it's not one of
1333 // those, the target's already in a register, so we don't need to do
1334 // anything extra.
1335 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001336 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001337 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001338 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001339 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1340 ARMPCLabelIndex,
1341 ARMCP::CPValue, 0);
1342 // Get the address of the callee into a register
1343 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1344 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1345 Callee = DAG.getLoad(getPointerTy(), dl,
1346 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001347 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001348 false, false, 0);
1349 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1350 const char *Sym = S->getSymbol();
1351
1352 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001353 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001354 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1355 Sym, ARMPCLabelIndex, 0);
1356 // Get the address of the callee into a register
1357 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1358 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1359 Callee = DAG.getLoad(getPointerTy(), dl,
1360 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001361 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001362 false, false, 0);
1363 }
1364 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001365 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001366 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001367 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001368 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001369 getTargetMachine().getRelocationModel() != Reloc::Static;
1370 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001371 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001372 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001373 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001374 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001375 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001376 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001377 ARMPCLabelIndex,
1378 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001379 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001380 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001381 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001382 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001383 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001384 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001385 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001386 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001387 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001388 } else {
1389 // On ELF targets for PIC code, direct calls should go through the PLT
1390 unsigned OpFlags = 0;
1391 if (Subtarget->isTargetELF() &&
1392 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1393 OpFlags = ARMII::MO_PLT;
1394 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1395 }
Bill Wendling056292f2008-09-16 21:48:12 +00001396 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001397 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001398 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001399 getTargetMachine().getRelocationModel() != Reloc::Static;
1400 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001401 // tBX takes a register source operand.
1402 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001403 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001404 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001405 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001406 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001407 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001408 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001409 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001410 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001411 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001412 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001413 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001414 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001415 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001416 } else {
1417 unsigned OpFlags = 0;
1418 // On ELF targets for PIC code, direct calls should go through the PLT
1419 if (Subtarget->isTargetELF() &&
1420 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1421 OpFlags = ARMII::MO_PLT;
1422 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1423 }
Evan Chenga8e29892007-01-19 07:51:42 +00001424 }
1425
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001426 // FIXME: handle tail calls differently.
1427 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001428 if (Subtarget->isThumb()) {
1429 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001430 CallOpc = ARMISD::CALL_NOLINK;
1431 else
1432 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1433 } else {
1434 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001435 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1436 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001437 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001438
Dan Gohman475871a2008-07-27 21:46:04 +00001439 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001440 Ops.push_back(Chain);
1441 Ops.push_back(Callee);
1442
1443 // Add argument registers to the end of the list so that they are known live
1444 // into the call.
1445 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1446 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1447 RegsToPass[i].second.getValueType()));
1448
Gabor Greifba36cb52008-08-28 21:40:38 +00001449 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001450 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001451
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001452 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001453 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001454 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001455
Duncan Sands4bdcb612008-07-02 17:40:58 +00001456 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001457 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001458 InFlag = Chain.getValue(1);
1459
Chris Lattnere563bbc2008-10-11 22:08:30 +00001460 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1461 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001462 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001463 InFlag = Chain.getValue(1);
1464
Bob Wilson1f595bb2009-04-17 19:07:39 +00001465 // Handle result values, copying them out of physregs into vregs that we
1466 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001467 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1468 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001469}
1470
Dale Johannesen51e28e62010-06-03 21:09:53 +00001471/// MatchingStackOffset - Return true if the given stack call argument is
1472/// already available in the same position (relatively) of the caller's
1473/// incoming argument stack.
1474static
1475bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1476 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1477 const ARMInstrInfo *TII) {
1478 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1479 int FI = INT_MAX;
1480 if (Arg.getOpcode() == ISD::CopyFromReg) {
1481 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001482 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001483 return false;
1484 MachineInstr *Def = MRI->getVRegDef(VR);
1485 if (!Def)
1486 return false;
1487 if (!Flags.isByVal()) {
1488 if (!TII->isLoadFromStackSlot(Def, FI))
1489 return false;
1490 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001491 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001492 }
1493 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1494 if (Flags.isByVal())
1495 // ByVal argument is passed in as a pointer but it's now being
1496 // dereferenced. e.g.
1497 // define @foo(%struct.X* %A) {
1498 // tail call @bar(%struct.X* byval %A)
1499 // }
1500 return false;
1501 SDValue Ptr = Ld->getBasePtr();
1502 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1503 if (!FINode)
1504 return false;
1505 FI = FINode->getIndex();
1506 } else
1507 return false;
1508
1509 assert(FI != INT_MAX);
1510 if (!MFI->isFixedObjectIndex(FI))
1511 return false;
1512 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1513}
1514
1515/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1516/// for tail call optimization. Targets which want to do tail call
1517/// optimization should implement this function.
1518bool
1519ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1520 CallingConv::ID CalleeCC,
1521 bool isVarArg,
1522 bool isCalleeStructRet,
1523 bool isCallerStructRet,
1524 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001525 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001526 const SmallVectorImpl<ISD::InputArg> &Ins,
1527 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001528 const Function *CallerF = DAG.getMachineFunction().getFunction();
1529 CallingConv::ID CallerCC = CallerF->getCallingConv();
1530 bool CCMatch = CallerCC == CalleeCC;
1531
1532 // Look for obvious safe cases to perform tail call optimization that do not
1533 // require ABI changes. This is what gcc calls sibcall.
1534
Jim Grosbach7616b642010-06-16 23:45:49 +00001535 // Do not sibcall optimize vararg calls unless the call site is not passing
1536 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001537 if (isVarArg && !Outs.empty())
1538 return false;
1539
1540 // Also avoid sibcall optimization if either caller or callee uses struct
1541 // return semantics.
1542 if (isCalleeStructRet || isCallerStructRet)
1543 return false;
1544
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001545 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001546 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001547 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1548 // LR. This means if we need to reload LR, it takes an extra instructions,
1549 // which outweighs the value of the tail call; but here we don't know yet
1550 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001551 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001552 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001553
1554 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1555 // but we need to make sure there are enough registers; the only valid
1556 // registers are the 4 used for parameters. We don't currently do this
1557 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001558 if (Subtarget->isThumb1Only())
1559 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001560
Dale Johannesen51e28e62010-06-03 21:09:53 +00001561 // If the calling conventions do not match, then we'd better make sure the
1562 // results are returned in the same way as what the caller expects.
1563 if (!CCMatch) {
1564 SmallVector<CCValAssign, 16> RVLocs1;
1565 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1566 RVLocs1, *DAG.getContext());
1567 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1568
1569 SmallVector<CCValAssign, 16> RVLocs2;
1570 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1571 RVLocs2, *DAG.getContext());
1572 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1573
1574 if (RVLocs1.size() != RVLocs2.size())
1575 return false;
1576 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1577 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1578 return false;
1579 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1580 return false;
1581 if (RVLocs1[i].isRegLoc()) {
1582 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1583 return false;
1584 } else {
1585 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1586 return false;
1587 }
1588 }
1589 }
1590
1591 // If the callee takes no arguments then go on to check the results of the
1592 // call.
1593 if (!Outs.empty()) {
1594 // Check if stack adjustment is needed. For now, do not do this if any
1595 // argument is passed on the stack.
1596 SmallVector<CCValAssign, 16> ArgLocs;
1597 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1598 ArgLocs, *DAG.getContext());
1599 CCInfo.AnalyzeCallOperands(Outs,
1600 CCAssignFnForNode(CalleeCC, false, isVarArg));
1601 if (CCInfo.getNextStackOffset()) {
1602 MachineFunction &MF = DAG.getMachineFunction();
1603
1604 // Check if the arguments are already laid out in the right way as
1605 // the caller's fixed stack objects.
1606 MachineFrameInfo *MFI = MF.getFrameInfo();
1607 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1608 const ARMInstrInfo *TII =
1609 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001610 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1611 i != e;
1612 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001613 CCValAssign &VA = ArgLocs[i];
1614 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001615 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001616 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001617 if (VA.getLocInfo() == CCValAssign::Indirect)
1618 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001619 if (VA.needsCustom()) {
1620 // f64 and vector types are split into multiple registers or
1621 // register/stack-slot combinations. The types will not match
1622 // the registers; give up on memory f64 refs until we figure
1623 // out what to do about this.
1624 if (!VA.isRegLoc())
1625 return false;
1626 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001627 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001628 if (RegVT == MVT::v2f64) {
1629 if (!ArgLocs[++i].isRegLoc())
1630 return false;
1631 if (!ArgLocs[++i].isRegLoc())
1632 return false;
1633 }
1634 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001635 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1636 MFI, MRI, TII))
1637 return false;
1638 }
1639 }
1640 }
1641 }
1642
1643 return true;
1644}
1645
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646SDValue
1647ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001648 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001650 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001651 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001652
Bob Wilsondee46d72009-04-17 20:35:10 +00001653 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001654 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001655
Bob Wilsondee46d72009-04-17 20:35:10 +00001656 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001657 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1658 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001659
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001661 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1662 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001663
1664 // If this is the first return lowered for this function, add
1665 // the regs to the liveout set for the function.
1666 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1667 for (unsigned i = 0; i != RVLocs.size(); ++i)
1668 if (RVLocs[i].isRegLoc())
1669 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001670 }
1671
Bob Wilson1f595bb2009-04-17 19:07:39 +00001672 SDValue Flag;
1673
1674 // Copy the result values into the output registers.
1675 for (unsigned i = 0, realRVLocIdx = 0;
1676 i != RVLocs.size();
1677 ++i, ++realRVLocIdx) {
1678 CCValAssign &VA = RVLocs[i];
1679 assert(VA.isRegLoc() && "Can only return in registers!");
1680
Dan Gohmanc9403652010-07-07 15:54:55 +00001681 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001682
1683 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001684 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001685 case CCValAssign::Full: break;
1686 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001687 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001688 break;
1689 }
1690
Bob Wilson1f595bb2009-04-17 19:07:39 +00001691 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001692 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001693 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001694 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1695 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001696 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001698
1699 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1700 Flag = Chain.getValue(1);
1701 VA = RVLocs[++i]; // skip ahead to next loc
1702 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1703 HalfGPRs.getValue(1), Flag);
1704 Flag = Chain.getValue(1);
1705 VA = RVLocs[++i]; // skip ahead to next loc
1706
1707 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1709 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001710 }
1711 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1712 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001713 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001714 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001715 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001716 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001717 VA = RVLocs[++i]; // skip ahead to next loc
1718 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1719 Flag);
1720 } else
1721 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1722
Bob Wilsondee46d72009-04-17 20:35:10 +00001723 // Guarantee that all emitted copies are
1724 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001725 Flag = Chain.getValue(1);
1726 }
1727
1728 SDValue result;
1729 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001730 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001731 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001732 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001733
1734 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001735}
1736
Evan Cheng3d2125c2010-11-30 23:55:39 +00001737bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1738 if (N->getNumValues() != 1)
1739 return false;
1740 if (!N->hasNUsesOfValue(1, 0))
1741 return false;
1742
1743 unsigned NumCopies = 0;
1744 SDNode* Copies[2];
1745 SDNode *Use = *N->use_begin();
1746 if (Use->getOpcode() == ISD::CopyToReg) {
1747 Copies[NumCopies++] = Use;
1748 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1749 // f64 returned in a pair of GPRs.
1750 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1751 UI != UE; ++UI) {
1752 if (UI->getOpcode() != ISD::CopyToReg)
1753 return false;
1754 Copies[UI.getUse().getResNo()] = *UI;
1755 ++NumCopies;
1756 }
1757 } else if (Use->getOpcode() == ISD::BITCAST) {
1758 // f32 returned in a single GPR.
1759 if (!Use->hasNUsesOfValue(1, 0))
1760 return false;
1761 Use = *Use->use_begin();
1762 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1763 return false;
1764 Copies[NumCopies++] = Use;
1765 } else {
1766 return false;
1767 }
1768
1769 if (NumCopies != 1 && NumCopies != 2)
1770 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001771
1772 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001773 for (unsigned i = 0; i < NumCopies; ++i) {
1774 SDNode *Copy = Copies[i];
1775 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1776 UI != UE; ++UI) {
1777 if (UI->getOpcode() == ISD::CopyToReg) {
1778 SDNode *Use = *UI;
1779 if (Use == Copies[0] || Use == Copies[1])
1780 continue;
1781 return false;
1782 }
1783 if (UI->getOpcode() != ARMISD::RET_FLAG)
1784 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001785 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001786 }
1787 }
1788
Evan Cheng1bf891a2010-12-01 22:59:46 +00001789 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001790}
1791
Bob Wilsonb62d2572009-11-03 00:02:05 +00001792// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1793// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1794// one of the above mentioned nodes. It has to be wrapped because otherwise
1795// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1796// be used to form addressing mode. These wrapped nodes will be selected
1797// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001798static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001799 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001800 // FIXME there is no actual debug info here
1801 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001802 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001803 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001804 if (CP->isMachineConstantPoolEntry())
1805 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1806 CP->getAlignment());
1807 else
1808 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1809 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001810 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001811}
1812
Jim Grosbache1102ca2010-07-19 17:20:38 +00001813unsigned ARMTargetLowering::getJumpTableEncoding() const {
1814 return MachineJumpTableInfo::EK_Inline;
1815}
1816
Dan Gohmand858e902010-04-17 15:26:15 +00001817SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1818 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001819 MachineFunction &MF = DAG.getMachineFunction();
1820 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1821 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001822 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001823 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001824 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001825 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1826 SDValue CPAddr;
1827 if (RelocM == Reloc::Static) {
1828 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1829 } else {
1830 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001831 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001832 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1833 ARMCP::CPBlockAddress,
1834 PCAdj);
1835 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1836 }
1837 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1838 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001839 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001840 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001841 if (RelocM == Reloc::Static)
1842 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001843 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001844 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001845}
1846
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001847// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001848SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001849ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001850 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001851 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001852 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001853 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001854 MachineFunction &MF = DAG.getMachineFunction();
1855 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001856 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001857 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001858 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001859 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001860 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001861 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001862 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001863 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001864 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001865 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001866
Evan Chenge7e0d622009-11-06 22:24:13 +00001867 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001868 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001869
1870 // call __tls_get_addr.
1871 ArgListTy Args;
1872 ArgListEntry Entry;
1873 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001874 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001875 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001876 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001877 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001878 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1879 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001880 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001881 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001882 return CallResult.first;
1883}
1884
1885// Lower ISD::GlobalTLSAddress using the "initial exec" or
1886// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001887SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001888ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001889 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001890 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001891 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001892 SDValue Offset;
1893 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001894 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001895 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001896 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001897
Chris Lattner4fb63d02009-07-15 04:12:33 +00001898 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001899 MachineFunction &MF = DAG.getMachineFunction();
1900 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001901 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001902 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001903 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1904 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001905 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001906 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001907 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001909 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001910 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001911 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001912 Chain = Offset.getValue(1);
1913
Evan Chenge7e0d622009-11-06 22:24:13 +00001914 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001915 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001916
Evan Cheng9eda6892009-10-31 03:39:36 +00001917 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001918 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001919 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001920 } else {
1921 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001922 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001923 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001924 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001925 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001926 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001927 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001928 }
1929
1930 // The address of the thread local variable is the add of the thread
1931 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001932 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001933}
1934
Dan Gohman475871a2008-07-27 21:46:04 +00001935SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001936ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001937 // TODO: implement the "local dynamic" model
1938 assert(Subtarget->isTargetELF() &&
1939 "TLS not implemented for non-ELF targets");
1940 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1941 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1942 // otherwise use the "Local Exec" TLS Model
1943 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1944 return LowerToTLSGeneralDynamicModel(GA, DAG);
1945 else
1946 return LowerToTLSExecModels(GA, DAG);
1947}
1948
Dan Gohman475871a2008-07-27 21:46:04 +00001949SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001950 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001951 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001952 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001953 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001954 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1955 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001956 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001957 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001958 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001959 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001961 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001962 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001963 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001964 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001966 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001967 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001968 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001969 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001970 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001971 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001972 }
1973
1974 // If we have T2 ops, we can materialize the address directly via movt/movw
1975 // pair. This is always cheaper.
1976 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00001977 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001978 // FIXME: Once remat is capable of dealing with instructions with register
1979 // operands, expand this into two nodes.
1980 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1981 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001982 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001983 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1984 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1985 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1986 MachinePointerInfo::getConstantPool(),
1987 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001988 }
1989}
1990
Dan Gohman475871a2008-07-27 21:46:04 +00001991SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001992 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001993 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001994 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001995 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001996 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001997 MachineFunction &MF = DAG.getMachineFunction();
1998 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1999
2000 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002001 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002002 // FIXME: Once remat is capable of dealing with instructions with register
2003 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002004 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002005 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2006 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2007
Evan Cheng53519f02011-01-21 18:55:51 +00002008 unsigned Wrapper = (RelocM == Reloc::PIC_)
2009 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2010 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002011 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002012 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2013 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2014 MachinePointerInfo::getGOT(), false, false, 0);
2015 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002016 }
2017
2018 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002020 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002021 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002022 } else {
2023 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002024 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2025 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002026 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002027 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002028 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002030
Evan Cheng9eda6892009-10-31 03:39:36 +00002031 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002032 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002033 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002035
2036 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002037 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002038 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002039 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002040
Evan Cheng63476a82009-09-03 07:04:02 +00002041 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002042 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002043 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002044
2045 return Result;
2046}
2047
Dan Gohman475871a2008-07-27 21:46:04 +00002048SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002049 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002050 assert(Subtarget->isTargetELF() &&
2051 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002052 MachineFunction &MF = DAG.getMachineFunction();
2053 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002054 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002055 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002056 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002057 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002058 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2059 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002060 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002061 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002062 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002063 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002064 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002065 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002066 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002067 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002068}
2069
Jim Grosbach0e0da732009-05-12 23:59:14 +00002070SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002071ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2072 const {
2073 DebugLoc dl = Op.getDebugLoc();
2074 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2075 Op.getOperand(0), Op.getOperand(1));
2076}
2077
2078SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002079ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2080 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002081 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002082 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2083 Op.getOperand(1), Val);
2084}
2085
2086SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002087ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2088 DebugLoc dl = Op.getDebugLoc();
2089 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2090 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2091}
2092
2093SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002094ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002095 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002096 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002097 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002098 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002099 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002100 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002101 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002102 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2103 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002104 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002105 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002106 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002107 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002108 EVT PtrVT = getPointerTy();
2109 DebugLoc dl = Op.getDebugLoc();
2110 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2111 SDValue CPAddr;
2112 unsigned PCAdj = (RelocM != Reloc::PIC_)
2113 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002114 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002115 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2116 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002117 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002118 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002119 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002120 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002121 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002122 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002123
2124 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002125 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002126 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2127 }
2128 return Result;
2129 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002130 }
2131}
2132
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002133static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002134 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002135 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002136 if (!Subtarget->hasDataBarrier()) {
2137 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2138 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2139 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002140 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002141 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002142 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002143 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002144 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002145
2146 SDValue Op5 = Op.getOperand(5);
2147 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2148 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2149 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2150 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2151
2152 ARM_MB::MemBOpt DMBOpt;
2153 if (isDeviceBarrier)
2154 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2155 else
2156 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2157 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2158 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002159}
2160
Evan Chengdfed19f2010-11-03 06:34:55 +00002161static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2162 const ARMSubtarget *Subtarget) {
2163 // ARM pre v5TE and Thumb1 does not have preload instructions.
2164 if (!(Subtarget->isThumb2() ||
2165 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2166 // Just preserve the chain.
2167 return Op.getOperand(0);
2168
2169 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002170 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2171 if (!isRead &&
2172 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2173 // ARMv7 with MP extension has PLDW.
2174 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002175
2176 if (Subtarget->isThumb())
2177 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002178 isRead = ~isRead & 1;
2179 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002180
Evan Cheng416941d2010-11-04 05:19:35 +00002181 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002182 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002183 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2184 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002185}
2186
Dan Gohman1e93df62010-04-17 14:41:14 +00002187static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2188 MachineFunction &MF = DAG.getMachineFunction();
2189 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2190
Evan Chenga8e29892007-01-19 07:51:42 +00002191 // vastart just stores the address of the VarArgsFrameIndex slot into the
2192 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002193 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002194 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002195 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002196 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002197 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2198 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002199}
2200
Dan Gohman475871a2008-07-27 21:46:04 +00002201SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002202ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2203 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002204 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002205 MachineFunction &MF = DAG.getMachineFunction();
2206 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2207
2208 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002209 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002210 RC = ARM::tGPRRegisterClass;
2211 else
2212 RC = ARM::GPRRegisterClass;
2213
2214 // Transform the arguments stored in physical registers into virtual ones.
Devang Patele9a7ea62011-01-31 21:38:14 +00002215 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002216 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002217
2218 SDValue ArgValue2;
2219 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002220 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002221 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002222
2223 // Create load node to retrieve arguments from the stack.
2224 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002225 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002226 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002227 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002228 } else {
Devang Patele9a7ea62011-01-31 21:38:14 +00002229 Reg = MF.addLiveIn(NextVA.getLocReg(), RC, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002230 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002231 }
2232
Jim Grosbache5165492009-11-09 00:11:35 +00002233 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002234}
2235
2236SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002237ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002238 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002239 const SmallVectorImpl<ISD::InputArg>
2240 &Ins,
2241 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002242 SmallVectorImpl<SDValue> &InVals)
2243 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002244
Bob Wilson1f595bb2009-04-17 19:07:39 +00002245 MachineFunction &MF = DAG.getMachineFunction();
2246 MachineFrameInfo *MFI = MF.getFrameInfo();
2247
Bob Wilson1f595bb2009-04-17 19:07:39 +00002248 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2249
2250 // Assign locations to all of the incoming arguments.
2251 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002252 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2253 *DAG.getContext());
2254 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002255 CCAssignFnForNode(CallConv, /* Return*/ false,
2256 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002257
2258 SmallVector<SDValue, 16> ArgValues;
2259
2260 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2261 CCValAssign &VA = ArgLocs[i];
2262
Bob Wilsondee46d72009-04-17 20:35:10 +00002263 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002264 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002265 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002266
Bob Wilson5bafff32009-06-22 23:27:02 +00002267 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002268 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002269 // f64 and vector types are split up into multiple registers or
2270 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002272 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002273 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002274 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002275 SDValue ArgValue2;
2276 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002277 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002278 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2279 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002280 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002281 false, false, 0);
2282 } else {
2283 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2284 Chain, DAG, dl);
2285 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002286 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2287 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002288 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002289 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002290 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2291 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002292 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002293
Bob Wilson5bafff32009-06-22 23:27:02 +00002294 } else {
2295 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002296
Owen Anderson825b72b2009-08-11 20:47:22 +00002297 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002298 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002300 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002301 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002302 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002303 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002304 RC = (AFI->isThumb1OnlyFunction() ?
2305 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002306 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002307 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002308
2309 // Transform the arguments in physical registers into virtual ones.
Devang Patele9a7ea62011-01-31 21:38:14 +00002310 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002311 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002312 }
2313
2314 // If this is an 8 or 16-bit value, it is really passed promoted
2315 // to 32 bits. Insert an assert[sz]ext to capture this, then
2316 // truncate to the right size.
2317 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002318 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002319 case CCValAssign::Full: break;
2320 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002321 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002322 break;
2323 case CCValAssign::SExt:
2324 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2325 DAG.getValueType(VA.getValVT()));
2326 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2327 break;
2328 case CCValAssign::ZExt:
2329 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2330 DAG.getValueType(VA.getValVT()));
2331 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2332 break;
2333 }
2334
Dan Gohman98ca4f22009-08-05 01:29:28 +00002335 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002336
2337 } else { // VA.isRegLoc()
2338
2339 // sanity check
2340 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002341 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002342
2343 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002344 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002345
Bob Wilsondee46d72009-04-17 20:35:10 +00002346 // Create load nodes to retrieve arguments from the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002347 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002348 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002349 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002350 false, false, 0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002351 }
2352 }
2353
2354 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002355 if (isVarArg) {
2356 static const unsigned GPRArgRegs[] = {
2357 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2358 };
2359
Bob Wilsondee46d72009-04-17 20:35:10 +00002360 unsigned NumGPRs = CCInfo.getFirstUnallocated
2361 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002362
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002363 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002364 unsigned VARegSize = (4 - NumGPRs) * 4;
2365 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002366 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002367 if (VARegSaveSize) {
2368 // If this function is vararg, store any remaining integer argument regs
2369 // to their spots on the stack so that they may be loaded by deferencing
2370 // the result of va_next.
2371 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002372 AFI->setVarArgsFrameIndex(
2373 MFI->CreateFixedObject(VARegSaveSize,
2374 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002375 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002376 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2377 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002378
Dan Gohman475871a2008-07-27 21:46:04 +00002379 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002380 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002381 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002382 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002383 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002384 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002385 RC = ARM::GPRRegisterClass;
2386
Devang Patele9a7ea62011-01-31 21:38:14 +00002387 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002388 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002389 SDValue Store =
2390 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002391 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2392 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002393 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002394 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002395 DAG.getConstant(4, getPointerTy()));
2396 }
2397 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002398 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002399 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002400 } else
2401 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002402 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002403 }
2404
Dan Gohman98ca4f22009-08-05 01:29:28 +00002405 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002406}
2407
2408/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002409static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002410 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002411 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002412 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002413 // Maybe this has already been legalized into the constant pool?
2414 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002415 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002416 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002417 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002418 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002419 }
2420 }
2421 return false;
2422}
2423
Evan Chenga8e29892007-01-19 07:51:42 +00002424/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2425/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002426SDValue
2427ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002428 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002429 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002430 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002431 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002432 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002433 // Constant does not fit, try adjusting it by one?
2434 switch (CC) {
2435 default: break;
2436 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002437 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002438 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002439 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002440 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002441 }
2442 break;
2443 case ISD::SETULT:
2444 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002445 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002446 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002447 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002448 }
2449 break;
2450 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002451 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002452 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002453 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002455 }
2456 break;
2457 case ISD::SETULE:
2458 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002459 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002460 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002461 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002462 }
2463 break;
2464 }
2465 }
2466 }
2467
2468 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002469 ARMISD::NodeType CompareType;
2470 switch (CondCode) {
2471 default:
2472 CompareType = ARMISD::CMP;
2473 break;
2474 case ARMCC::EQ:
2475 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002476 // Uses only Z Flag
2477 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002478 break;
2479 }
Evan Cheng218977b2010-07-13 19:27:42 +00002480 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002481 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002482}
2483
2484/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002485SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002486ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002487 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002488 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002489 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002490 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002491 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002492 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2493 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002494}
2495
Bill Wendlingde2b1512010-08-11 08:43:16 +00002496SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2497 SDValue Cond = Op.getOperand(0);
2498 SDValue SelectTrue = Op.getOperand(1);
2499 SDValue SelectFalse = Op.getOperand(2);
2500 DebugLoc dl = Op.getDebugLoc();
2501
2502 // Convert:
2503 //
2504 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2505 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2506 //
2507 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2508 const ConstantSDNode *CMOVTrue =
2509 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2510 const ConstantSDNode *CMOVFalse =
2511 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2512
2513 if (CMOVTrue && CMOVFalse) {
2514 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2515 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2516
2517 SDValue True;
2518 SDValue False;
2519 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2520 True = SelectTrue;
2521 False = SelectFalse;
2522 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2523 True = SelectFalse;
2524 False = SelectTrue;
2525 }
2526
2527 if (True.getNode() && False.getNode()) {
2528 EVT VT = Cond.getValueType();
2529 SDValue ARMcc = Cond.getOperand(2);
2530 SDValue CCR = Cond.getOperand(3);
2531 SDValue Cmp = Cond.getOperand(4);
2532 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2533 }
2534 }
2535 }
2536
2537 return DAG.getSelectCC(dl, Cond,
2538 DAG.getConstant(0, Cond.getValueType()),
2539 SelectTrue, SelectFalse, ISD::SETNE);
2540}
2541
Dan Gohmand858e902010-04-17 15:26:15 +00002542SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002543 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002544 SDValue LHS = Op.getOperand(0);
2545 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002546 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002547 SDValue TrueVal = Op.getOperand(2);
2548 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002549 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002550
Owen Anderson825b72b2009-08-11 20:47:22 +00002551 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002552 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002553 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002554 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2555 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002556 }
2557
2558 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002559 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002560
Evan Cheng218977b2010-07-13 19:27:42 +00002561 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2562 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002563 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002564 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002565 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002566 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002567 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002568 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002569 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002570 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002571 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002572 }
2573 return Result;
2574}
2575
Evan Cheng218977b2010-07-13 19:27:42 +00002576/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2577/// to morph to an integer compare sequence.
2578static bool canChangeToInt(SDValue Op, bool &SeenZero,
2579 const ARMSubtarget *Subtarget) {
2580 SDNode *N = Op.getNode();
2581 if (!N->hasOneUse())
2582 // Otherwise it requires moving the value from fp to integer registers.
2583 return false;
2584 if (!N->getNumValues())
2585 return false;
2586 EVT VT = Op.getValueType();
2587 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2588 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2589 // vmrs are very slow, e.g. cortex-a8.
2590 return false;
2591
2592 if (isFloatingPointZero(Op)) {
2593 SeenZero = true;
2594 return true;
2595 }
2596 return ISD::isNormalLoad(N);
2597}
2598
2599static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2600 if (isFloatingPointZero(Op))
2601 return DAG.getConstant(0, MVT::i32);
2602
2603 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2604 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002605 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002606 Ld->isVolatile(), Ld->isNonTemporal(),
2607 Ld->getAlignment());
2608
2609 llvm_unreachable("Unknown VFP cmp argument!");
2610}
2611
2612static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2613 SDValue &RetVal1, SDValue &RetVal2) {
2614 if (isFloatingPointZero(Op)) {
2615 RetVal1 = DAG.getConstant(0, MVT::i32);
2616 RetVal2 = DAG.getConstant(0, MVT::i32);
2617 return;
2618 }
2619
2620 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2621 SDValue Ptr = Ld->getBasePtr();
2622 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2623 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002624 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002625 Ld->isVolatile(), Ld->isNonTemporal(),
2626 Ld->getAlignment());
2627
2628 EVT PtrType = Ptr.getValueType();
2629 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2630 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2631 PtrType, Ptr, DAG.getConstant(4, PtrType));
2632 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2633 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002634 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002635 Ld->isVolatile(), Ld->isNonTemporal(),
2636 NewAlign);
2637 return;
2638 }
2639
2640 llvm_unreachable("Unknown VFP cmp argument!");
2641}
2642
2643/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2644/// f32 and even f64 comparisons to integer ones.
2645SDValue
2646ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2647 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002648 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002649 SDValue LHS = Op.getOperand(2);
2650 SDValue RHS = Op.getOperand(3);
2651 SDValue Dest = Op.getOperand(4);
2652 DebugLoc dl = Op.getDebugLoc();
2653
2654 bool SeenZero = false;
2655 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2656 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002657 // If one of the operand is zero, it's safe to ignore the NaN case since
2658 // we only care about equality comparisons.
2659 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Evan Cheng218977b2010-07-13 19:27:42 +00002660 // If unsafe fp math optimization is enabled and there are no othter uses of
2661 // the CMP operands, and the condition code is EQ oe NE, we can optimize it
2662 // to an integer comparison.
2663 if (CC == ISD::SETOEQ)
2664 CC = ISD::SETEQ;
2665 else if (CC == ISD::SETUNE)
2666 CC = ISD::SETNE;
2667
2668 SDValue ARMcc;
2669 if (LHS.getValueType() == MVT::f32) {
2670 LHS = bitcastf32Toi32(LHS, DAG);
2671 RHS = bitcastf32Toi32(RHS, DAG);
2672 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2673 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2674 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2675 Chain, Dest, ARMcc, CCR, Cmp);
2676 }
2677
2678 SDValue LHS1, LHS2;
2679 SDValue RHS1, RHS2;
2680 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2681 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2682 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2683 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002684 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002685 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2686 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2687 }
2688
2689 return SDValue();
2690}
2691
2692SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2693 SDValue Chain = Op.getOperand(0);
2694 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2695 SDValue LHS = Op.getOperand(2);
2696 SDValue RHS = Op.getOperand(3);
2697 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002698 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002699
Owen Anderson825b72b2009-08-11 20:47:22 +00002700 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002701 SDValue ARMcc;
2702 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002703 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002704 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002705 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002706 }
2707
Owen Anderson825b72b2009-08-11 20:47:22 +00002708 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002709
2710 if (UnsafeFPMath &&
2711 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2712 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2713 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2714 if (Result.getNode())
2715 return Result;
2716 }
2717
Evan Chenga8e29892007-01-19 07:51:42 +00002718 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002719 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002720
Evan Cheng218977b2010-07-13 19:27:42 +00002721 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2722 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002723 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002724 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002725 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002726 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002727 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002728 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2729 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002730 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002731 }
2732 return Res;
2733}
2734
Dan Gohmand858e902010-04-17 15:26:15 +00002735SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002736 SDValue Chain = Op.getOperand(0);
2737 SDValue Table = Op.getOperand(1);
2738 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002739 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002740
Owen Andersone50ed302009-08-10 22:56:29 +00002741 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002742 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2743 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002744 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002745 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002746 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002747 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2748 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002749 if (Subtarget->isThumb2()) {
2750 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2751 // which does another jump to the destination. This also makes it easier
2752 // to translate it to TBB / TBH later.
2753 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002754 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002755 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002756 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002757 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002758 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002759 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002760 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002761 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002762 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002763 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002764 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002765 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002766 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002767 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002768 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002769 }
Evan Chenga8e29892007-01-19 07:51:42 +00002770}
2771
Bob Wilson76a312b2010-03-19 22:51:32 +00002772static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2773 DebugLoc dl = Op.getDebugLoc();
2774 unsigned Opc;
2775
2776 switch (Op.getOpcode()) {
2777 default:
2778 assert(0 && "Invalid opcode!");
2779 case ISD::FP_TO_SINT:
2780 Opc = ARMISD::FTOSI;
2781 break;
2782 case ISD::FP_TO_UINT:
2783 Opc = ARMISD::FTOUI;
2784 break;
2785 }
2786 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002787 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002788}
2789
2790static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2791 EVT VT = Op.getValueType();
2792 DebugLoc dl = Op.getDebugLoc();
2793 unsigned Opc;
2794
2795 switch (Op.getOpcode()) {
2796 default:
2797 assert(0 && "Invalid opcode!");
2798 case ISD::SINT_TO_FP:
2799 Opc = ARMISD::SITOF;
2800 break;
2801 case ISD::UINT_TO_FP:
2802 Opc = ARMISD::UITOF;
2803 break;
2804 }
2805
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002806 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002807 return DAG.getNode(Opc, dl, VT, Op);
2808}
2809
Evan Cheng515fe3a2010-07-08 02:08:50 +00002810SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002811 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002812 SDValue Tmp0 = Op.getOperand(0);
2813 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002814 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002815 EVT VT = Op.getValueType();
2816 EVT SrcVT = Tmp1.getValueType();
Dale Johannesende064702009-02-06 21:50:26 +00002817 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
Evan Cheng218977b2010-07-13 19:27:42 +00002818 SDValue ARMcc = DAG.getConstant(ARMCC::LT, MVT::i32);
Evan Cheng515fe3a2010-07-08 02:08:50 +00002819 SDValue FP0 = DAG.getConstantFP(0.0, SrcVT);
Evan Cheng218977b2010-07-13 19:27:42 +00002820 SDValue Cmp = getVFPCmp(Tmp1, FP0, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002821 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002822 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002823}
2824
Evan Cheng2457f2c2010-05-22 01:47:14 +00002825SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2826 MachineFunction &MF = DAG.getMachineFunction();
2827 MachineFrameInfo *MFI = MF.getFrameInfo();
2828 MFI->setReturnAddressIsTaken(true);
2829
2830 EVT VT = Op.getValueType();
2831 DebugLoc dl = Op.getDebugLoc();
2832 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2833 if (Depth) {
2834 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2835 SDValue Offset = DAG.getConstant(4, MVT::i32);
2836 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2837 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002838 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002839 }
2840
2841 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patele9a7ea62011-01-31 21:38:14 +00002842 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32), dl);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002843 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
2844}
2845
Dan Gohmand858e902010-04-17 15:26:15 +00002846SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00002847 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2848 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00002849
Owen Andersone50ed302009-08-10 22:56:29 +00002850 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002851 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2852 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00002853 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00002854 ? ARM::R7 : ARM::R11;
2855 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2856 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002857 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
2858 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00002859 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00002860 return FrameAddr;
2861}
2862
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002863/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00002864/// expand a bit convert where either the source or destination type is i64 to
2865/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2866/// operand type is illegal (e.g., v2f32 for a target that doesn't support
2867/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002868static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00002869 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2870 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002871 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00002872
Bob Wilson9f3f0612010-04-17 05:30:19 +00002873 // This function is only supposed to be called for i64 types, either as the
2874 // source or destination of the bit convert.
2875 EVT SrcVT = Op.getValueType();
2876 EVT DstVT = N->getValueType(0);
2877 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002878 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00002879
Bob Wilson9f3f0612010-04-17 05:30:19 +00002880 // Turn i64->f64 into VMOVDRR.
2881 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002882 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2883 DAG.getConstant(0, MVT::i32));
2884 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2885 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002886 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00002887 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00002888 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002889
Jim Grosbache5165492009-11-09 00:11:35 +00002890 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00002891 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2892 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2893 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2894 // Merge the pieces into a single i64 value.
2895 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2896 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00002897
Bob Wilson9f3f0612010-04-17 05:30:19 +00002898 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00002899}
2900
Bob Wilson5bafff32009-06-22 23:27:02 +00002901/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00002902/// Zero vectors are used to represent vector negation and in those cases
2903/// will be implemented with the NEON VNEG instruction. However, VNEG does
2904/// not support i64 elements, so sometimes the zero vectors will need to be
2905/// explicitly constructed. Regardless, use a canonical VMOV to create the
2906/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002907static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002908 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00002909 // The canonical modified immediate encoding of a zero vector is....0!
2910 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
2911 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
2912 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002913 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00002914}
2915
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002916/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2917/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002918SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2919 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002920 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2921 EVT VT = Op.getValueType();
2922 unsigned VTBits = VT.getSizeInBits();
2923 DebugLoc dl = Op.getDebugLoc();
2924 SDValue ShOpLo = Op.getOperand(0);
2925 SDValue ShOpHi = Op.getOperand(1);
2926 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002927 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002928 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002929
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002930 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2931
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002932 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2933 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2934 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2935 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2936 DAG.getConstant(VTBits, MVT::i32));
2937 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2938 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002939 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002940
2941 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2942 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002943 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00002944 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002945 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00002946 CCR, Cmp);
2947
2948 SDValue Ops[2] = { Lo, Hi };
2949 return DAG.getMergeValues(Ops, 2, dl);
2950}
2951
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002952/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2953/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00002954SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2955 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002956 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2957 EVT VT = Op.getValueType();
2958 unsigned VTBits = VT.getSizeInBits();
2959 DebugLoc dl = Op.getDebugLoc();
2960 SDValue ShOpLo = Op.getOperand(0);
2961 SDValue ShOpHi = Op.getOperand(1);
2962 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00002963 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002964
2965 assert(Op.getOpcode() == ISD::SHL_PARTS);
2966 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2967 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2968 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2969 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2970 DAG.getConstant(VTBits, MVT::i32));
2971 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2972 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2973
2974 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2975 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2976 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00002977 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002978 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00002979 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00002980 CCR, Cmp);
2981
2982 SDValue Ops[2] = { Lo, Hi };
2983 return DAG.getMergeValues(Ops, 2, dl);
2984}
2985
Jim Grosbach4725ca72010-09-08 03:54:02 +00002986SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00002987 SelectionDAG &DAG) const {
2988 // The rounding mode is in bits 23:22 of the FPSCR.
2989 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
2990 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
2991 // so that the shift + and get folded into a bitfield extract.
2992 DebugLoc dl = Op.getDebugLoc();
2993 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
2994 DAG.getConstant(Intrinsic::arm_get_fpscr,
2995 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00002996 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00002997 DAG.getConstant(1U << 22, MVT::i32));
2998 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
2999 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003000 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003001 DAG.getConstant(3, MVT::i32));
3002}
3003
Jim Grosbach3482c802010-01-18 19:58:49 +00003004static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3005 const ARMSubtarget *ST) {
3006 EVT VT = N->getValueType(0);
3007 DebugLoc dl = N->getDebugLoc();
3008
3009 if (!ST->hasV6T2Ops())
3010 return SDValue();
3011
3012 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3013 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3014}
3015
Bob Wilson5bafff32009-06-22 23:27:02 +00003016static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3017 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003018 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003019 DebugLoc dl = N->getDebugLoc();
3020
Bob Wilsond5448bb2010-11-18 21:16:28 +00003021 if (!VT.isVector())
3022 return SDValue();
3023
Bob Wilson5bafff32009-06-22 23:27:02 +00003024 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003025 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003026
Bob Wilsond5448bb2010-11-18 21:16:28 +00003027 // Left shifts translate directly to the vshiftu intrinsic.
3028 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003029 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003030 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3031 N->getOperand(0), N->getOperand(1));
3032
3033 assert((N->getOpcode() == ISD::SRA ||
3034 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3035
3036 // NEON uses the same intrinsics for both left and right shifts. For
3037 // right shifts, the shift amounts are negative, so negate the vector of
3038 // shift amounts.
3039 EVT ShiftVT = N->getOperand(1).getValueType();
3040 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3041 getZeroVector(ShiftVT, DAG, dl),
3042 N->getOperand(1));
3043 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3044 Intrinsic::arm_neon_vshifts :
3045 Intrinsic::arm_neon_vshiftu);
3046 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3047 DAG.getConstant(vshiftInt, MVT::i32),
3048 N->getOperand(0), NegatedCount);
3049}
3050
3051static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3052 const ARMSubtarget *ST) {
3053 EVT VT = N->getValueType(0);
3054 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003055
Eli Friedmance392eb2009-08-22 03:13:10 +00003056 // We can get here for a node like i32 = ISD::SHL i32, i64
3057 if (VT != MVT::i64)
3058 return SDValue();
3059
3060 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003061 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003062
Chris Lattner27a6c732007-11-24 07:07:01 +00003063 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3064 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003065 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003066 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003067
Chris Lattner27a6c732007-11-24 07:07:01 +00003068 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003069 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003070
Chris Lattner27a6c732007-11-24 07:07:01 +00003071 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003072 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003073 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003074 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003075 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003076
Chris Lattner27a6c732007-11-24 07:07:01 +00003077 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3078 // captures the result into a carry flag.
3079 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003080 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003081
Chris Lattner27a6c732007-11-24 07:07:01 +00003082 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003083 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003084
Chris Lattner27a6c732007-11-24 07:07:01 +00003085 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003086 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003087}
3088
Bob Wilson5bafff32009-06-22 23:27:02 +00003089static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3090 SDValue TmpOp0, TmpOp1;
3091 bool Invert = false;
3092 bool Swap = false;
3093 unsigned Opc = 0;
3094
3095 SDValue Op0 = Op.getOperand(0);
3096 SDValue Op1 = Op.getOperand(1);
3097 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003098 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003099 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3100 DebugLoc dl = Op.getDebugLoc();
3101
3102 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3103 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003104 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003105 case ISD::SETUNE:
3106 case ISD::SETNE: Invert = true; // Fallthrough
3107 case ISD::SETOEQ:
3108 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3109 case ISD::SETOLT:
3110 case ISD::SETLT: Swap = true; // Fallthrough
3111 case ISD::SETOGT:
3112 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3113 case ISD::SETOLE:
3114 case ISD::SETLE: Swap = true; // Fallthrough
3115 case ISD::SETOGE:
3116 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3117 case ISD::SETUGE: Swap = true; // Fallthrough
3118 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3119 case ISD::SETUGT: Swap = true; // Fallthrough
3120 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3121 case ISD::SETUEQ: Invert = true; // Fallthrough
3122 case ISD::SETONE:
3123 // Expand this to (OLT | OGT).
3124 TmpOp0 = Op0;
3125 TmpOp1 = Op1;
3126 Opc = ISD::OR;
3127 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3128 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3129 break;
3130 case ISD::SETUO: Invert = true; // Fallthrough
3131 case ISD::SETO:
3132 // Expand this to (OLT | OGE).
3133 TmpOp0 = Op0;
3134 TmpOp1 = Op1;
3135 Opc = ISD::OR;
3136 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3137 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3138 break;
3139 }
3140 } else {
3141 // Integer comparisons.
3142 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003143 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003144 case ISD::SETNE: Invert = true;
3145 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3146 case ISD::SETLT: Swap = true;
3147 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3148 case ISD::SETLE: Swap = true;
3149 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3150 case ISD::SETULT: Swap = true;
3151 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3152 case ISD::SETULE: Swap = true;
3153 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3154 }
3155
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003156 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003157 if (Opc == ARMISD::VCEQ) {
3158
3159 SDValue AndOp;
3160 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3161 AndOp = Op0;
3162 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3163 AndOp = Op1;
3164
3165 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003166 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003167 AndOp = AndOp.getOperand(0);
3168
3169 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3170 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003171 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3172 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003173 Invert = !Invert;
3174 }
3175 }
3176 }
3177
3178 if (Swap)
3179 std::swap(Op0, Op1);
3180
Owen Andersonc24cb352010-11-08 23:21:22 +00003181 // If one of the operands is a constant vector zero, attempt to fold the
3182 // comparison to a specialized compare-against-zero form.
3183 SDValue SingleOp;
3184 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3185 SingleOp = Op0;
3186 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3187 if (Opc == ARMISD::VCGE)
3188 Opc = ARMISD::VCLEZ;
3189 else if (Opc == ARMISD::VCGT)
3190 Opc = ARMISD::VCLTZ;
3191 SingleOp = Op1;
3192 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003193
Owen Andersonc24cb352010-11-08 23:21:22 +00003194 SDValue Result;
3195 if (SingleOp.getNode()) {
3196 switch (Opc) {
3197 case ARMISD::VCEQ:
3198 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3199 case ARMISD::VCGE:
3200 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3201 case ARMISD::VCLEZ:
3202 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3203 case ARMISD::VCGT:
3204 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3205 case ARMISD::VCLTZ:
3206 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3207 default:
3208 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3209 }
3210 } else {
3211 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3212 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003213
3214 if (Invert)
3215 Result = DAG.getNOT(dl, Result, VT);
3216
3217 return Result;
3218}
3219
Bob Wilsond3c42842010-06-14 22:19:57 +00003220/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3221/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003222/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003223static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3224 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003225 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003226 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003227
Bob Wilson827b2102010-06-15 19:05:35 +00003228 // SplatBitSize is set to the smallest size that splats the vector, so a
3229 // zero vector will always have SplatBitSize == 8. However, NEON modified
3230 // immediate instructions others than VMOV do not support the 8-bit encoding
3231 // of a zero vector, and the default encoding of zero is supposed to be the
3232 // 32-bit version.
3233 if (SplatBits == 0)
3234 SplatBitSize = 32;
3235
Bob Wilson5bafff32009-06-22 23:27:02 +00003236 switch (SplatBitSize) {
3237 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003238 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003239 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003240 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003241 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003242 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003243 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003244 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003245 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003246
3247 case 16:
3248 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003249 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003250 if ((SplatBits & ~0xff) == 0) {
3251 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003252 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003253 Imm = SplatBits;
3254 break;
3255 }
3256 if ((SplatBits & ~0xff00) == 0) {
3257 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003258 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003259 Imm = SplatBits >> 8;
3260 break;
3261 }
3262 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003263
3264 case 32:
3265 // NEON's 32-bit VMOV supports splat values where:
3266 // * only one byte is nonzero, or
3267 // * the least significant byte is 0xff and the second byte is nonzero, or
3268 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003269 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003270 if ((SplatBits & ~0xff) == 0) {
3271 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003272 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003273 Imm = SplatBits;
3274 break;
3275 }
3276 if ((SplatBits & ~0xff00) == 0) {
3277 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003278 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003279 Imm = SplatBits >> 8;
3280 break;
3281 }
3282 if ((SplatBits & ~0xff0000) == 0) {
3283 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003284 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003285 Imm = SplatBits >> 16;
3286 break;
3287 }
3288 if ((SplatBits & ~0xff000000) == 0) {
3289 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003290 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003291 Imm = SplatBits >> 24;
3292 break;
3293 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003294
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003295 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3296 if (type == OtherModImm) return SDValue();
3297
Bob Wilson5bafff32009-06-22 23:27:02 +00003298 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003299 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3300 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003301 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003302 Imm = SplatBits >> 8;
3303 SplatBits |= 0xff;
3304 break;
3305 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003306
3307 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003308 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3309 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003310 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003311 Imm = SplatBits >> 16;
3312 SplatBits |= 0xffff;
3313 break;
3314 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003315
3316 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3317 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3318 // VMOV.I32. A (very) minor optimization would be to replicate the value
3319 // and fall through here to test for a valid 64-bit splat. But, then the
3320 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003321 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003322
3323 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003324 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003325 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003326 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003327 uint64_t BitMask = 0xff;
3328 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003329 unsigned ImmMask = 1;
3330 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003331 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003332 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003333 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003334 Imm |= ImmMask;
3335 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003336 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003337 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003338 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003339 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003340 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003341 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003342 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003343 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003344 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003345 break;
3346 }
3347
Bob Wilson1a913ed2010-06-11 21:34:50 +00003348 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003349 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003350 return SDValue();
3351 }
3352
Bob Wilsoncba270d2010-07-13 21:16:48 +00003353 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3354 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003355}
3356
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003357static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3358 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003359 unsigned NumElts = VT.getVectorNumElements();
3360 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003361
3362 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3363 if (M[0] < 0)
3364 return false;
3365
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003366 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003367
3368 // If this is a VEXT shuffle, the immediate value is the index of the first
3369 // element. The other shuffle indices must be the successive elements after
3370 // the first one.
3371 unsigned ExpectedElt = Imm;
3372 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003373 // Increment the expected index. If it wraps around, it may still be
3374 // a VEXT but the source vectors must be swapped.
3375 ExpectedElt += 1;
3376 if (ExpectedElt == NumElts * 2) {
3377 ExpectedElt = 0;
3378 ReverseVEXT = true;
3379 }
3380
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003381 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003382 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003383 return false;
3384 }
3385
3386 // Adjust the index value if the source operands will be swapped.
3387 if (ReverseVEXT)
3388 Imm -= NumElts;
3389
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003390 return true;
3391}
3392
Bob Wilson8bb9e482009-07-26 00:39:34 +00003393/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3394/// instruction with the specified blocksize. (The order of the elements
3395/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003396static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3397 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003398 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3399 "Only possible block sizes for VREV are: 16, 32, 64");
3400
Bob Wilson8bb9e482009-07-26 00:39:34 +00003401 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003402 if (EltSz == 64)
3403 return false;
3404
3405 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003406 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003407 // If the first shuffle index is UNDEF, be optimistic.
3408 if (M[0] < 0)
3409 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003410
3411 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3412 return false;
3413
3414 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003415 if (M[i] < 0) continue; // ignore UNDEF indices
3416 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003417 return false;
3418 }
3419
3420 return true;
3421}
3422
Bob Wilsonc692cb72009-08-21 20:54:19 +00003423static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3424 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003425 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3426 if (EltSz == 64)
3427 return false;
3428
Bob Wilsonc692cb72009-08-21 20:54:19 +00003429 unsigned NumElts = VT.getVectorNumElements();
3430 WhichResult = (M[0] == 0 ? 0 : 1);
3431 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003432 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3433 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003434 return false;
3435 }
3436 return true;
3437}
3438
Bob Wilson324f4f12009-12-03 06:40:55 +00003439/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3440/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3441/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3442static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3443 unsigned &WhichResult) {
3444 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3445 if (EltSz == 64)
3446 return false;
3447
3448 unsigned NumElts = VT.getVectorNumElements();
3449 WhichResult = (M[0] == 0 ? 0 : 1);
3450 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003451 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3452 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003453 return false;
3454 }
3455 return true;
3456}
3457
Bob Wilsonc692cb72009-08-21 20:54:19 +00003458static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3459 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003460 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3461 if (EltSz == 64)
3462 return false;
3463
Bob Wilsonc692cb72009-08-21 20:54:19 +00003464 unsigned NumElts = VT.getVectorNumElements();
3465 WhichResult = (M[0] == 0 ? 0 : 1);
3466 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003467 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003468 if ((unsigned) M[i] != 2 * i + WhichResult)
3469 return false;
3470 }
3471
3472 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003473 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003474 return false;
3475
3476 return true;
3477}
3478
Bob Wilson324f4f12009-12-03 06:40:55 +00003479/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3480/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3481/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3482static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3483 unsigned &WhichResult) {
3484 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3485 if (EltSz == 64)
3486 return false;
3487
3488 unsigned Half = VT.getVectorNumElements() / 2;
3489 WhichResult = (M[0] == 0 ? 0 : 1);
3490 for (unsigned j = 0; j != 2; ++j) {
3491 unsigned Idx = WhichResult;
3492 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003493 int MIdx = M[i + j * Half];
3494 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003495 return false;
3496 Idx += 2;
3497 }
3498 }
3499
3500 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3501 if (VT.is64BitVector() && EltSz == 32)
3502 return false;
3503
3504 return true;
3505}
3506
Bob Wilsonc692cb72009-08-21 20:54:19 +00003507static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3508 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003509 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3510 if (EltSz == 64)
3511 return false;
3512
Bob Wilsonc692cb72009-08-21 20:54:19 +00003513 unsigned NumElts = VT.getVectorNumElements();
3514 WhichResult = (M[0] == 0 ? 0 : 1);
3515 unsigned Idx = WhichResult * NumElts / 2;
3516 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003517 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3518 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003519 return false;
3520 Idx += 1;
3521 }
3522
3523 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003524 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003525 return false;
3526
3527 return true;
3528}
3529
Bob Wilson324f4f12009-12-03 06:40:55 +00003530/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3531/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3532/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3533static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3534 unsigned &WhichResult) {
3535 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3536 if (EltSz == 64)
3537 return false;
3538
3539 unsigned NumElts = VT.getVectorNumElements();
3540 WhichResult = (M[0] == 0 ? 0 : 1);
3541 unsigned Idx = WhichResult * NumElts / 2;
3542 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003543 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3544 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003545 return false;
3546 Idx += 1;
3547 }
3548
3549 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3550 if (VT.is64BitVector() && EltSz == 32)
3551 return false;
3552
3553 return true;
3554}
3555
Dale Johannesenf630c712010-07-29 20:10:08 +00003556// If N is an integer constant that can be moved into a register in one
3557// instruction, return an SDValue of such a constant (will become a MOV
3558// instruction). Otherwise return null.
3559static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3560 const ARMSubtarget *ST, DebugLoc dl) {
3561 uint64_t Val;
3562 if (!isa<ConstantSDNode>(N))
3563 return SDValue();
3564 Val = cast<ConstantSDNode>(N)->getZExtValue();
3565
3566 if (ST->isThumb1Only()) {
3567 if (Val <= 255 || ~Val <= 255)
3568 return DAG.getConstant(Val, MVT::i32);
3569 } else {
3570 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3571 return DAG.getConstant(Val, MVT::i32);
3572 }
3573 return SDValue();
3574}
3575
Bob Wilson5bafff32009-06-22 23:27:02 +00003576// If this is a case we can't handle, return null and let the default
3577// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003578SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3579 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003580 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003581 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003582 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003583
3584 APInt SplatBits, SplatUndef;
3585 unsigned SplatBitSize;
3586 bool HasAnyUndefs;
3587 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003588 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003589 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003590 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003591 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003592 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003593 DAG, VmovVT, VT.is128BitVector(),
3594 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003595 if (Val.getNode()) {
3596 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003597 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003598 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003599
3600 // Try an immediate VMVN.
3601 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3602 ((1LL << SplatBitSize) - 1));
3603 Val = isNEONModifiedImm(NegatedImm,
3604 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003605 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003606 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003607 if (Val.getNode()) {
3608 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003609 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003610 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003611 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003612 }
3613
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003614 // Scan through the operands to see if only one value is used.
3615 unsigned NumElts = VT.getVectorNumElements();
3616 bool isOnlyLowElement = true;
3617 bool usesOnlyOneValue = true;
3618 bool isConstant = true;
3619 SDValue Value;
3620 for (unsigned i = 0; i < NumElts; ++i) {
3621 SDValue V = Op.getOperand(i);
3622 if (V.getOpcode() == ISD::UNDEF)
3623 continue;
3624 if (i > 0)
3625 isOnlyLowElement = false;
3626 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3627 isConstant = false;
3628
3629 if (!Value.getNode())
3630 Value = V;
3631 else if (V != Value)
3632 usesOnlyOneValue = false;
3633 }
3634
3635 if (!Value.getNode())
3636 return DAG.getUNDEF(VT);
3637
3638 if (isOnlyLowElement)
3639 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3640
Dale Johannesenf630c712010-07-29 20:10:08 +00003641 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3642
Dale Johannesen575cd142010-10-19 20:00:17 +00003643 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3644 // i32 and try again.
3645 if (usesOnlyOneValue && EltSize <= 32) {
3646 if (!isConstant)
3647 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3648 if (VT.getVectorElementType().isFloatingPoint()) {
3649 SmallVector<SDValue, 8> Ops;
3650 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003651 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003652 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003653 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3654 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003655 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3656 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003657 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003658 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003659 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3660 if (Val.getNode())
3661 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003662 }
3663
3664 // If all elements are constants and the case above didn't get hit, fall back
3665 // to the default expansion, which will generate a load from the constant
3666 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003667 if (isConstant)
3668 return SDValue();
3669
Bob Wilson11a1dff2011-01-07 21:37:30 +00003670 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3671 if (NumElts >= 4) {
3672 SDValue shuffle = ReconstructShuffle(Op, DAG);
3673 if (shuffle != SDValue())
3674 return shuffle;
3675 }
3676
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003677 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003678 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3679 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003680 if (EltSize >= 32) {
3681 // Do the expansion with floating-point types, since that is what the VFP
3682 // registers are defined to use, and since i64 is not legal.
3683 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3684 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003685 SmallVector<SDValue, 8> Ops;
3686 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003687 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003688 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003689 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003690 }
3691
3692 return SDValue();
3693}
3694
Bob Wilson11a1dff2011-01-07 21:37:30 +00003695// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003696// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003697SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3698 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003699 DebugLoc dl = Op.getDebugLoc();
3700 EVT VT = Op.getValueType();
3701 unsigned NumElts = VT.getVectorNumElements();
3702
3703 SmallVector<SDValue, 2> SourceVecs;
3704 SmallVector<unsigned, 2> MinElts;
3705 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003706
Bob Wilson11a1dff2011-01-07 21:37:30 +00003707 for (unsigned i = 0; i < NumElts; ++i) {
3708 SDValue V = Op.getOperand(i);
3709 if (V.getOpcode() == ISD::UNDEF)
3710 continue;
3711 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3712 // A shuffle can only come from building a vector from various
3713 // elements of other vectors.
3714 return SDValue();
3715 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003716
Bob Wilson11a1dff2011-01-07 21:37:30 +00003717 // Record this extraction against the appropriate vector if possible...
3718 SDValue SourceVec = V.getOperand(0);
3719 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3720 bool FoundSource = false;
3721 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3722 if (SourceVecs[j] == SourceVec) {
3723 if (MinElts[j] > EltNo)
3724 MinElts[j] = EltNo;
3725 if (MaxElts[j] < EltNo)
3726 MaxElts[j] = EltNo;
3727 FoundSource = true;
3728 break;
3729 }
3730 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003731
Bob Wilson11a1dff2011-01-07 21:37:30 +00003732 // Or record a new source if not...
3733 if (!FoundSource) {
3734 SourceVecs.push_back(SourceVec);
3735 MinElts.push_back(EltNo);
3736 MaxElts.push_back(EltNo);
3737 }
3738 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003739
Bob Wilson11a1dff2011-01-07 21:37:30 +00003740 // Currently only do something sane when at most two source vectors
3741 // involved.
3742 if (SourceVecs.size() > 2)
3743 return SDValue();
3744
3745 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3746 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003747
Bob Wilson11a1dff2011-01-07 21:37:30 +00003748 // This loop extracts the usage patterns of the source vectors
3749 // and prepares appropriate SDValues for a shuffle if possible.
3750 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3751 if (SourceVecs[i].getValueType() == VT) {
3752 // No VEXT necessary
3753 ShuffleSrcs[i] = SourceVecs[i];
3754 VEXTOffsets[i] = 0;
3755 continue;
3756 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3757 // It probably isn't worth padding out a smaller vector just to
3758 // break it down again in a shuffle.
3759 return SDValue();
3760 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003761
Bob Wilson11a1dff2011-01-07 21:37:30 +00003762 // Since only 64-bit and 128-bit vectors are legal on ARM and
3763 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003764 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3765 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003766
Bob Wilson11a1dff2011-01-07 21:37:30 +00003767 if (MaxElts[i] - MinElts[i] >= NumElts) {
3768 // Span too large for a VEXT to cope
3769 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003770 }
3771
Bob Wilson11a1dff2011-01-07 21:37:30 +00003772 if (MinElts[i] >= NumElts) {
3773 // The extraction can just take the second half
3774 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003775 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3776 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003777 DAG.getIntPtrConstant(NumElts));
3778 } else if (MaxElts[i] < NumElts) {
3779 // The extraction can just take the first half
3780 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003781 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3782 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003783 DAG.getIntPtrConstant(0));
3784 } else {
3785 // An actual VEXT is needed
3786 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003787 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3788 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003789 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003790 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3791 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003792 DAG.getIntPtrConstant(NumElts));
3793 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3794 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3795 }
3796 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003797
Bob Wilson11a1dff2011-01-07 21:37:30 +00003798 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003799
Bob Wilson11a1dff2011-01-07 21:37:30 +00003800 for (unsigned i = 0; i < NumElts; ++i) {
3801 SDValue Entry = Op.getOperand(i);
3802 if (Entry.getOpcode() == ISD::UNDEF) {
3803 Mask.push_back(-1);
3804 continue;
3805 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003806
Bob Wilson11a1dff2011-01-07 21:37:30 +00003807 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003808 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3809 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003810 if (ExtractVec == SourceVecs[0]) {
3811 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3812 } else {
3813 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3814 }
3815 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003816
Bob Wilson11a1dff2011-01-07 21:37:30 +00003817 // Final check before we try to produce nonsense...
3818 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00003819 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
3820 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003821
Bob Wilson11a1dff2011-01-07 21:37:30 +00003822 return SDValue();
3823}
3824
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003825/// isShuffleMaskLegal - Targets can use this to indicate that they only
3826/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3827/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3828/// are assumed to be legal.
3829bool
3830ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
3831 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003832 if (VT.getVectorNumElements() == 4 &&
3833 (VT.is128BitVector() || VT.is64BitVector())) {
3834 unsigned PFIndexes[4];
3835 for (unsigned i = 0; i != 4; ++i) {
3836 if (M[i] < 0)
3837 PFIndexes[i] = 8;
3838 else
3839 PFIndexes[i] = M[i];
3840 }
3841
3842 // Compute the index in the perfect shuffle table.
3843 unsigned PFTableIndex =
3844 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3845 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3846 unsigned Cost = (PFEntry >> 30);
3847
3848 if (Cost <= 4)
3849 return true;
3850 }
3851
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003852 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00003853 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003854
Bob Wilson53dd2452010-06-07 23:53:38 +00003855 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3856 return (EltSize >= 32 ||
3857 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003858 isVREVMask(M, VT, 64) ||
3859 isVREVMask(M, VT, 32) ||
3860 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00003861 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
3862 isVTRNMask(M, VT, WhichResult) ||
3863 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00003864 isVZIPMask(M, VT, WhichResult) ||
3865 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
3866 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
3867 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003868}
3869
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003870/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3871/// the specified operations to build the shuffle.
3872static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
3873 SDValue RHS, SelectionDAG &DAG,
3874 DebugLoc dl) {
3875 unsigned OpNum = (PFEntry >> 26) & 0x0F;
3876 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
3877 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
3878
3879 enum {
3880 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
3881 OP_VREV,
3882 OP_VDUP0,
3883 OP_VDUP1,
3884 OP_VDUP2,
3885 OP_VDUP3,
3886 OP_VEXT1,
3887 OP_VEXT2,
3888 OP_VEXT3,
3889 OP_VUZPL, // VUZP, left result
3890 OP_VUZPR, // VUZP, right result
3891 OP_VZIPL, // VZIP, left result
3892 OP_VZIPR, // VZIP, right result
3893 OP_VTRNL, // VTRN, left result
3894 OP_VTRNR // VTRN, right result
3895 };
3896
3897 if (OpNum == OP_COPY) {
3898 if (LHSID == (1*9+2)*9+3) return LHS;
3899 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3900 return RHS;
3901 }
3902
3903 SDValue OpLHS, OpRHS;
3904 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3905 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
3906 EVT VT = OpLHS.getValueType();
3907
3908 switch (OpNum) {
3909 default: llvm_unreachable("Unknown shuffle opcode!");
3910 case OP_VREV:
3911 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
3912 case OP_VDUP0:
3913 case OP_VDUP1:
3914 case OP_VDUP2:
3915 case OP_VDUP3:
3916 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003917 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003918 case OP_VEXT1:
3919 case OP_VEXT2:
3920 case OP_VEXT3:
3921 return DAG.getNode(ARMISD::VEXT, dl, VT,
3922 OpLHS, OpRHS,
3923 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
3924 case OP_VUZPL:
3925 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003926 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003927 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
3928 case OP_VZIPL:
3929 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003930 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003931 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
3932 case OP_VTRNL:
3933 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00003934 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3935 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003936 }
3937}
3938
Bob Wilson5bafff32009-06-22 23:27:02 +00003939static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003940 SDValue V1 = Op.getOperand(0);
3941 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00003942 DebugLoc dl = Op.getDebugLoc();
3943 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00003944 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003945 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00003946
Bob Wilson28865062009-08-13 02:13:04 +00003947 // Convert shuffles that are directly supported on NEON to target-specific
3948 // DAG nodes, instead of keeping them as shuffles and matching them again
3949 // during code selection. This is more efficient and avoids the possibility
3950 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00003951 // FIXME: floating-point vectors should be canonicalized to integer vectors
3952 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003953 SVN->getMask(ShuffleMask);
3954
Bob Wilson53dd2452010-06-07 23:53:38 +00003955 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3956 if (EltSize <= 32) {
3957 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
3958 int Lane = SVN->getSplatIndex();
3959 // If this is undef splat, generate it via "just" vdup, if possible.
3960 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00003961
Bob Wilson53dd2452010-06-07 23:53:38 +00003962 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3963 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
3964 }
3965 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
3966 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00003967 }
Bob Wilson53dd2452010-06-07 23:53:38 +00003968
3969 bool ReverseVEXT;
3970 unsigned Imm;
3971 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
3972 if (ReverseVEXT)
3973 std::swap(V1, V2);
3974 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
3975 DAG.getConstant(Imm, MVT::i32));
3976 }
3977
3978 if (isVREVMask(ShuffleMask, VT, 64))
3979 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
3980 if (isVREVMask(ShuffleMask, VT, 32))
3981 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
3982 if (isVREVMask(ShuffleMask, VT, 16))
3983 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
3984
3985 // Check for Neon shuffles that modify both input vectors in place.
3986 // If both results are used, i.e., if there are two shuffles with the same
3987 // source operands and with masks corresponding to both results of one of
3988 // these operations, DAG memoization will ensure that a single node is
3989 // used for both shuffles.
3990 unsigned WhichResult;
3991 if (isVTRNMask(ShuffleMask, VT, WhichResult))
3992 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3993 V1, V2).getValue(WhichResult);
3994 if (isVUZPMask(ShuffleMask, VT, WhichResult))
3995 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3996 V1, V2).getValue(WhichResult);
3997 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3998 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3999 V1, V2).getValue(WhichResult);
4000
4001 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4002 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4003 V1, V1).getValue(WhichResult);
4004 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4005 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4006 V1, V1).getValue(WhichResult);
4007 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4008 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4009 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004010 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004011
Bob Wilsonc692cb72009-08-21 20:54:19 +00004012 // If the shuffle is not directly supported and it has 4 elements, use
4013 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004014 unsigned NumElts = VT.getVectorNumElements();
4015 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004016 unsigned PFIndexes[4];
4017 for (unsigned i = 0; i != 4; ++i) {
4018 if (ShuffleMask[i] < 0)
4019 PFIndexes[i] = 8;
4020 else
4021 PFIndexes[i] = ShuffleMask[i];
4022 }
4023
4024 // Compute the index in the perfect shuffle table.
4025 unsigned PFTableIndex =
4026 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004027 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4028 unsigned Cost = (PFEntry >> 30);
4029
4030 if (Cost <= 4)
4031 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4032 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004033
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004034 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004035 if (EltSize >= 32) {
4036 // Do the expansion with floating-point types, since that is what the VFP
4037 // registers are defined to use, and since i64 is not legal.
4038 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4039 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004040 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4041 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004042 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004043 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004044 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004045 Ops.push_back(DAG.getUNDEF(EltVT));
4046 else
4047 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4048 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4049 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4050 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004051 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004052 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004053 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004054 }
4055
Bob Wilson22cac0d2009-08-14 05:16:33 +00004056 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004057}
4058
Bob Wilson5bafff32009-06-22 23:27:02 +00004059static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004060 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004061 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004062 if (!isa<ConstantSDNode>(Lane))
4063 return SDValue();
4064
4065 SDValue Vec = Op.getOperand(0);
4066 if (Op.getValueType() == MVT::i32 &&
4067 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4068 DebugLoc dl = Op.getDebugLoc();
4069 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4070 }
4071
4072 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004073}
4074
Bob Wilsona6d65862009-08-03 20:36:38 +00004075static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4076 // The only time a CONCAT_VECTORS operation can have legal types is when
4077 // two 64-bit vectors are concatenated to a 128-bit vector.
4078 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4079 "unexpected CONCAT_VECTORS");
4080 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004081 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004082 SDValue Op0 = Op.getOperand(0);
4083 SDValue Op1 = Op.getOperand(1);
4084 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004086 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004087 DAG.getIntPtrConstant(0));
4088 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004089 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004090 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004091 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004092 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004093}
4094
Bob Wilson626613d2010-11-23 19:38:38 +00004095/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4096/// element has been zero/sign-extended, depending on the isSigned parameter,
4097/// from an integer type half its size.
4098static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4099 bool isSigned) {
4100 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4101 EVT VT = N->getValueType(0);
4102 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4103 SDNode *BVN = N->getOperand(0).getNode();
4104 if (BVN->getValueType(0) != MVT::v4i32 ||
4105 BVN->getOpcode() != ISD::BUILD_VECTOR)
4106 return false;
4107 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4108 unsigned HiElt = 1 - LoElt;
4109 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4110 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4111 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4112 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4113 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4114 return false;
4115 if (isSigned) {
4116 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4117 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4118 return true;
4119 } else {
4120 if (Hi0->isNullValue() && Hi1->isNullValue())
4121 return true;
4122 }
4123 return false;
4124 }
4125
4126 if (N->getOpcode() != ISD::BUILD_VECTOR)
4127 return false;
4128
4129 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4130 SDNode *Elt = N->getOperand(i).getNode();
4131 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4132 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4133 unsigned HalfSize = EltSize / 2;
4134 if (isSigned) {
4135 int64_t SExtVal = C->getSExtValue();
4136 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4137 return false;
4138 } else {
4139 if ((C->getZExtValue() >> HalfSize) != 0)
4140 return false;
4141 }
4142 continue;
4143 }
4144 return false;
4145 }
4146
4147 return true;
4148}
4149
4150/// isSignExtended - Check if a node is a vector value that is sign-extended
4151/// or a constant BUILD_VECTOR with sign-extended elements.
4152static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4153 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4154 return true;
4155 if (isExtendedBUILD_VECTOR(N, DAG, true))
4156 return true;
4157 return false;
4158}
4159
4160/// isZeroExtended - Check if a node is a vector value that is zero-extended
4161/// or a constant BUILD_VECTOR with zero-extended elements.
4162static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4163 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4164 return true;
4165 if (isExtendedBUILD_VECTOR(N, DAG, false))
4166 return true;
4167 return false;
4168}
4169
4170/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4171/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004172static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4173 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4174 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004175 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4176 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4177 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4178 LD->isNonTemporal(), LD->getAlignment());
4179 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4180 // have been legalized as a BITCAST from v4i32.
4181 if (N->getOpcode() == ISD::BITCAST) {
4182 SDNode *BVN = N->getOperand(0).getNode();
4183 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4184 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4185 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4186 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4187 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4188 }
4189 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4190 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4191 EVT VT = N->getValueType(0);
4192 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4193 unsigned NumElts = VT.getVectorNumElements();
4194 MVT TruncVT = MVT::getIntegerVT(EltSize);
4195 SmallVector<SDValue, 8> Ops;
4196 for (unsigned i = 0; i != NumElts; ++i) {
4197 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4198 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004199 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004200 }
4201 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4202 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004203}
4204
4205static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4206 // Multiplications are only custom-lowered for 128-bit vectors so that
4207 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4208 EVT VT = Op.getValueType();
4209 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4210 SDNode *N0 = Op.getOperand(0).getNode();
4211 SDNode *N1 = Op.getOperand(1).getNode();
4212 unsigned NewOpc = 0;
Bob Wilson626613d2010-11-23 19:38:38 +00004213 if (isSignExtended(N0, DAG) && isSignExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004214 NewOpc = ARMISD::VMULLs;
Bob Wilson626613d2010-11-23 19:38:38 +00004215 else if (isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG))
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004216 NewOpc = ARMISD::VMULLu;
Bob Wilson626613d2010-11-23 19:38:38 +00004217 else if (VT == MVT::v2i64)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004218 // Fall through to expand this. It is not legal.
4219 return SDValue();
Bob Wilson626613d2010-11-23 19:38:38 +00004220 else
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004221 // Other vector multiplications are legal.
4222 return Op;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004223
4224 // Legalize to a VMULL instruction.
4225 DebugLoc DL = Op.getDebugLoc();
4226 SDValue Op0 = SkipExtension(N0, DAG);
4227 SDValue Op1 = SkipExtension(N1, DAG);
4228
4229 assert(Op0.getValueType().is64BitVector() &&
4230 Op1.getValueType().is64BitVector() &&
4231 "unexpected types for extended operands to VMULL");
4232 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4233}
4234
Dan Gohmand858e902010-04-17 15:26:15 +00004235SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004236 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004237 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004238 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004239 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004240 case ISD::GlobalAddress:
4241 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4242 LowerGlobalAddressELF(Op, DAG);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00004243 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004244 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004245 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4246 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004247 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004248 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004249 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004250 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004251 case ISD::SINT_TO_FP:
4252 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4253 case ISD::FP_TO_SINT:
4254 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004255 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004256 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004257 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004258 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004259 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004260 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004261 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004262 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4263 Subtarget);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004264 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004265 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004266 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004267 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004268 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004269 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004270 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004271 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004272 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004273 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004274 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004275 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004276 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004277 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004278 case ISD::MUL: return LowerMUL(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004279 }
Dan Gohman475871a2008-07-27 21:46:04 +00004280 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004281}
4282
Duncan Sands1607f052008-12-01 11:39:25 +00004283/// ReplaceNodeResults - Replace the results of node with an illegal result
4284/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004285void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4286 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004287 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004288 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004289 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004290 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004291 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004292 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004293 case ISD::BITCAST:
4294 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004295 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004296 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004297 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004298 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004299 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004300 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004301 if (Res.getNode())
4302 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004303}
Chris Lattner27a6c732007-11-24 07:07:01 +00004304
Evan Chenga8e29892007-01-19 07:51:42 +00004305//===----------------------------------------------------------------------===//
4306// ARM Scheduler Hooks
4307//===----------------------------------------------------------------------===//
4308
4309MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004310ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4311 MachineBasicBlock *BB,
4312 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004313 unsigned dest = MI->getOperand(0).getReg();
4314 unsigned ptr = MI->getOperand(1).getReg();
4315 unsigned oldval = MI->getOperand(2).getReg();
4316 unsigned newval = MI->getOperand(3).getReg();
4317 unsigned scratch = BB->getParent()->getRegInfo()
4318 .createVirtualRegister(ARM::GPRRegisterClass);
4319 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4320 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004321 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004322
4323 unsigned ldrOpc, strOpc;
4324 switch (Size) {
4325 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004326 case 1:
4327 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
4328 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
4329 break;
4330 case 2:
4331 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4332 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4333 break;
4334 case 4:
4335 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4336 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4337 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004338 }
4339
4340 MachineFunction *MF = BB->getParent();
4341 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4342 MachineFunction::iterator It = BB;
4343 ++It; // insert the new blocks after the current block
4344
4345 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4346 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4347 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4348 MF->insert(It, loop1MBB);
4349 MF->insert(It, loop2MBB);
4350 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004351
4352 // Transfer the remainder of BB and its successor edges to exitMBB.
4353 exitMBB->splice(exitMBB->begin(), BB,
4354 llvm::next(MachineBasicBlock::iterator(MI)),
4355 BB->end());
4356 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004357
4358 // thisMBB:
4359 // ...
4360 // fallthrough --> loop1MBB
4361 BB->addSuccessor(loop1MBB);
4362
4363 // loop1MBB:
4364 // ldrex dest, [ptr]
4365 // cmp dest, oldval
4366 // bne exitMBB
4367 BB = loop1MBB;
4368 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004369 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004370 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004371 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4372 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004373 BB->addSuccessor(loop2MBB);
4374 BB->addSuccessor(exitMBB);
4375
4376 // loop2MBB:
4377 // strex scratch, newval, [ptr]
4378 // cmp scratch, #0
4379 // bne loop1MBB
4380 BB = loop2MBB;
4381 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4382 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004383 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004384 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004385 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4386 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004387 BB->addSuccessor(loop1MBB);
4388 BB->addSuccessor(exitMBB);
4389
4390 // exitMBB:
4391 // ...
4392 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004393
Dan Gohman14152b42010-07-06 20:24:04 +00004394 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004395
Jim Grosbach5278eb82009-12-11 01:42:04 +00004396 return BB;
4397}
4398
4399MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004400ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4401 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004402 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4403 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4404
4405 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004406 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004407 MachineFunction::iterator It = BB;
4408 ++It;
4409
4410 unsigned dest = MI->getOperand(0).getReg();
4411 unsigned ptr = MI->getOperand(1).getReg();
4412 unsigned incr = MI->getOperand(2).getReg();
4413 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004414
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004415 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004416 unsigned ldrOpc, strOpc;
4417 switch (Size) {
4418 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004419 case 1:
4420 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004421 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004422 break;
4423 case 2:
4424 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4425 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4426 break;
4427 case 4:
4428 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4429 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4430 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004431 }
4432
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004433 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4434 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4435 MF->insert(It, loopMBB);
4436 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004437
4438 // Transfer the remainder of BB and its successor edges to exitMBB.
4439 exitMBB->splice(exitMBB->begin(), BB,
4440 llvm::next(MachineBasicBlock::iterator(MI)),
4441 BB->end());
4442 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004443
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004444 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004445 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4446 unsigned scratch2 = (!BinOpcode) ? incr :
4447 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4448
4449 // thisMBB:
4450 // ...
4451 // fallthrough --> loopMBB
4452 BB->addSuccessor(loopMBB);
4453
4454 // loopMBB:
4455 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004456 // <binop> scratch2, dest, incr
4457 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004458 // cmp scratch, #0
4459 // bne- loopMBB
4460 // fallthrough --> exitMBB
4461 BB = loopMBB;
4462 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004463 if (BinOpcode) {
4464 // operand order needs to go the other way for NAND
4465 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4466 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4467 addReg(incr).addReg(dest)).addReg(0);
4468 else
4469 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4470 addReg(dest).addReg(incr)).addReg(0);
4471 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004472
4473 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4474 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004475 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004476 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004477 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4478 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004479
4480 BB->addSuccessor(loopMBB);
4481 BB->addSuccessor(exitMBB);
4482
4483 // exitMBB:
4484 // ...
4485 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004486
Dan Gohman14152b42010-07-06 20:24:04 +00004487 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004488
Jim Grosbachc3c23542009-12-14 04:22:04 +00004489 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004490}
4491
Evan Cheng218977b2010-07-13 19:27:42 +00004492static
4493MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4494 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4495 E = MBB->succ_end(); I != E; ++I)
4496 if (*I != Succ)
4497 return *I;
4498 llvm_unreachable("Expecting a BB with two successors!");
4499}
4500
Jim Grosbache801dc42009-12-12 01:40:06 +00004501MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004502ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004503 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004504 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004505 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004506 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004507 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004508 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004509 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004510 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004511
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004512 case ARM::ATOMIC_LOAD_ADD_I8:
4513 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4514 case ARM::ATOMIC_LOAD_ADD_I16:
4515 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4516 case ARM::ATOMIC_LOAD_ADD_I32:
4517 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004518
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004519 case ARM::ATOMIC_LOAD_AND_I8:
4520 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4521 case ARM::ATOMIC_LOAD_AND_I16:
4522 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4523 case ARM::ATOMIC_LOAD_AND_I32:
4524 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004525
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004526 case ARM::ATOMIC_LOAD_OR_I8:
4527 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4528 case ARM::ATOMIC_LOAD_OR_I16:
4529 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4530 case ARM::ATOMIC_LOAD_OR_I32:
4531 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004532
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004533 case ARM::ATOMIC_LOAD_XOR_I8:
4534 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4535 case ARM::ATOMIC_LOAD_XOR_I16:
4536 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4537 case ARM::ATOMIC_LOAD_XOR_I32:
4538 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004539
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004540 case ARM::ATOMIC_LOAD_NAND_I8:
4541 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4542 case ARM::ATOMIC_LOAD_NAND_I16:
4543 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4544 case ARM::ATOMIC_LOAD_NAND_I32:
4545 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004546
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004547 case ARM::ATOMIC_LOAD_SUB_I8:
4548 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4549 case ARM::ATOMIC_LOAD_SUB_I16:
4550 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4551 case ARM::ATOMIC_LOAD_SUB_I32:
4552 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004553
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004554 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
4555 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
4556 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00004557
4558 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
4559 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
4560 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004561
Evan Cheng007ea272009-08-12 05:17:19 +00004562 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00004563 // To "insert" a SELECT_CC instruction, we actually have to insert the
4564 // diamond control-flow pattern. The incoming instruction knows the
4565 // destination vreg to set, the condition code register to branch on, the
4566 // true/false values to select between, and a branch opcode to use.
4567 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004568 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00004569 ++It;
4570
4571 // thisMBB:
4572 // ...
4573 // TrueVal = ...
4574 // cmpTY ccX, r1, r2
4575 // bCC copy1MBB
4576 // fallthrough --> copy0MBB
4577 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004578 MachineFunction *F = BB->getParent();
4579 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4580 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00004581 F->insert(It, copy0MBB);
4582 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004583
4584 // Transfer the remainder of BB and its successor edges to sinkMBB.
4585 sinkMBB->splice(sinkMBB->begin(), BB,
4586 llvm::next(MachineBasicBlock::iterator(MI)),
4587 BB->end());
4588 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4589
Dan Gohman258c58c2010-07-06 15:49:48 +00004590 BB->addSuccessor(copy0MBB);
4591 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00004592
Dan Gohman14152b42010-07-06 20:24:04 +00004593 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
4594 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
4595
Evan Chenga8e29892007-01-19 07:51:42 +00004596 // copy0MBB:
4597 // %FalseValue = ...
4598 // # fallthrough to sinkMBB
4599 BB = copy0MBB;
4600
4601 // Update machine-CFG edges
4602 BB->addSuccessor(sinkMBB);
4603
4604 // sinkMBB:
4605 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4606 // ...
4607 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004608 BuildMI(*BB, BB->begin(), dl,
4609 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00004610 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4611 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4612
Dan Gohman14152b42010-07-06 20:24:04 +00004613 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00004614 return BB;
4615 }
Evan Cheng86198642009-08-07 00:34:42 +00004616
Evan Cheng218977b2010-07-13 19:27:42 +00004617 case ARM::BCCi64:
4618 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00004619 // If there is an unconditional branch to the other successor, remove it.
4620 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004621
Evan Cheng218977b2010-07-13 19:27:42 +00004622 // Compare both parts that make up the double comparison separately for
4623 // equality.
4624 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
4625
4626 unsigned LHS1 = MI->getOperand(1).getReg();
4627 unsigned LHS2 = MI->getOperand(2).getReg();
4628 if (RHSisZero) {
4629 AddDefaultPred(BuildMI(BB, dl,
4630 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4631 .addReg(LHS1).addImm(0));
4632 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
4633 .addReg(LHS2).addImm(0)
4634 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4635 } else {
4636 unsigned RHS1 = MI->getOperand(3).getReg();
4637 unsigned RHS2 = MI->getOperand(4).getReg();
4638 AddDefaultPred(BuildMI(BB, dl,
4639 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4640 .addReg(LHS1).addReg(RHS1));
4641 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
4642 .addReg(LHS2).addReg(RHS2)
4643 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
4644 }
4645
4646 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
4647 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
4648 if (MI->getOperand(0).getImm() == ARMCC::NE)
4649 std::swap(destMBB, exitMBB);
4650
4651 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4652 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
4653 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
4654 .addMBB(exitMBB);
4655
4656 MI->eraseFromParent(); // The pseudo instruction is gone now.
4657 return BB;
4658 }
Evan Chenga8e29892007-01-19 07:51:42 +00004659 }
4660}
4661
4662//===----------------------------------------------------------------------===//
4663// ARM Optimization Hooks
4664//===----------------------------------------------------------------------===//
4665
Chris Lattnerd1980a52009-03-12 06:52:53 +00004666static
4667SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
4668 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004669 SelectionDAG &DAG = DCI.DAG;
4670 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004671 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00004672 unsigned Opc = N->getOpcode();
4673 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
4674 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
4675 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
4676 ISD::CondCode CC = ISD::SETCC_INVALID;
4677
4678 if (isSlctCC) {
4679 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
4680 } else {
4681 SDValue CCOp = Slct.getOperand(0);
4682 if (CCOp.getOpcode() == ISD::SETCC)
4683 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
4684 }
4685
4686 bool DoXform = false;
4687 bool InvCC = false;
4688 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
4689 "Bad input!");
4690
4691 if (LHS.getOpcode() == ISD::Constant &&
4692 cast<ConstantSDNode>(LHS)->isNullValue()) {
4693 DoXform = true;
4694 } else if (CC != ISD::SETCC_INVALID &&
4695 RHS.getOpcode() == ISD::Constant &&
4696 cast<ConstantSDNode>(RHS)->isNullValue()) {
4697 std::swap(LHS, RHS);
4698 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004699 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00004700 Op0.getOperand(0).getValueType();
4701 bool isInt = OpVT.isInteger();
4702 CC = ISD::getSetCCInverse(CC, isInt);
4703
4704 if (!TLI.isCondCodeLegal(CC, OpVT))
4705 return SDValue(); // Inverse operator isn't legal.
4706
4707 DoXform = true;
4708 InvCC = true;
4709 }
4710
4711 if (DoXform) {
4712 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
4713 if (isSlctCC)
4714 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
4715 Slct.getOperand(0), Slct.getOperand(1), CC);
4716 SDValue CCOp = Slct.getOperand(0);
4717 if (InvCC)
4718 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
4719 CCOp.getOperand(0), CCOp.getOperand(1), CC);
4720 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4721 CCOp, OtherOp, Result);
4722 }
4723 return SDValue();
4724}
4725
Bob Wilson3d5792a2010-07-29 20:34:14 +00004726/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
4727/// operands N0 and N1. This is a helper for PerformADDCombine that is
4728/// called with the default operands, and if that fails, with commuted
4729/// operands.
4730static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
4731 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00004732 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
4733 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
4734 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
4735 if (Result.getNode()) return Result;
4736 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00004737 return SDValue();
4738}
4739
Bob Wilson3d5792a2010-07-29 20:34:14 +00004740/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
4741///
4742static SDValue PerformADDCombine(SDNode *N,
4743 TargetLowering::DAGCombinerInfo &DCI) {
4744 SDValue N0 = N->getOperand(0);
4745 SDValue N1 = N->getOperand(1);
4746
4747 // First try with the default operand order.
4748 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
4749 if (Result.getNode())
4750 return Result;
4751
4752 // If that didn't work, try again with the operands commuted.
4753 return PerformADDCombineWithOperands(N, N1, N0, DCI);
4754}
4755
Chris Lattnerd1980a52009-03-12 06:52:53 +00004756/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00004757///
Chris Lattnerd1980a52009-03-12 06:52:53 +00004758static SDValue PerformSUBCombine(SDNode *N,
4759 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00004760 SDValue N0 = N->getOperand(0);
4761 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00004762
Chris Lattnerd1980a52009-03-12 06:52:53 +00004763 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
4764 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
4765 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
4766 if (Result.getNode()) return Result;
4767 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00004768
Chris Lattnerd1980a52009-03-12 06:52:53 +00004769 return SDValue();
4770}
4771
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004772static SDValue PerformMULCombine(SDNode *N,
4773 TargetLowering::DAGCombinerInfo &DCI,
4774 const ARMSubtarget *Subtarget) {
4775 SelectionDAG &DAG = DCI.DAG;
4776
4777 if (Subtarget->isThumb1Only())
4778 return SDValue();
4779
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004780 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
4781 return SDValue();
4782
4783 EVT VT = N->getValueType(0);
4784 if (VT != MVT::i32)
4785 return SDValue();
4786
4787 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
4788 if (!C)
4789 return SDValue();
4790
4791 uint64_t MulAmt = C->getZExtValue();
4792 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
4793 ShiftAmt = ShiftAmt & (32 - 1);
4794 SDValue V = N->getOperand(0);
4795 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004796
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004797 SDValue Res;
4798 MulAmt >>= ShiftAmt;
4799 if (isPowerOf2_32(MulAmt - 1)) {
4800 // (mul x, 2^N + 1) => (add (shl x, N), x)
4801 Res = DAG.getNode(ISD::ADD, DL, VT,
4802 V, DAG.getNode(ISD::SHL, DL, VT,
4803 V, DAG.getConstant(Log2_32(MulAmt-1),
4804 MVT::i32)));
4805 } else if (isPowerOf2_32(MulAmt + 1)) {
4806 // (mul x, 2^N - 1) => (sub (shl x, N), x)
4807 Res = DAG.getNode(ISD::SUB, DL, VT,
4808 DAG.getNode(ISD::SHL, DL, VT,
4809 V, DAG.getConstant(Log2_32(MulAmt+1),
4810 MVT::i32)),
4811 V);
4812 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004813 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004814
4815 if (ShiftAmt != 0)
4816 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
4817 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004818
4819 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00004820 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00004821 return SDValue();
4822}
4823
Owen Anderson080c0922010-11-05 19:27:46 +00004824static SDValue PerformANDCombine(SDNode *N,
4825 TargetLowering::DAGCombinerInfo &DCI) {
4826 // Attempt to use immediate-form VBIC
4827 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4828 DebugLoc dl = N->getDebugLoc();
4829 EVT VT = N->getValueType(0);
4830 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004831
Owen Anderson080c0922010-11-05 19:27:46 +00004832 APInt SplatBits, SplatUndef;
4833 unsigned SplatBitSize;
4834 bool HasAnyUndefs;
4835 if (BVN &&
4836 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4837 if (SplatBitSize <= 64) {
4838 EVT VbicVT;
4839 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
4840 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004841 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004842 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00004843 if (Val.getNode()) {
4844 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004845 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00004846 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004847 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00004848 }
4849 }
4850 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004851
Owen Anderson080c0922010-11-05 19:27:46 +00004852 return SDValue();
4853}
4854
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004855/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
4856static SDValue PerformORCombine(SDNode *N,
4857 TargetLowering::DAGCombinerInfo &DCI,
4858 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00004859 // Attempt to use immediate-form VORR
4860 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
4861 DebugLoc dl = N->getDebugLoc();
4862 EVT VT = N->getValueType(0);
4863 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004864
Owen Anderson60f48702010-11-03 23:15:26 +00004865 APInt SplatBits, SplatUndef;
4866 unsigned SplatBitSize;
4867 bool HasAnyUndefs;
4868 if (BVN && Subtarget->hasNEON() &&
4869 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4870 if (SplatBitSize <= 64) {
4871 EVT VorrVT;
4872 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4873 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004874 DAG, VorrVT, VT.is128BitVector(),
4875 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00004876 if (Val.getNode()) {
4877 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004878 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00004879 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004880 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00004881 }
4882 }
4883 }
4884
Jim Grosbach54238562010-07-17 03:30:54 +00004885 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
4886 // reasonable.
4887
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004888 // BFI is only available on V6T2+
4889 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
4890 return SDValue();
4891
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004892 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00004893 DebugLoc DL = N->getDebugLoc();
4894 // 1) or (and A, mask), val => ARMbfi A, val, mask
4895 // iff (val & mask) == val
4896 //
4897 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
4898 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
4899 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4900 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
4901 // && CountPopulation_32(mask) == CountPopulation_32(~mask2)
4902 // (i.e., copy a bitfield value into another bitfield of the same width)
4903 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004904 return SDValue();
4905
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004906 if (VT != MVT::i32)
4907 return SDValue();
4908
Evan Cheng30fb13f2010-12-13 20:32:54 +00004909 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00004910
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004911 // The value and the mask need to be constants so we can verify this is
4912 // actually a bitfield set. If the mask is 0xffff, we can do better
4913 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00004914 SDValue MaskOp = N0.getOperand(1);
4915 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
4916 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004917 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004918 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004919 if (Mask == 0xffff)
4920 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004921 SDValue Res;
4922 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004923 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4924 if (N1C) {
4925 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004926 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00004927 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004928
Evan Chenga9688c42010-12-11 04:11:38 +00004929 if (ARM::isBitFieldInvertedMask(Mask)) {
4930 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00004931
Evan Cheng30fb13f2010-12-13 20:32:54 +00004932 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00004933 DAG.getConstant(Val, MVT::i32),
4934 DAG.getConstant(Mask, MVT::i32));
4935
4936 // Do not add new nodes to DAG combiner worklist.
4937 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004938 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00004939 }
Jim Grosbach54238562010-07-17 03:30:54 +00004940 } else if (N1.getOpcode() == ISD::AND) {
4941 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00004942 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
4943 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00004944 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00004945 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004946
4947 if (ARM::isBitFieldInvertedMask(Mask) &&
4948 ARM::isBitFieldInvertedMask(~Mask2) &&
4949 (CountPopulation_32(Mask) == CountPopulation_32(~Mask2))) {
4950 // The pack halfword instruction works better for masks that fit it,
4951 // so use that when it's available.
4952 if (Subtarget->hasT2ExtractPack() &&
4953 (Mask == 0xffff || Mask == 0xffff0000))
4954 return SDValue();
4955 // 2a
4956 unsigned lsb = CountTrailingZeros_32(Mask2);
4957 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
4958 DAG.getConstant(lsb, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00004959 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00004960 DAG.getConstant(Mask, MVT::i32));
4961 // Do not add new nodes to DAG combiner worklist.
4962 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004963 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004964 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
4965 ARM::isBitFieldInvertedMask(Mask2) &&
4966 (CountPopulation_32(~Mask) == CountPopulation_32(Mask2))) {
4967 // The pack halfword instruction works better for masks that fit it,
4968 // so use that when it's available.
4969 if (Subtarget->hasT2ExtractPack() &&
4970 (Mask2 == 0xffff || Mask2 == 0xffff0000))
4971 return SDValue();
4972 // 2b
4973 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004974 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00004975 DAG.getConstant(lsb, MVT::i32));
4976 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
4977 DAG.getConstant(Mask2, MVT::i32));
4978 // Do not add new nodes to DAG combiner worklist.
4979 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00004980 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00004981 }
4982 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004983
Evan Cheng30fb13f2010-12-13 20:32:54 +00004984 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
4985 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
4986 ARM::isBitFieldInvertedMask(~Mask)) {
4987 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
4988 // where lsb(mask) == #shamt and masked bits of B are known zero.
4989 SDValue ShAmt = N00.getOperand(1);
4990 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
4991 unsigned LSB = CountTrailingZeros_32(Mask);
4992 if (ShAmtC != LSB)
4993 return SDValue();
4994
4995 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
4996 DAG.getConstant(~Mask, MVT::i32));
4997
4998 // Do not add new nodes to DAG combiner worklist.
4999 DCI.CombineTo(N, Res, false);
5000 }
5001
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005002 return SDValue();
5003}
5004
Evan Cheng0c1aec12010-12-14 03:22:07 +00005005/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5006/// C1 & C2 == C1.
5007static SDValue PerformBFICombine(SDNode *N,
5008 TargetLowering::DAGCombinerInfo &DCI) {
5009 SDValue N1 = N->getOperand(1);
5010 if (N1.getOpcode() == ISD::AND) {
5011 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5012 if (!N11C)
5013 return SDValue();
5014 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5015 unsigned Mask2 = N11C->getZExtValue();
5016 if ((Mask & Mask2) == Mask2)
5017 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5018 N->getOperand(0), N1.getOperand(0),
5019 N->getOperand(2));
5020 }
5021 return SDValue();
5022}
5023
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005024/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5025/// ARMISD::VMOVRRD.
5026static SDValue PerformVMOVRRDCombine(SDNode *N,
5027 TargetLowering::DAGCombinerInfo &DCI) {
5028 // vmovrrd(vmovdrr x, y) -> x,y
5029 SDValue InDouble = N->getOperand(0);
5030 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5031 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
5032 return SDValue();
5033}
5034
5035/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5036/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5037static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5038 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5039 SDValue Op0 = N->getOperand(0);
5040 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005041 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005042 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005043 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005044 Op1 = Op1.getOperand(0);
5045 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5046 Op0.getNode() == Op1.getNode() &&
5047 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005048 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005049 N->getValueType(0), Op0.getOperand(0));
5050 return SDValue();
5051}
5052
Bob Wilson31600902010-12-21 06:43:19 +00005053/// PerformSTORECombine - Target-specific dag combine xforms for
5054/// ISD::STORE.
5055static SDValue PerformSTORECombine(SDNode *N,
5056 TargetLowering::DAGCombinerInfo &DCI) {
5057 // Bitcast an i64 store extracted from a vector to f64.
5058 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5059 StoreSDNode *St = cast<StoreSDNode>(N);
5060 SDValue StVal = St->getValue();
5061 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5062 StVal.getValueType() != MVT::i64 ||
5063 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5064 return SDValue();
5065
5066 SelectionDAG &DAG = DCI.DAG;
5067 DebugLoc dl = StVal.getDebugLoc();
5068 SDValue IntVec = StVal.getOperand(0);
5069 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5070 IntVec.getValueType().getVectorNumElements());
5071 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5072 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5073 Vec, StVal.getOperand(1));
5074 dl = N->getDebugLoc();
5075 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5076 // Make the DAGCombiner fold the bitcasts.
5077 DCI.AddToWorklist(Vec.getNode());
5078 DCI.AddToWorklist(ExtElt.getNode());
5079 DCI.AddToWorklist(V.getNode());
5080 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5081 St->getPointerInfo(), St->isVolatile(),
5082 St->isNonTemporal(), St->getAlignment(),
5083 St->getTBAAInfo());
5084}
5085
5086/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5087/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5088/// i64 vector to have f64 elements, since the value can then be loaded
5089/// directly into a VFP register.
5090static bool hasNormalLoadOperand(SDNode *N) {
5091 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5092 for (unsigned i = 0; i < NumElts; ++i) {
5093 SDNode *Elt = N->getOperand(i).getNode();
5094 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5095 return true;
5096 }
5097 return false;
5098}
5099
Bob Wilson75f02882010-09-17 22:59:05 +00005100/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5101/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005102static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5103 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005104 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5105 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5106 // into a pair of GPRs, which is fine when the value is used as a scalar,
5107 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005108 SelectionDAG &DAG = DCI.DAG;
5109 if (N->getNumOperands() == 2) {
5110 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5111 if (RV.getNode())
5112 return RV;
5113 }
Bob Wilson75f02882010-09-17 22:59:05 +00005114
Bob Wilson31600902010-12-21 06:43:19 +00005115 // Load i64 elements as f64 values so that type legalization does not split
5116 // them up into i32 values.
5117 EVT VT = N->getValueType(0);
5118 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5119 return SDValue();
5120 DebugLoc dl = N->getDebugLoc();
5121 SmallVector<SDValue, 8> Ops;
5122 unsigned NumElts = VT.getVectorNumElements();
5123 for (unsigned i = 0; i < NumElts; ++i) {
5124 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5125 Ops.push_back(V);
5126 // Make the DAGCombiner fold the bitcast.
5127 DCI.AddToWorklist(V.getNode());
5128 }
5129 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5130 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5131 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5132}
5133
5134/// PerformInsertEltCombine - Target-specific dag combine xforms for
5135/// ISD::INSERT_VECTOR_ELT.
5136static SDValue PerformInsertEltCombine(SDNode *N,
5137 TargetLowering::DAGCombinerInfo &DCI) {
5138 // Bitcast an i64 load inserted into a vector to f64.
5139 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5140 EVT VT = N->getValueType(0);
5141 SDNode *Elt = N->getOperand(1).getNode();
5142 if (VT.getVectorElementType() != MVT::i64 ||
5143 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5144 return SDValue();
5145
5146 SelectionDAG &DAG = DCI.DAG;
5147 DebugLoc dl = N->getDebugLoc();
5148 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5149 VT.getVectorNumElements());
5150 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5151 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5152 // Make the DAGCombiner fold the bitcasts.
5153 DCI.AddToWorklist(Vec.getNode());
5154 DCI.AddToWorklist(V.getNode());
5155 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5156 Vec, V, N->getOperand(2));
5157 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005158}
5159
Bob Wilsonf20700c2010-10-27 20:38:28 +00005160/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5161/// ISD::VECTOR_SHUFFLE.
5162static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5163 // The LLVM shufflevector instruction does not require the shuffle mask
5164 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5165 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5166 // operands do not match the mask length, they are extended by concatenating
5167 // them with undef vectors. That is probably the right thing for other
5168 // targets, but for NEON it is better to concatenate two double-register
5169 // size vector operands into a single quad-register size vector. Do that
5170 // transformation here:
5171 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5172 // shuffle(concat(v1, v2), undef)
5173 SDValue Op0 = N->getOperand(0);
5174 SDValue Op1 = N->getOperand(1);
5175 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5176 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5177 Op0.getNumOperands() != 2 ||
5178 Op1.getNumOperands() != 2)
5179 return SDValue();
5180 SDValue Concat0Op1 = Op0.getOperand(1);
5181 SDValue Concat1Op1 = Op1.getOperand(1);
5182 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5183 Concat1Op1.getOpcode() != ISD::UNDEF)
5184 return SDValue();
5185 // Skip the transformation if any of the types are illegal.
5186 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5187 EVT VT = N->getValueType(0);
5188 if (!TLI.isTypeLegal(VT) ||
5189 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5190 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5191 return SDValue();
5192
5193 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5194 Op0.getOperand(0), Op1.getOperand(0));
5195 // Translate the shuffle mask.
5196 SmallVector<int, 16> NewMask;
5197 unsigned NumElts = VT.getVectorNumElements();
5198 unsigned HalfElts = NumElts/2;
5199 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5200 for (unsigned n = 0; n < NumElts; ++n) {
5201 int MaskElt = SVN->getMaskElt(n);
5202 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005203 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005204 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005205 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005206 NewElt = HalfElts + MaskElt - NumElts;
5207 NewMask.push_back(NewElt);
5208 }
5209 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5210 DAG.getUNDEF(VT), NewMask.data());
5211}
5212
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005213/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5214/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5215/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5216/// return true.
5217static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5218 SelectionDAG &DAG = DCI.DAG;
5219 EVT VT = N->getValueType(0);
5220 // vldN-dup instructions only support 64-bit vectors for N > 1.
5221 if (!VT.is64BitVector())
5222 return false;
5223
5224 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5225 SDNode *VLD = N->getOperand(0).getNode();
5226 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5227 return false;
5228 unsigned NumVecs = 0;
5229 unsigned NewOpc = 0;
5230 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5231 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5232 NumVecs = 2;
5233 NewOpc = ARMISD::VLD2DUP;
5234 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5235 NumVecs = 3;
5236 NewOpc = ARMISD::VLD3DUP;
5237 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5238 NumVecs = 4;
5239 NewOpc = ARMISD::VLD4DUP;
5240 } else {
5241 return false;
5242 }
5243
5244 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5245 // numbers match the load.
5246 unsigned VLDLaneNo =
5247 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5248 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5249 UI != UE; ++UI) {
5250 // Ignore uses of the chain result.
5251 if (UI.getUse().getResNo() == NumVecs)
5252 continue;
5253 SDNode *User = *UI;
5254 if (User->getOpcode() != ARMISD::VDUPLANE ||
5255 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5256 return false;
5257 }
5258
5259 // Create the vldN-dup node.
5260 EVT Tys[5];
5261 unsigned n;
5262 for (n = 0; n < NumVecs; ++n)
5263 Tys[n] = VT;
5264 Tys[n] = MVT::Other;
5265 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5266 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5267 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5268 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5269 Ops, 2, VLDMemInt->getMemoryVT(),
5270 VLDMemInt->getMemOperand());
5271
5272 // Update the uses.
5273 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5274 UI != UE; ++UI) {
5275 unsigned ResNo = UI.getUse().getResNo();
5276 // Ignore uses of the chain result.
5277 if (ResNo == NumVecs)
5278 continue;
5279 SDNode *User = *UI;
5280 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5281 }
5282
5283 // Now the vldN-lane intrinsic is dead except for its chain result.
5284 // Update uses of the chain.
5285 std::vector<SDValue> VLDDupResults;
5286 for (unsigned n = 0; n < NumVecs; ++n)
5287 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5288 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5289 DCI.CombineTo(VLD, VLDDupResults);
5290
5291 return true;
5292}
5293
Bob Wilson9e82bf12010-07-14 01:22:12 +00005294/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5295/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005296static SDValue PerformVDUPLANECombine(SDNode *N,
5297 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005298 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005299
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005300 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5301 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5302 if (CombineVLDDUP(N, DCI))
5303 return SDValue(N, 0);
5304
5305 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5306 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005307 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005308 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005309 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005310 return SDValue();
5311
5312 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5313 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5314 // The canonical VMOV for a zero vector uses a 32-bit element size.
5315 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5316 unsigned EltBits;
5317 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5318 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005319 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005320 if (EltSize > VT.getVectorElementType().getSizeInBits())
5321 return SDValue();
5322
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005323 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005324}
5325
Bob Wilson5bafff32009-06-22 23:27:02 +00005326/// getVShiftImm - Check if this is a valid build_vector for the immediate
5327/// operand of a vector shift operation, where all the elements of the
5328/// build_vector must have the same constant integer value.
5329static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5330 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005331 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005332 Op = Op.getOperand(0);
5333 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5334 APInt SplatBits, SplatUndef;
5335 unsigned SplatBitSize;
5336 bool HasAnyUndefs;
5337 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5338 HasAnyUndefs, ElementBits) ||
5339 SplatBitSize > ElementBits)
5340 return false;
5341 Cnt = SplatBits.getSExtValue();
5342 return true;
5343}
5344
5345/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5346/// operand of a vector shift left operation. That value must be in the range:
5347/// 0 <= Value < ElementBits for a left shift; or
5348/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005349static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005350 assert(VT.isVector() && "vector shift count is not a vector type");
5351 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5352 if (! getVShiftImm(Op, ElementBits, Cnt))
5353 return false;
5354 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5355}
5356
5357/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5358/// operand of a vector shift right operation. For a shift opcode, the value
5359/// is positive, but for an intrinsic the value count must be negative. The
5360/// absolute value must be in the range:
5361/// 1 <= |Value| <= ElementBits for a right shift; or
5362/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005363static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005364 int64_t &Cnt) {
5365 assert(VT.isVector() && "vector shift count is not a vector type");
5366 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5367 if (! getVShiftImm(Op, ElementBits, Cnt))
5368 return false;
5369 if (isIntrinsic)
5370 Cnt = -Cnt;
5371 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5372}
5373
5374/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5375static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5376 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5377 switch (IntNo) {
5378 default:
5379 // Don't do anything for most intrinsics.
5380 break;
5381
5382 // Vector shifts: check for immediate versions and lower them.
5383 // Note: This is done during DAG combining instead of DAG legalizing because
5384 // the build_vectors for 64-bit vector element shift counts are generally
5385 // not legal, and it is hard to see their values after they get legalized to
5386 // loads from a constant pool.
5387 case Intrinsic::arm_neon_vshifts:
5388 case Intrinsic::arm_neon_vshiftu:
5389 case Intrinsic::arm_neon_vshiftls:
5390 case Intrinsic::arm_neon_vshiftlu:
5391 case Intrinsic::arm_neon_vshiftn:
5392 case Intrinsic::arm_neon_vrshifts:
5393 case Intrinsic::arm_neon_vrshiftu:
5394 case Intrinsic::arm_neon_vrshiftn:
5395 case Intrinsic::arm_neon_vqshifts:
5396 case Intrinsic::arm_neon_vqshiftu:
5397 case Intrinsic::arm_neon_vqshiftsu:
5398 case Intrinsic::arm_neon_vqshiftns:
5399 case Intrinsic::arm_neon_vqshiftnu:
5400 case Intrinsic::arm_neon_vqshiftnsu:
5401 case Intrinsic::arm_neon_vqrshiftns:
5402 case Intrinsic::arm_neon_vqrshiftnu:
5403 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005404 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005405 int64_t Cnt;
5406 unsigned VShiftOpc = 0;
5407
5408 switch (IntNo) {
5409 case Intrinsic::arm_neon_vshifts:
5410 case Intrinsic::arm_neon_vshiftu:
5411 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5412 VShiftOpc = ARMISD::VSHL;
5413 break;
5414 }
5415 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5416 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5417 ARMISD::VSHRs : ARMISD::VSHRu);
5418 break;
5419 }
5420 return SDValue();
5421
5422 case Intrinsic::arm_neon_vshiftls:
5423 case Intrinsic::arm_neon_vshiftlu:
5424 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
5425 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005426 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005427
5428 case Intrinsic::arm_neon_vrshifts:
5429 case Intrinsic::arm_neon_vrshiftu:
5430 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
5431 break;
5432 return SDValue();
5433
5434 case Intrinsic::arm_neon_vqshifts:
5435 case Intrinsic::arm_neon_vqshiftu:
5436 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5437 break;
5438 return SDValue();
5439
5440 case Intrinsic::arm_neon_vqshiftsu:
5441 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
5442 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00005443 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005444
5445 case Intrinsic::arm_neon_vshiftn:
5446 case Intrinsic::arm_neon_vrshiftn:
5447 case Intrinsic::arm_neon_vqshiftns:
5448 case Intrinsic::arm_neon_vqshiftnu:
5449 case Intrinsic::arm_neon_vqshiftnsu:
5450 case Intrinsic::arm_neon_vqrshiftns:
5451 case Intrinsic::arm_neon_vqrshiftnu:
5452 case Intrinsic::arm_neon_vqrshiftnsu:
5453 // Narrowing shifts require an immediate right shift.
5454 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
5455 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00005456 llvm_unreachable("invalid shift count for narrowing vector shift "
5457 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005458
5459 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005460 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00005461 }
5462
5463 switch (IntNo) {
5464 case Intrinsic::arm_neon_vshifts:
5465 case Intrinsic::arm_neon_vshiftu:
5466 // Opcode already set above.
5467 break;
5468 case Intrinsic::arm_neon_vshiftls:
5469 case Intrinsic::arm_neon_vshiftlu:
5470 if (Cnt == VT.getVectorElementType().getSizeInBits())
5471 VShiftOpc = ARMISD::VSHLLi;
5472 else
5473 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
5474 ARMISD::VSHLLs : ARMISD::VSHLLu);
5475 break;
5476 case Intrinsic::arm_neon_vshiftn:
5477 VShiftOpc = ARMISD::VSHRN; break;
5478 case Intrinsic::arm_neon_vrshifts:
5479 VShiftOpc = ARMISD::VRSHRs; break;
5480 case Intrinsic::arm_neon_vrshiftu:
5481 VShiftOpc = ARMISD::VRSHRu; break;
5482 case Intrinsic::arm_neon_vrshiftn:
5483 VShiftOpc = ARMISD::VRSHRN; break;
5484 case Intrinsic::arm_neon_vqshifts:
5485 VShiftOpc = ARMISD::VQSHLs; break;
5486 case Intrinsic::arm_neon_vqshiftu:
5487 VShiftOpc = ARMISD::VQSHLu; break;
5488 case Intrinsic::arm_neon_vqshiftsu:
5489 VShiftOpc = ARMISD::VQSHLsu; break;
5490 case Intrinsic::arm_neon_vqshiftns:
5491 VShiftOpc = ARMISD::VQSHRNs; break;
5492 case Intrinsic::arm_neon_vqshiftnu:
5493 VShiftOpc = ARMISD::VQSHRNu; break;
5494 case Intrinsic::arm_neon_vqshiftnsu:
5495 VShiftOpc = ARMISD::VQSHRNsu; break;
5496 case Intrinsic::arm_neon_vqrshiftns:
5497 VShiftOpc = ARMISD::VQRSHRNs; break;
5498 case Intrinsic::arm_neon_vqrshiftnu:
5499 VShiftOpc = ARMISD::VQRSHRNu; break;
5500 case Intrinsic::arm_neon_vqrshiftnsu:
5501 VShiftOpc = ARMISD::VQRSHRNsu; break;
5502 }
5503
5504 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005505 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005506 }
5507
5508 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00005509 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005510 int64_t Cnt;
5511 unsigned VShiftOpc = 0;
5512
5513 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
5514 VShiftOpc = ARMISD::VSLI;
5515 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
5516 VShiftOpc = ARMISD::VSRI;
5517 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005518 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00005519 }
5520
5521 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
5522 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00005523 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005524 }
5525
5526 case Intrinsic::arm_neon_vqrshifts:
5527 case Intrinsic::arm_neon_vqrshiftu:
5528 // No immediate versions of these to check for.
5529 break;
5530 }
5531
5532 return SDValue();
5533}
5534
5535/// PerformShiftCombine - Checks for immediate versions of vector shifts and
5536/// lowers them. As with the vector shift intrinsics, this is done during DAG
5537/// combining instead of DAG legalizing because the build_vectors for 64-bit
5538/// vector element shift counts are generally not legal, and it is hard to see
5539/// their values after they get legalized to loads from a constant pool.
5540static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
5541 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00005542 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00005543
5544 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00005545 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5546 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00005547 return SDValue();
5548
5549 assert(ST->hasNEON() && "unexpected vector shift");
5550 int64_t Cnt;
5551
5552 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005553 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005554
5555 case ISD::SHL:
5556 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
5557 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005558 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005559 break;
5560
5561 case ISD::SRA:
5562 case ISD::SRL:
5563 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
5564 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
5565 ARMISD::VSHRs : ARMISD::VSHRu);
5566 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00005567 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00005568 }
5569 }
5570 return SDValue();
5571}
5572
5573/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
5574/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
5575static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
5576 const ARMSubtarget *ST) {
5577 SDValue N0 = N->getOperand(0);
5578
5579 // Check for sign- and zero-extensions of vector extract operations of 8-
5580 // and 16-bit vector elements. NEON supports these directly. They are
5581 // handled during DAG combining because type legalization will promote them
5582 // to 32-bit types and it is messy to recognize the operations after that.
5583 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5584 SDValue Vec = N0.getOperand(0);
5585 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005586 EVT VT = N->getValueType(0);
5587 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005588 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5589
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 if (VT == MVT::i32 &&
5591 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00005592 TLI.isTypeLegal(Vec.getValueType()) &&
5593 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005594
5595 unsigned Opc = 0;
5596 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005597 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00005598 case ISD::SIGN_EXTEND:
5599 Opc = ARMISD::VGETLANEs;
5600 break;
5601 case ISD::ZERO_EXTEND:
5602 case ISD::ANY_EXTEND:
5603 Opc = ARMISD::VGETLANEu;
5604 break;
5605 }
5606 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
5607 }
5608 }
5609
5610 return SDValue();
5611}
5612
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005613/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
5614/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
5615static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
5616 const ARMSubtarget *ST) {
5617 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00005618 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005619 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
5620 // a NaN; only do the transformation when it matches that behavior.
5621
5622 // For now only do this when using NEON for FP operations; if using VFP, it
5623 // is not obvious that the benefit outweighs the cost of switching to the
5624 // NEON pipeline.
5625 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
5626 N->getValueType(0) != MVT::f32)
5627 return SDValue();
5628
5629 SDValue CondLHS = N->getOperand(0);
5630 SDValue CondRHS = N->getOperand(1);
5631 SDValue LHS = N->getOperand(2);
5632 SDValue RHS = N->getOperand(3);
5633 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
5634
5635 unsigned Opcode = 0;
5636 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00005637 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005638 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00005639 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005640 IsReversed = true ; // x CC y ? y : x
5641 } else {
5642 return SDValue();
5643 }
5644
Bob Wilsone742bb52010-02-24 22:15:53 +00005645 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005646 switch (CC) {
5647 default: break;
5648 case ISD::SETOLT:
5649 case ISD::SETOLE:
5650 case ISD::SETLT:
5651 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005652 case ISD::SETULT:
5653 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005654 // If LHS is NaN, an ordered comparison will be false and the result will
5655 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
5656 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5657 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
5658 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5659 break;
5660 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
5661 // will return -0, so vmin can only be used for unsafe math or if one of
5662 // the operands is known to be nonzero.
5663 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
5664 !UnsafeFPMath &&
5665 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5666 break;
5667 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005668 break;
5669
5670 case ISD::SETOGT:
5671 case ISD::SETOGE:
5672 case ISD::SETGT:
5673 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005674 case ISD::SETUGT:
5675 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00005676 // If LHS is NaN, an ordered comparison will be false and the result will
5677 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
5678 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
5679 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
5680 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
5681 break;
5682 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
5683 // will return +0, so vmax can only be used for unsafe math or if one of
5684 // the operands is known to be nonzero.
5685 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
5686 !UnsafeFPMath &&
5687 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
5688 break;
5689 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005690 break;
5691 }
5692
5693 if (!Opcode)
5694 return SDValue();
5695 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
5696}
5697
Dan Gohman475871a2008-07-27 21:46:04 +00005698SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00005699 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005700 switch (N->getOpcode()) {
5701 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005702 case ISD::ADD: return PerformADDCombine(N, DCI);
5703 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005704 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005705 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00005706 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00005707 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00005708 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005709 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00005710 case ISD::STORE: return PerformSTORECombine(N, DCI);
5711 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
5712 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00005713 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005714 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005715 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005716 case ISD::SHL:
5717 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005718 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005719 case ISD::SIGN_EXTEND:
5720 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00005721 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
5722 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005723 }
Dan Gohman475871a2008-07-27 21:46:04 +00005724 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00005725}
5726
Bill Wendlingaf566342009-08-15 21:21:19 +00005727bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00005728 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00005729 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00005730
5731 switch (VT.getSimpleVT().SimpleTy) {
5732 default:
5733 return false;
5734 case MVT::i8:
5735 case MVT::i16:
5736 case MVT::i32:
5737 return true;
5738 // FIXME: VLD1 etc with standard alignment is legal.
5739 }
5740}
5741
Evan Chenge6c835f2009-08-14 20:09:37 +00005742static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
5743 if (V < 0)
5744 return false;
5745
5746 unsigned Scale = 1;
5747 switch (VT.getSimpleVT().SimpleTy) {
5748 default: return false;
5749 case MVT::i1:
5750 case MVT::i8:
5751 // Scale == 1;
5752 break;
5753 case MVT::i16:
5754 // Scale == 2;
5755 Scale = 2;
5756 break;
5757 case MVT::i32:
5758 // Scale == 4;
5759 Scale = 4;
5760 break;
5761 }
5762
5763 if ((V & (Scale - 1)) != 0)
5764 return false;
5765 V /= Scale;
5766 return V == (V & ((1LL << 5) - 1));
5767}
5768
5769static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
5770 const ARMSubtarget *Subtarget) {
5771 bool isNeg = false;
5772 if (V < 0) {
5773 isNeg = true;
5774 V = - V;
5775 }
5776
5777 switch (VT.getSimpleVT().SimpleTy) {
5778 default: return false;
5779 case MVT::i1:
5780 case MVT::i8:
5781 case MVT::i16:
5782 case MVT::i32:
5783 // + imm12 or - imm8
5784 if (isNeg)
5785 return V == (V & ((1LL << 8) - 1));
5786 return V == (V & ((1LL << 12) - 1));
5787 case MVT::f32:
5788 case MVT::f64:
5789 // Same as ARM mode. FIXME: NEON?
5790 if (!Subtarget->hasVFP2())
5791 return false;
5792 if ((V & 3) != 0)
5793 return false;
5794 V >>= 2;
5795 return V == (V & ((1LL << 8) - 1));
5796 }
5797}
5798
Evan Chengb01fad62007-03-12 23:30:29 +00005799/// isLegalAddressImmediate - Return true if the integer value can be used
5800/// as the offset of the target addressing mode for load / store of the
5801/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00005802static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005803 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00005804 if (V == 0)
5805 return true;
5806
Evan Cheng65011532009-03-09 19:15:00 +00005807 if (!VT.isSimple())
5808 return false;
5809
Evan Chenge6c835f2009-08-14 20:09:37 +00005810 if (Subtarget->isThumb1Only())
5811 return isLegalT1AddressImmediate(V, VT);
5812 else if (Subtarget->isThumb2())
5813 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00005814
Evan Chenge6c835f2009-08-14 20:09:37 +00005815 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00005816 if (V < 0)
5817 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00005819 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005820 case MVT::i1:
5821 case MVT::i8:
5822 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00005823 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005824 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005825 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00005826 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005827 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005828 case MVT::f32:
5829 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00005830 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00005831 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00005832 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00005833 return false;
5834 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00005835 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00005836 }
Evan Chenga8e29892007-01-19 07:51:42 +00005837}
5838
Evan Chenge6c835f2009-08-14 20:09:37 +00005839bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
5840 EVT VT) const {
5841 int Scale = AM.Scale;
5842 if (Scale < 0)
5843 return false;
5844
5845 switch (VT.getSimpleVT().SimpleTy) {
5846 default: return false;
5847 case MVT::i1:
5848 case MVT::i8:
5849 case MVT::i16:
5850 case MVT::i32:
5851 if (Scale == 1)
5852 return true;
5853 // r + r << imm
5854 Scale = Scale & ~1;
5855 return Scale == 2 || Scale == 4 || Scale == 8;
5856 case MVT::i64:
5857 // r + r
5858 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
5859 return true;
5860 return false;
5861 case MVT::isVoid:
5862 // Note, we allow "void" uses (basically, uses that aren't loads or
5863 // stores), because arm allows folding a scale into many arithmetic
5864 // operations. This should be made more precise and revisited later.
5865
5866 // Allow r << imm, but the imm has to be a multiple of two.
5867 if (Scale & 1) return false;
5868 return isPowerOf2_32(Scale);
5869 }
5870}
5871
Chris Lattner37caf8c2007-04-09 23:33:39 +00005872/// isLegalAddressingMode - Return true if the addressing mode represented
5873/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005874bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00005875 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005876 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00005877 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00005878 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005879
Chris Lattner37caf8c2007-04-09 23:33:39 +00005880 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005881 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005882 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005883
Chris Lattner37caf8c2007-04-09 23:33:39 +00005884 switch (AM.Scale) {
5885 case 0: // no scale reg, must be "r+i" or "r", or "i".
5886 break;
5887 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00005888 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00005889 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005890 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00005891 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00005892 // ARM doesn't support any R+R*scale+imm addr modes.
5893 if (AM.BaseOffs)
5894 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005895
Bob Wilson2c7dab12009-04-08 17:55:28 +00005896 if (!VT.isSimple())
5897 return false;
5898
Evan Chenge6c835f2009-08-14 20:09:37 +00005899 if (Subtarget->isThumb2())
5900 return isLegalT2ScaledAddressingMode(AM, VT);
5901
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005902 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00005903 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00005904 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 case MVT::i1:
5906 case MVT::i8:
5907 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005908 if (Scale < 0) Scale = -Scale;
5909 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005910 return true;
5911 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00005912 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00005913 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00005914 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005915 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00005916 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00005917 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00005918 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00005919
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00005921 // Note, we allow "void" uses (basically, uses that aren't loads or
5922 // stores), because arm allows folding a scale into many arithmetic
5923 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00005924
Chris Lattner37caf8c2007-04-09 23:33:39 +00005925 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00005926 if (Scale & 1) return false;
5927 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00005928 }
5929 break;
Evan Chengb01fad62007-03-12 23:30:29 +00005930 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00005931 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00005932}
5933
Evan Cheng77e47512009-11-11 19:05:52 +00005934/// isLegalICmpImmediate - Return true if the specified immediate is legal
5935/// icmp immediate, that is the target has icmp instructions which can compare
5936/// a register against the immediate without having to materialize the
5937/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00005938bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00005939 if (!Subtarget->isThumb())
5940 return ARM_AM::getSOImmVal(Imm) != -1;
5941 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00005942 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00005943 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00005944}
5945
Owen Andersone50ed302009-08-10 22:56:29 +00005946static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00005947 bool isSEXTLoad, SDValue &Base,
5948 SDValue &Offset, bool &isInc,
5949 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00005950 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
5951 return false;
5952
Owen Anderson825b72b2009-08-11 20:47:22 +00005953 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00005954 // AddressingMode 3
5955 Base = Ptr->getOperand(0);
5956 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005957 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005958 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005959 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005960 isInc = false;
5961 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5962 return true;
5963 }
5964 }
5965 isInc = (Ptr->getOpcode() == ISD::ADD);
5966 Offset = Ptr->getOperand(1);
5967 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005968 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00005969 // AddressingMode 2
5970 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005971 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005972 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00005973 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00005974 isInc = false;
5975 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
5976 Base = Ptr->getOperand(0);
5977 return true;
5978 }
5979 }
5980
5981 if (Ptr->getOpcode() == ISD::ADD) {
5982 isInc = true;
5983 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
5984 if (ShOpcVal != ARM_AM::no_shift) {
5985 Base = Ptr->getOperand(1);
5986 Offset = Ptr->getOperand(0);
5987 } else {
5988 Base = Ptr->getOperand(0);
5989 Offset = Ptr->getOperand(1);
5990 }
5991 return true;
5992 }
5993
5994 isInc = (Ptr->getOpcode() == ISD::ADD);
5995 Base = Ptr->getOperand(0);
5996 Offset = Ptr->getOperand(1);
5997 return true;
5998 }
5999
Jim Grosbache5165492009-11-09 00:11:35 +00006000 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006001 return false;
6002}
6003
Owen Andersone50ed302009-08-10 22:56:29 +00006004static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006005 bool isSEXTLoad, SDValue &Base,
6006 SDValue &Offset, bool &isInc,
6007 SelectionDAG &DAG) {
6008 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6009 return false;
6010
6011 Base = Ptr->getOperand(0);
6012 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6013 int RHSC = (int)RHS->getZExtValue();
6014 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6015 assert(Ptr->getOpcode() == ISD::ADD);
6016 isInc = false;
6017 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6018 return true;
6019 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6020 isInc = Ptr->getOpcode() == ISD::ADD;
6021 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6022 return true;
6023 }
6024 }
6025
6026 return false;
6027}
6028
Evan Chenga8e29892007-01-19 07:51:42 +00006029/// getPreIndexedAddressParts - returns true by value, base pointer and
6030/// offset pointer and addressing mode by reference if the node's address
6031/// can be legally represented as pre-indexed load / store address.
6032bool
Dan Gohman475871a2008-07-27 21:46:04 +00006033ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6034 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006035 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006036 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006037 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006038 return false;
6039
Owen Andersone50ed302009-08-10 22:56:29 +00006040 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006041 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006042 bool isSEXTLoad = false;
6043 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6044 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006045 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006046 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6047 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6048 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006049 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006050 } else
6051 return false;
6052
6053 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006054 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006055 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006056 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6057 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006058 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006059 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006060 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006061 if (!isLegal)
6062 return false;
6063
6064 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6065 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006066}
6067
6068/// getPostIndexedAddressParts - returns true by value, base pointer and
6069/// offset pointer and addressing mode by reference if this node can be
6070/// combined with a load / store to form a post-indexed load / store.
6071bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006072 SDValue &Base,
6073 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006074 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006075 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006076 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006077 return false;
6078
Owen Andersone50ed302009-08-10 22:56:29 +00006079 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006080 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006081 bool isSEXTLoad = false;
6082 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006083 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006084 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006085 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6086 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006087 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006088 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006089 } else
6090 return false;
6091
6092 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006093 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006094 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006095 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006096 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006097 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006098 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6099 isInc, DAG);
6100 if (!isLegal)
6101 return false;
6102
Evan Cheng28dad2a2010-05-18 21:31:17 +00006103 if (Ptr != Base) {
6104 // Swap base ptr and offset to catch more post-index load / store when
6105 // it's legal. In Thumb2 mode, offset must be an immediate.
6106 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6107 !Subtarget->isThumb2())
6108 std::swap(Base, Offset);
6109
6110 // Post-indexed load / store update the base pointer.
6111 if (Ptr != Base)
6112 return false;
6113 }
6114
Evan Chenge88d5ce2009-07-02 07:28:31 +00006115 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6116 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006117}
6118
Dan Gohman475871a2008-07-27 21:46:04 +00006119void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006120 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006121 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006122 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006123 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006124 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006125 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006126 switch (Op.getOpcode()) {
6127 default: break;
6128 case ARMISD::CMOV: {
6129 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006130 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006131 if (KnownZero == 0 && KnownOne == 0) return;
6132
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006133 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006134 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6135 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006136 KnownZero &= KnownZeroRHS;
6137 KnownOne &= KnownOneRHS;
6138 return;
6139 }
6140 }
6141}
6142
6143//===----------------------------------------------------------------------===//
6144// ARM Inline Assembly Support
6145//===----------------------------------------------------------------------===//
6146
Evan Cheng55d42002011-01-08 01:24:27 +00006147bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6148 // Looking for "rev" which is V6+.
6149 if (!Subtarget->hasV6Ops())
6150 return false;
6151
6152 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6153 std::string AsmStr = IA->getAsmString();
6154 SmallVector<StringRef, 4> AsmPieces;
6155 SplitString(AsmStr, AsmPieces, ";\n");
6156
6157 switch (AsmPieces.size()) {
6158 default: return false;
6159 case 1:
6160 AsmStr = AsmPieces[0];
6161 AsmPieces.clear();
6162 SplitString(AsmStr, AsmPieces, " \t,");
6163
6164 // rev $0, $1
6165 if (AsmPieces.size() == 3 &&
6166 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6167 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6168 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6169 if (Ty && Ty->getBitWidth() == 32)
6170 return IntrinsicLowering::LowerToByteSwap(CI);
6171 }
6172 break;
6173 }
6174
6175 return false;
6176}
6177
Evan Chenga8e29892007-01-19 07:51:42 +00006178/// getConstraintType - Given a constraint letter, return the type of
6179/// constraint it is for this target.
6180ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006181ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6182 if (Constraint.size() == 1) {
6183 switch (Constraint[0]) {
6184 default: break;
6185 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006186 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006187 }
Evan Chenga8e29892007-01-19 07:51:42 +00006188 }
Chris Lattner4234f572007-03-25 02:14:49 +00006189 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006190}
6191
John Thompson44ab89e2010-10-29 17:29:13 +00006192/// Examine constraint type and operand type and determine a weight value.
6193/// This object must already have been set up with the operand type
6194/// and the current alternative constraint selected.
6195TargetLowering::ConstraintWeight
6196ARMTargetLowering::getSingleConstraintMatchWeight(
6197 AsmOperandInfo &info, const char *constraint) const {
6198 ConstraintWeight weight = CW_Invalid;
6199 Value *CallOperandVal = info.CallOperandVal;
6200 // If we don't have a value, we can't do a match,
6201 // but allow it at the lowest weight.
6202 if (CallOperandVal == NULL)
6203 return CW_Default;
6204 const Type *type = CallOperandVal->getType();
6205 // Look at the constraint type.
6206 switch (*constraint) {
6207 default:
6208 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6209 break;
6210 case 'l':
6211 if (type->isIntegerTy()) {
6212 if (Subtarget->isThumb())
6213 weight = CW_SpecificReg;
6214 else
6215 weight = CW_Register;
6216 }
6217 break;
6218 case 'w':
6219 if (type->isFloatingPointTy())
6220 weight = CW_Register;
6221 break;
6222 }
6223 return weight;
6224}
6225
Bob Wilson2dc4f542009-03-20 22:42:55 +00006226std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006227ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006228 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006229 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006230 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006231 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006232 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006233 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006234 return std::make_pair(0U, ARM::tGPRRegisterClass);
6235 else
6236 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006237 case 'r':
6238 return std::make_pair(0U, ARM::GPRRegisterClass);
6239 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006240 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006241 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006242 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006243 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006244 if (VT.getSizeInBits() == 128)
6245 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006246 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006247 }
6248 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006249 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006250 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006251
Evan Chenga8e29892007-01-19 07:51:42 +00006252 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6253}
6254
6255std::vector<unsigned> ARMTargetLowering::
6256getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006257 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006258 if (Constraint.size() != 1)
6259 return std::vector<unsigned>();
6260
6261 switch (Constraint[0]) { // GCC ARM Constraint Letters
6262 default: break;
6263 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006264 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6265 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6266 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006267 case 'r':
6268 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6269 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6270 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6271 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006272 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006273 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006274 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6275 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6276 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6277 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6278 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6279 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6280 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6281 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006282 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006283 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6284 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6285 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6286 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006287 if (VT.getSizeInBits() == 128)
6288 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6289 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006290 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006291 }
6292
6293 return std::vector<unsigned>();
6294}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006295
6296/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6297/// vector. If it is invalid, don't add anything to Ops.
6298void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6299 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006300 std::vector<SDValue>&Ops,
6301 SelectionDAG &DAG) const {
6302 SDValue Result(0, 0);
6303
6304 switch (Constraint) {
6305 default: break;
6306 case 'I': case 'J': case 'K': case 'L':
6307 case 'M': case 'N': case 'O':
6308 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6309 if (!C)
6310 return;
6311
6312 int64_t CVal64 = C->getSExtValue();
6313 int CVal = (int) CVal64;
6314 // None of these constraints allow values larger than 32 bits. Check
6315 // that the value fits in an int.
6316 if (CVal != CVal64)
6317 return;
6318
6319 switch (Constraint) {
6320 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006321 if (Subtarget->isThumb1Only()) {
6322 // This must be a constant between 0 and 255, for ADD
6323 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006324 if (CVal >= 0 && CVal <= 255)
6325 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006326 } else if (Subtarget->isThumb2()) {
6327 // A constant that can be used as an immediate value in a
6328 // data-processing instruction.
6329 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6330 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006331 } else {
6332 // A constant that can be used as an immediate value in a
6333 // data-processing instruction.
6334 if (ARM_AM::getSOImmVal(CVal) != -1)
6335 break;
6336 }
6337 return;
6338
6339 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006340 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006341 // This must be a constant between -255 and -1, for negated ADD
6342 // immediates. This can be used in GCC with an "n" modifier that
6343 // prints the negated value, for use with SUB instructions. It is
6344 // not useful otherwise but is implemented for compatibility.
6345 if (CVal >= -255 && CVal <= -1)
6346 break;
6347 } else {
6348 // This must be a constant between -4095 and 4095. It is not clear
6349 // what this constraint is intended for. Implemented for
6350 // compatibility with GCC.
6351 if (CVal >= -4095 && CVal <= 4095)
6352 break;
6353 }
6354 return;
6355
6356 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006357 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006358 // A 32-bit value where only one byte has a nonzero value. Exclude
6359 // zero to match GCC. This constraint is used by GCC internally for
6360 // constants that can be loaded with a move/shift combination.
6361 // It is not useful otherwise but is implemented for compatibility.
6362 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6363 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006364 } else if (Subtarget->isThumb2()) {
6365 // A constant whose bitwise inverse can be used as an immediate
6366 // value in a data-processing instruction. This can be used in GCC
6367 // with a "B" modifier that prints the inverted value, for use with
6368 // BIC and MVN instructions. It is not useful otherwise but is
6369 // implemented for compatibility.
6370 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
6371 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006372 } else {
6373 // A constant whose bitwise inverse can be used as an immediate
6374 // value in a data-processing instruction. This can be used in GCC
6375 // with a "B" modifier that prints the inverted value, for use with
6376 // BIC and MVN instructions. It is not useful otherwise but is
6377 // implemented for compatibility.
6378 if (ARM_AM::getSOImmVal(~CVal) != -1)
6379 break;
6380 }
6381 return;
6382
6383 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006384 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006385 // This must be a constant between -7 and 7,
6386 // for 3-operand ADD/SUB immediate instructions.
6387 if (CVal >= -7 && CVal < 7)
6388 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006389 } else if (Subtarget->isThumb2()) {
6390 // A constant whose negation can be used as an immediate value in a
6391 // data-processing instruction. This can be used in GCC with an "n"
6392 // modifier that prints the negated value, for use with SUB
6393 // instructions. It is not useful otherwise but is implemented for
6394 // compatibility.
6395 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
6396 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006397 } else {
6398 // A constant whose negation can be used as an immediate value in a
6399 // data-processing instruction. This can be used in GCC with an "n"
6400 // modifier that prints the negated value, for use with SUB
6401 // instructions. It is not useful otherwise but is implemented for
6402 // compatibility.
6403 if (ARM_AM::getSOImmVal(-CVal) != -1)
6404 break;
6405 }
6406 return;
6407
6408 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006409 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006410 // This must be a multiple of 4 between 0 and 1020, for
6411 // ADD sp + immediate.
6412 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
6413 break;
6414 } else {
6415 // A power of two or a constant between 0 and 32. This is used in
6416 // GCC for the shift amount on shifted register operands, but it is
6417 // useful in general for any shift amounts.
6418 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
6419 break;
6420 }
6421 return;
6422
6423 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006424 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006425 // This must be a constant between 0 and 31, for shift amounts.
6426 if (CVal >= 0 && CVal <= 31)
6427 break;
6428 }
6429 return;
6430
6431 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006432 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006433 // This must be a multiple of 4 between -508 and 508, for
6434 // ADD/SUB sp = sp + immediate.
6435 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
6436 break;
6437 }
6438 return;
6439 }
6440 Result = DAG.getTargetConstant(CVal, Op.getValueType());
6441 break;
6442 }
6443
6444 if (Result.getNode()) {
6445 Ops.push_back(Result);
6446 return;
6447 }
Dale Johannesen1784d162010-06-25 21:55:36 +00006448 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006449}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00006450
6451bool
6452ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
6453 // The ARM target isn't yet aware of offsets.
6454 return false;
6455}
Evan Cheng39382422009-10-28 01:44:26 +00006456
6457int ARM::getVFPf32Imm(const APFloat &FPImm) {
6458 APInt Imm = FPImm.bitcastToAPInt();
6459 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
6460 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
6461 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
6462
6463 // We can handle 4 bits of mantissa.
6464 // mantissa = (16+UInt(e:f:g:h))/16.
6465 if (Mantissa & 0x7ffff)
6466 return -1;
6467 Mantissa >>= 19;
6468 if ((Mantissa & 0xf) != Mantissa)
6469 return -1;
6470
6471 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6472 if (Exp < -3 || Exp > 4)
6473 return -1;
6474 Exp = ((Exp+3) & 0x7) ^ 4;
6475
6476 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6477}
6478
6479int ARM::getVFPf64Imm(const APFloat &FPImm) {
6480 APInt Imm = FPImm.bitcastToAPInt();
6481 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
6482 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
6483 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
6484
6485 // We can handle 4 bits of mantissa.
6486 // mantissa = (16+UInt(e:f:g:h))/16.
6487 if (Mantissa & 0xffffffffffffLL)
6488 return -1;
6489 Mantissa >>= 48;
6490 if ((Mantissa & 0xf) != Mantissa)
6491 return -1;
6492
6493 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
6494 if (Exp < -3 || Exp > 4)
6495 return -1;
6496 Exp = ((Exp+3) & 0x7) ^ 4;
6497
6498 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
6499}
6500
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006501bool ARM::isBitFieldInvertedMask(unsigned v) {
6502 if (v == 0xffffffff)
6503 return 0;
6504 // there can be 1's on either or both "outsides", all the "inside"
6505 // bits must be 0's
6506 unsigned int lsb = 0, msb = 31;
6507 while (v & (1 << msb)) --msb;
6508 while (v & (1 << lsb)) ++lsb;
6509 for (unsigned int i = lsb; i <= msb; ++i) {
6510 if (v & (1 << i))
6511 return 0;
6512 }
6513 return 1;
6514}
6515
Evan Cheng39382422009-10-28 01:44:26 +00006516/// isFPImmLegal - Returns true if the target can instruction select the
6517/// specified FP immediate natively. If false, the legalizer will
6518/// materialize the FP immediate as a load from a constant pool.
6519bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
6520 if (!Subtarget->hasVFP3())
6521 return false;
6522 if (VT == MVT::f32)
6523 return ARM::getVFPf32Imm(Imm) != -1;
6524 if (VT == MVT::f64)
6525 return ARM::getVFPf64Imm(Imm) != -1;
6526 return false;
6527}
Bob Wilson65ffec42010-09-21 17:56:22 +00006528
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006529/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00006530/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
6531/// specified in the intrinsic calls.
6532bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
6533 const CallInst &I,
6534 unsigned Intrinsic) const {
6535 switch (Intrinsic) {
6536 case Intrinsic::arm_neon_vld1:
6537 case Intrinsic::arm_neon_vld2:
6538 case Intrinsic::arm_neon_vld3:
6539 case Intrinsic::arm_neon_vld4:
6540 case Intrinsic::arm_neon_vld2lane:
6541 case Intrinsic::arm_neon_vld3lane:
6542 case Intrinsic::arm_neon_vld4lane: {
6543 Info.opc = ISD::INTRINSIC_W_CHAIN;
6544 // Conservatively set memVT to the entire set of vectors loaded.
6545 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
6546 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6547 Info.ptrVal = I.getArgOperand(0);
6548 Info.offset = 0;
6549 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6550 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6551 Info.vol = false; // volatile loads with NEON intrinsics not supported
6552 Info.readMem = true;
6553 Info.writeMem = false;
6554 return true;
6555 }
6556 case Intrinsic::arm_neon_vst1:
6557 case Intrinsic::arm_neon_vst2:
6558 case Intrinsic::arm_neon_vst3:
6559 case Intrinsic::arm_neon_vst4:
6560 case Intrinsic::arm_neon_vst2lane:
6561 case Intrinsic::arm_neon_vst3lane:
6562 case Intrinsic::arm_neon_vst4lane: {
6563 Info.opc = ISD::INTRINSIC_VOID;
6564 // Conservatively set memVT to the entire set of vectors stored.
6565 unsigned NumElts = 0;
6566 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
6567 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
6568 if (!ArgTy->isVectorTy())
6569 break;
6570 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
6571 }
6572 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
6573 Info.ptrVal = I.getArgOperand(0);
6574 Info.offset = 0;
6575 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
6576 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
6577 Info.vol = false; // volatile stores with NEON intrinsics not supported
6578 Info.readMem = false;
6579 Info.writeMem = true;
6580 return true;
6581 }
6582 default:
6583 break;
6584 }
6585
6586 return false;
6587}