blob: 330a7816f5ca2ece0d5e7bc594f0a5b5145fbefd [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
Evan Cheng2c69f8e2011-04-07 00:58:44 +0000396 if (HasDivModLibcall) {
Evan Cheng8e23e812011-04-01 00:42:02 +0000397 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
398 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
399 }
400
David Goodwinf1daf7d2009-07-08 23:10:31 +0000401 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000403 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000405 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000407 if (!Subtarget->isFPOnlySP())
408 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000409
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000411 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000412
413 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 addDRTypeForNEON(MVT::v2f32);
415 addDRTypeForNEON(MVT::v8i8);
416 addDRTypeForNEON(MVT::v4i16);
417 addDRTypeForNEON(MVT::v2i32);
418 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000419
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 addQRTypeForNEON(MVT::v4f32);
421 addQRTypeForNEON(MVT::v2f64);
422 addQRTypeForNEON(MVT::v16i8);
423 addQRTypeForNEON(MVT::v8i16);
424 addQRTypeForNEON(MVT::v4i32);
425 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000426
Bob Wilson74dc72e2009-09-15 23:55:57 +0000427 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
428 // neither Neon nor VFP support any arithmetic operations on it.
429 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
430 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
431 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
432 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
433 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
434 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
435 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
436 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
437 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
438 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
439 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
440 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
441 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
442 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
443 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
444 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
445 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
446 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
447 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
448 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
449 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
450 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
451 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
452 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
453
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000454 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
455
Bob Wilson642b3292009-09-16 00:32:15 +0000456 // Neon does not support some operations on v1i64 and v2i64 types.
457 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000458 // Custom handling for some quad-vector types to detect VMULL.
459 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
460 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
461 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000462 // Custom handling for some vector types to avoid expensive expansions
463 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
464 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
465 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
466 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000467 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
468 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000469 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
470 // a destination type that is wider than the source.
471 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
472 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000473
Bob Wilson1c3ef902011-02-07 17:43:21 +0000474 setTargetDAGCombine(ISD::INTRINSIC_VOID);
475 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000476 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
477 setTargetDAGCombine(ISD::SHL);
478 setTargetDAGCombine(ISD::SRL);
479 setTargetDAGCombine(ISD::SRA);
480 setTargetDAGCombine(ISD::SIGN_EXTEND);
481 setTargetDAGCombine(ISD::ZERO_EXTEND);
482 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000483 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000484 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000485 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000486 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
487 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000488 }
489
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000490 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000491
492 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000494
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000495 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000497
Evan Chenga8e29892007-01-19 07:51:42 +0000498 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000499 if (!Subtarget->isThumb1Only()) {
500 for (unsigned im = (unsigned)ISD::PRE_INC;
501 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setIndexedLoadAction(im, MVT::i1, Legal);
503 setIndexedLoadAction(im, MVT::i8, Legal);
504 setIndexedLoadAction(im, MVT::i16, Legal);
505 setIndexedLoadAction(im, MVT::i32, Legal);
506 setIndexedStoreAction(im, MVT::i1, Legal);
507 setIndexedStoreAction(im, MVT::i8, Legal);
508 setIndexedStoreAction(im, MVT::i16, Legal);
509 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000510 }
Evan Chenga8e29892007-01-19 07:51:42 +0000511 }
512
513 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000514 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::MUL, MVT::i64, Expand);
516 setOperationAction(ISD::MULHU, MVT::i32, Expand);
517 setOperationAction(ISD::MULHS, MVT::i32, Expand);
518 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
519 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000520 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 setOperationAction(ISD::MUL, MVT::i64, Expand);
522 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000523 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000525 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000526 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000527 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000528 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 setOperationAction(ISD::SRL, MVT::i64, Custom);
530 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000531
532 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000534 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000536 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000538
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000539 // Only ARMv6 has BSWAP.
540 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000542
Evan Chenga8e29892007-01-19 07:51:42 +0000543 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000544 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000545 // v7M has a hardware divider
546 setOperationAction(ISD::SDIV, MVT::i32, Expand);
547 setOperationAction(ISD::UDIV, MVT::i32, Expand);
548 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::SREM, MVT::i32, Expand);
550 setOperationAction(ISD::UREM, MVT::i32, Expand);
551 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
552 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000553
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
555 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
556 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
557 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000558 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000559
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000560 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000561
Evan Chenga8e29892007-01-19 07:51:42 +0000562 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setOperationAction(ISD::VASTART, MVT::Other, Custom);
564 setOperationAction(ISD::VAARG, MVT::Other, Expand);
565 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
566 setOperationAction(ISD::VAEND, MVT::Other, Expand);
567 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
568 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000569 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000570 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
571 setExceptionPointerRegister(ARM::R0);
572 setExceptionSelectorRegister(ARM::R1);
573
Evan Cheng3a1588a2010-04-15 22:20:34 +0000574 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000575 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
576 // the default expansion.
577 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000578 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000579 // membarrier needs custom lowering; the rest are legal and handled
580 // normally.
581 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
582 } else {
583 // Set them all for expansion, which will force libcalls.
584 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
585 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
586 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
587 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000588 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
589 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
590 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000591 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
592 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
593 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
594 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
595 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
596 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
604 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
605 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
606 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
607 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
608 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000609 // Since the libcalls include locking, fold in the fences
610 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000611 }
612 // 64-bit versions are always libcalls (for now)
613 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000614 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000615 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
616 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
617 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
618 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
619 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
620 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000621
Evan Cheng416941d2010-11-04 05:19:35 +0000622 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000623
Eli Friedmana2c6f452010-06-26 04:36:50 +0000624 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
625 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
627 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000628 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000630
Nate Begemand1fb5832010-08-03 21:31:55 +0000631 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000632 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
633 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000634 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000635 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
636 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000637
638 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000640 if (Subtarget->isTargetDarwin()) {
641 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
642 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000643 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000644 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000645
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::SETCC, MVT::i32, Expand);
647 setOperationAction(ISD::SETCC, MVT::f32, Expand);
648 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000649 setOperationAction(ISD::SELECT, MVT::i32, Custom);
650 setOperationAction(ISD::SELECT, MVT::f32, Custom);
651 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
653 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
654 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
657 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
658 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
659 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
660 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000661
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000662 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::FSIN, MVT::f64, Expand);
664 setOperationAction(ISD::FSIN, MVT::f32, Expand);
665 setOperationAction(ISD::FCOS, MVT::f32, Expand);
666 setOperationAction(ISD::FCOS, MVT::f64, Expand);
667 setOperationAction(ISD::FREM, MVT::f64, Expand);
668 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000669 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
671 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000672 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::FPOW, MVT::f64, Expand);
674 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000675
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000676 // Various VFP goodness
677 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000678 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
679 if (Subtarget->hasVFP2()) {
680 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
681 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
682 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
683 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
684 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000685 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000686 if (!Subtarget->hasFP16()) {
687 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
688 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000689 }
Evan Cheng110cf482008-04-01 01:50:16 +0000690 }
Evan Chenga8e29892007-01-19 07:51:42 +0000691
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000692 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000693 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000694 setTargetDAGCombine(ISD::ADD);
695 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000696 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000697
Owen Anderson080c0922010-11-05 19:27:46 +0000698 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000699 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000700 if (Subtarget->hasNEON())
701 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000702
Evan Chenga8e29892007-01-19 07:51:42 +0000703 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000704
Evan Chengf7d87ee2010-05-21 00:43:17 +0000705 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
706 setSchedulingPreference(Sched::RegPressure);
707 else
708 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000709
Evan Cheng05219282011-01-06 06:52:41 +0000710 //// temporary - rewrite interface to use type
711 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000712
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000713 // On ARM arguments smaller than 4 bytes are extended, so all arguments
714 // are at least 4 bytes aligned.
715 setMinStackArgumentAlignment(4);
716
Evan Chengfff606d2010-09-24 19:07:23 +0000717 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000718}
719
Andrew Trick32cec0a2011-01-19 02:35:27 +0000720// FIXME: It might make sense to define the representative register class as the
721// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
722// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
723// SPR's representative would be DPR_VFP2. This should work well if register
724// pressure tracking were modified such that a register use would increment the
725// pressure of the register class's representative and all of it's super
726// classes' representatives transitively. We have not implemented this because
727// of the difficulty prior to coalescing of modeling operand register classes
728// due to the common occurence of cross class copies and subregister insertions
729// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000730std::pair<const TargetRegisterClass*, uint8_t>
731ARMTargetLowering::findRepresentativeClass(EVT VT) const{
732 const TargetRegisterClass *RRC = 0;
733 uint8_t Cost = 1;
734 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000735 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000736 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000737 // Use DPR as representative register class for all floating point
738 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
739 // the cost is 1 for both f32 and f64.
740 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000741 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000742 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000743 // When NEON is used for SP, only half of the register file is available
744 // because operations that define both SP and DP results will be constrained
745 // to the VFP2 class (D0-D15). We currently model this constraint prior to
746 // coalescing by double-counting the SP regs. See the FIXME above.
747 if (Subtarget->useNEONForSinglePrecisionFP())
748 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000749 break;
750 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
751 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000752 RRC = ARM::DPRRegisterClass;
753 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000754 break;
755 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000756 RRC = ARM::DPRRegisterClass;
757 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000758 break;
759 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000760 RRC = ARM::DPRRegisterClass;
761 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000762 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000763 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000764 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000765}
766
Evan Chenga8e29892007-01-19 07:51:42 +0000767const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
768 switch (Opcode) {
769 default: return 0;
770 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000771 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000772 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000773 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
774 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000775 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000776 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
777 case ARMISD::tCALL: return "ARMISD::tCALL";
778 case ARMISD::BRCOND: return "ARMISD::BRCOND";
779 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000780 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000781 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
782 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
783 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000784 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000785 case ARMISD::CMPFP: return "ARMISD::CMPFP";
786 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000787 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000788 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
789 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000790
Jim Grosbach3482c802010-01-18 19:58:49 +0000791 case ARMISD::RBIT: return "ARMISD::RBIT";
792
Bob Wilson76a312b2010-03-19 22:51:32 +0000793 case ARMISD::FTOSI: return "ARMISD::FTOSI";
794 case ARMISD::FTOUI: return "ARMISD::FTOUI";
795 case ARMISD::SITOF: return "ARMISD::SITOF";
796 case ARMISD::UITOF: return "ARMISD::UITOF";
797
Evan Chenga8e29892007-01-19 07:51:42 +0000798 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
799 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
800 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000801
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000802 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
803 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000804
Evan Chengc5942082009-10-28 06:55:03 +0000805 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
806 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000807 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000808
Dale Johannesen51e28e62010-06-03 21:09:53 +0000809 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000810
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000811 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000812
Evan Cheng86198642009-08-07 00:34:42 +0000813 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
814
Jim Grosbach3728e962009-12-10 00:11:09 +0000815 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000816 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000817
Evan Chengdfed19f2010-11-03 06:34:55 +0000818 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
819
Bob Wilson5bafff32009-06-22 23:27:02 +0000820 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000821 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000822 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000823 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
824 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000825 case ARMISD::VCGEU: return "ARMISD::VCGEU";
826 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000827 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
828 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000829 case ARMISD::VCGTU: return "ARMISD::VCGTU";
830 case ARMISD::VTST: return "ARMISD::VTST";
831
832 case ARMISD::VSHL: return "ARMISD::VSHL";
833 case ARMISD::VSHRs: return "ARMISD::VSHRs";
834 case ARMISD::VSHRu: return "ARMISD::VSHRu";
835 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
836 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
837 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
838 case ARMISD::VSHRN: return "ARMISD::VSHRN";
839 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
840 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
841 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
842 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
843 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
844 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
845 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
846 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
847 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
848 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
849 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
850 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
851 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
852 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000853 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000854 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000855 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000856 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000857 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000858 case ARMISD::VREV64: return "ARMISD::VREV64";
859 case ARMISD::VREV32: return "ARMISD::VREV32";
860 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000861 case ARMISD::VZIP: return "ARMISD::VZIP";
862 case ARMISD::VUZP: return "ARMISD::VUZP";
863 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000864 case ARMISD::VTBL1: return "ARMISD::VTBL1";
865 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000866 case ARMISD::VMULLs: return "ARMISD::VMULLs";
867 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000868 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000869 case ARMISD::FMAX: return "ARMISD::FMAX";
870 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000871 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000872 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
873 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000874 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000875 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
876 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
877 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000878 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
879 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
880 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
881 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
882 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
883 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
884 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
885 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
886 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
887 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
888 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
889 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
890 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
891 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
892 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
893 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
894 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000895 }
896}
897
Evan Cheng06b666c2010-05-15 02:18:07 +0000898/// getRegClassFor - Return the register class that should be used for the
899/// specified value type.
900TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
901 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
902 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
903 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000904 if (Subtarget->hasNEON()) {
905 if (VT == MVT::v4i64)
906 return ARM::QQPRRegisterClass;
907 else if (VT == MVT::v8i64)
908 return ARM::QQQQPRRegisterClass;
909 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000910 return TargetLowering::getRegClassFor(VT);
911}
912
Eric Christopherab695882010-07-21 22:26:11 +0000913// Create a fast isel object.
914FastISel *
915ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
916 return ARM::createFastISel(funcInfo);
917}
918
Bill Wendlingb4202b82009-07-01 18:50:55 +0000919/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000920unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000921 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000922}
923
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000924/// getMaximalGlobalOffset - Returns the maximal possible offset which can
925/// be used for loads / stores from the global.
926unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
927 return (Subtarget->isThumb1Only() ? 127 : 4095);
928}
929
Evan Cheng1cc39842010-05-20 23:26:43 +0000930Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000931 unsigned NumVals = N->getNumValues();
932 if (!NumVals)
933 return Sched::RegPressure;
934
935 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000936 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000937 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000938 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000939 if (VT.isFloatingPoint() || VT.isVector())
940 return Sched::Latency;
941 }
Evan Chengc10f5432010-05-28 23:25:23 +0000942
943 if (!N->isMachineOpcode())
944 return Sched::RegPressure;
945
946 // Load are scheduled for latency even if there instruction itinerary
947 // is not available.
948 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
949 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000950
951 if (TID.getNumDefs() == 0)
952 return Sched::RegPressure;
953 if (!Itins->isEmpty() &&
954 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000955 return Sched::Latency;
956
Evan Cheng1cc39842010-05-20 23:26:43 +0000957 return Sched::RegPressure;
958}
959
Evan Chenga8e29892007-01-19 07:51:42 +0000960//===----------------------------------------------------------------------===//
961// Lowering Code
962//===----------------------------------------------------------------------===//
963
Evan Chenga8e29892007-01-19 07:51:42 +0000964/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
965static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
966 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000967 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000968 case ISD::SETNE: return ARMCC::NE;
969 case ISD::SETEQ: return ARMCC::EQ;
970 case ISD::SETGT: return ARMCC::GT;
971 case ISD::SETGE: return ARMCC::GE;
972 case ISD::SETLT: return ARMCC::LT;
973 case ISD::SETLE: return ARMCC::LE;
974 case ISD::SETUGT: return ARMCC::HI;
975 case ISD::SETUGE: return ARMCC::HS;
976 case ISD::SETULT: return ARMCC::LO;
977 case ISD::SETULE: return ARMCC::LS;
978 }
979}
980
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000981/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
982static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000983 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000984 CondCode2 = ARMCC::AL;
985 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000986 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000987 case ISD::SETEQ:
988 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
989 case ISD::SETGT:
990 case ISD::SETOGT: CondCode = ARMCC::GT; break;
991 case ISD::SETGE:
992 case ISD::SETOGE: CondCode = ARMCC::GE; break;
993 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000994 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000995 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
996 case ISD::SETO: CondCode = ARMCC::VC; break;
997 case ISD::SETUO: CondCode = ARMCC::VS; break;
998 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
999 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1000 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1001 case ISD::SETLT:
1002 case ISD::SETULT: CondCode = ARMCC::LT; break;
1003 case ISD::SETLE:
1004 case ISD::SETULE: CondCode = ARMCC::LE; break;
1005 case ISD::SETNE:
1006 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1007 }
Evan Chenga8e29892007-01-19 07:51:42 +00001008}
1009
Bob Wilson1f595bb2009-04-17 19:07:39 +00001010//===----------------------------------------------------------------------===//
1011// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001012//===----------------------------------------------------------------------===//
1013
1014#include "ARMGenCallingConv.inc"
1015
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001016/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1017/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001018CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001019 bool Return,
1020 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001021 switch (CC) {
1022 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001023 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001024 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001025 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001026 if (!Subtarget->isAAPCS_ABI())
1027 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1028 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1029 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1030 }
1031 // Fallthrough
1032 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001033 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001034 if (!Subtarget->isAAPCS_ABI())
1035 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1036 else if (Subtarget->hasVFP2() &&
1037 FloatABIType == FloatABI::Hard && !isVarArg)
1038 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1039 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1040 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001041 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001042 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001043 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001044 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001045 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001046 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001047 }
1048}
1049
Dan Gohman98ca4f22009-08-05 01:29:28 +00001050/// LowerCallResult - Lower the result values of a call into the
1051/// appropriate copies out of appropriate physical registers.
1052SDValue
1053ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001054 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001055 const SmallVectorImpl<ISD::InputArg> &Ins,
1056 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001057 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001058
Bob Wilson1f595bb2009-04-17 19:07:39 +00001059 // Assign locations to each value returned by this call.
1060 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001061 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001062 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001063 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001064 CCAssignFnForNode(CallConv, /* Return*/ true,
1065 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001066
1067 // Copy all of the result registers out of their specified physreg.
1068 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1069 CCValAssign VA = RVLocs[i];
1070
Bob Wilson80915242009-04-25 00:33:20 +00001071 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001073 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001074 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001075 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001076 Chain = Lo.getValue(1);
1077 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001078 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001080 InFlag);
1081 Chain = Hi.getValue(1);
1082 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001083 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001084
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 if (VA.getLocVT() == MVT::v2f64) {
1086 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1087 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1088 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001089
1090 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001091 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001092 Chain = Lo.getValue(1);
1093 InFlag = Lo.getValue(2);
1094 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001095 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001096 Chain = Hi.getValue(1);
1097 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001098 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001099 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1100 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001101 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001102 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001103 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1104 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001105 Chain = Val.getValue(1);
1106 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107 }
Bob Wilson80915242009-04-25 00:33:20 +00001108
1109 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001110 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001111 case CCValAssign::Full: break;
1112 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001113 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001114 break;
1115 }
1116
Dan Gohman98ca4f22009-08-05 01:29:28 +00001117 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118 }
1119
Dan Gohman98ca4f22009-08-05 01:29:28 +00001120 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001121}
1122
1123/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1124/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001125/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126/// a byval function parameter.
1127/// Sometimes what we are copying is the end of a larger object, the part that
1128/// does not fit in registers.
1129static SDValue
1130CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1131 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1132 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001133 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001135 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001136 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137}
1138
Bob Wilsondee46d72009-04-17 20:35:10 +00001139/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001140SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001141ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1142 SDValue StackPtr, SDValue Arg,
1143 DebugLoc dl, SelectionDAG &DAG,
1144 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001145 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146 unsigned LocMemOffset = VA.getLocMemOffset();
1147 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1148 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001149 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001150 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001151
Bob Wilson1f595bb2009-04-17 19:07:39 +00001152 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001153 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001154 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001155}
1156
Dan Gohman98ca4f22009-08-05 01:29:28 +00001157void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001158 SDValue Chain, SDValue &Arg,
1159 RegsToPassVector &RegsToPass,
1160 CCValAssign &VA, CCValAssign &NextVA,
1161 SDValue &StackPtr,
1162 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001163 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001164
Jim Grosbache5165492009-11-09 00:11:35 +00001165 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001166 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001167 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1168
1169 if (NextVA.isRegLoc())
1170 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1171 else {
1172 assert(NextVA.isMemLoc());
1173 if (StackPtr.getNode() == 0)
1174 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1177 dl, DAG, NextVA,
1178 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001179 }
1180}
1181
Dan Gohman98ca4f22009-08-05 01:29:28 +00001182/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001183/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1184/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001186ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001187 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001188 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001190 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001191 const SmallVectorImpl<ISD::InputArg> &Ins,
1192 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001193 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001194 MachineFunction &MF = DAG.getMachineFunction();
1195 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1196 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001197 // Temporarily disable tail calls so things don't break.
1198 if (!EnableARMTailCalls)
1199 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001200 if (isTailCall) {
1201 // Check if it's really possible to do a tail call.
1202 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1203 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001204 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001205 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1206 // detected sibcalls.
1207 if (isTailCall) {
1208 ++NumTailCalls;
1209 IsSibCall = true;
1210 }
1211 }
Evan Chenga8e29892007-01-19 07:51:42 +00001212
Bob Wilson1f595bb2009-04-17 19:07:39 +00001213 // Analyze operands of the call, assigning locations to each operand.
1214 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001215 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1216 *DAG.getContext());
1217 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001218 CCAssignFnForNode(CallConv, /* Return*/ false,
1219 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001220
Bob Wilson1f595bb2009-04-17 19:07:39 +00001221 // Get a count of how many bytes are to be pushed on the stack.
1222 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001223
Dale Johannesen51e28e62010-06-03 21:09:53 +00001224 // For tail calls, memory operands are available in our caller's stack.
1225 if (IsSibCall)
1226 NumBytes = 0;
1227
Evan Chenga8e29892007-01-19 07:51:42 +00001228 // Adjust the stack pointer for the new arguments...
1229 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001230 if (!IsSibCall)
1231 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001232
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001233 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001234
Bob Wilson5bafff32009-06-22 23:27:02 +00001235 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001237
Bob Wilson1f595bb2009-04-17 19:07:39 +00001238 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001239 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001240 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1241 i != e;
1242 ++i, ++realArgIdx) {
1243 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001244 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001246 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001247
Bob Wilson1f595bb2009-04-17 19:07:39 +00001248 // Promote the value if needed.
1249 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001250 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251 case CCValAssign::Full: break;
1252 case CCValAssign::SExt:
1253 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1254 break;
1255 case CCValAssign::ZExt:
1256 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1257 break;
1258 case CCValAssign::AExt:
1259 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1260 break;
1261 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001262 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001263 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001264 }
1265
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001266 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001267 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 if (VA.getLocVT() == MVT::v2f64) {
1269 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1270 DAG.getConstant(0, MVT::i32));
1271 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1272 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001273
Dan Gohman98ca4f22009-08-05 01:29:28 +00001274 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001275 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1276
1277 VA = ArgLocs[++i]; // skip ahead to next loc
1278 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001279 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001280 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1281 } else {
1282 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001283
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1285 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001286 }
1287 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001289 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001290 }
1291 } else if (VA.isRegLoc()) {
1292 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsf222e592011-02-28 17:17:53 +00001293 } else if (!IsSibCall || isByVal) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001294 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001295
Dan Gohman98ca4f22009-08-05 01:29:28 +00001296 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1297 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001298 }
Evan Chenga8e29892007-01-19 07:51:42 +00001299 }
1300
1301 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001302 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001303 &MemOpChains[0], MemOpChains.size());
1304
1305 // Build a sequence of copy-to-reg nodes chained together with token chain
1306 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001308 // Tail call byval lowering might overwrite argument registers so in case of
1309 // tail call optimization the copies to registers are lowered later.
1310 if (!isTailCall)
1311 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1312 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1313 RegsToPass[i].second, InFlag);
1314 InFlag = Chain.getValue(1);
1315 }
Evan Chenga8e29892007-01-19 07:51:42 +00001316
Dale Johannesen51e28e62010-06-03 21:09:53 +00001317 // For tail calls lower the arguments to the 'real' stack slot.
1318 if (isTailCall) {
1319 // Force all the incoming stack arguments to be loaded from the stack
1320 // before any new outgoing arguments are stored to the stack, because the
1321 // outgoing stack slots may alias the incoming argument stack slots, and
1322 // the alias isn't otherwise explicit. This is slightly more conservative
1323 // than necessary, because it means that each store effectively depends
1324 // on every argument instead of just those arguments it would clobber.
1325
1326 // Do not flag preceeding copytoreg stuff together with the following stuff.
1327 InFlag = SDValue();
1328 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1329 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1330 RegsToPass[i].second, InFlag);
1331 InFlag = Chain.getValue(1);
1332 }
1333 InFlag =SDValue();
1334 }
1335
Bill Wendling056292f2008-09-16 21:48:12 +00001336 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1337 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1338 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001339 bool isDirect = false;
1340 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001341 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001342 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001343
1344 if (EnableARMLongCalls) {
1345 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1346 && "long-calls with non-static relocation model!");
1347 // Handle a global address or an external symbol. If it's not one of
1348 // those, the target's already in a register, so we don't need to do
1349 // anything extra.
1350 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001351 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001352 // 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(GV,
1355 ARMPCLabelIndex,
1356 ARMCP::CPValue, 0);
1357 // Get the address of the callee into a register
1358 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1359 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1360 Callee = DAG.getLoad(getPointerTy(), dl,
1361 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001362 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001363 false, false, 0);
1364 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1365 const char *Sym = S->getSymbol();
1366
1367 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001368 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001369 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1370 Sym, ARMPCLabelIndex, 0);
1371 // Get the address of the callee into a register
1372 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1373 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1374 Callee = DAG.getLoad(getPointerTy(), dl,
1375 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001376 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001377 false, false, 0);
1378 }
1379 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001380 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001381 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001382 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001383 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001384 getTargetMachine().getRelocationModel() != Reloc::Static;
1385 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001386 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001387 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001388 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001389 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001390 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001391 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001392 ARMPCLabelIndex,
1393 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001394 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001395 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001396 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001397 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001398 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001399 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001400 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001401 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001402 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001403 } else {
1404 // On ELF targets for PIC code, direct calls should go through the PLT
1405 unsigned OpFlags = 0;
1406 if (Subtarget->isTargetELF() &&
1407 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1408 OpFlags = ARMII::MO_PLT;
1409 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1410 }
Bill Wendling056292f2008-09-16 21:48:12 +00001411 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001412 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001413 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001414 getTargetMachine().getRelocationModel() != Reloc::Static;
1415 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001416 // tBX takes a register source operand.
1417 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001418 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001419 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001420 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001421 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001422 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001424 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001425 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001426 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001427 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001428 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001429 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001430 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001431 } else {
1432 unsigned OpFlags = 0;
1433 // On ELF targets for PIC code, direct calls should go through the PLT
1434 if (Subtarget->isTargetELF() &&
1435 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1436 OpFlags = ARMII::MO_PLT;
1437 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1438 }
Evan Chenga8e29892007-01-19 07:51:42 +00001439 }
1440
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001441 // FIXME: handle tail calls differently.
1442 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001443 if (Subtarget->isThumb()) {
1444 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001445 CallOpc = ARMISD::CALL_NOLINK;
1446 else
1447 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1448 } else {
1449 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001450 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1451 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001452 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001453
Dan Gohman475871a2008-07-27 21:46:04 +00001454 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001455 Ops.push_back(Chain);
1456 Ops.push_back(Callee);
1457
1458 // Add argument registers to the end of the list so that they are known live
1459 // into the call.
1460 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1461 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1462 RegsToPass[i].second.getValueType()));
1463
Gabor Greifba36cb52008-08-28 21:40:38 +00001464 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001465 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001466
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001467 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001468 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001469 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001470
Duncan Sands4bdcb612008-07-02 17:40:58 +00001471 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001472 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001473 InFlag = Chain.getValue(1);
1474
Chris Lattnere563bbc2008-10-11 22:08:30 +00001475 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1476 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001477 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001478 InFlag = Chain.getValue(1);
1479
Bob Wilson1f595bb2009-04-17 19:07:39 +00001480 // Handle result values, copying them out of physregs into vregs that we
1481 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1483 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001484}
1485
Stuart Hastingsf222e592011-02-28 17:17:53 +00001486/// HandleByVal - Every parameter *after* a byval parameter is passed
1487/// on the stack. Confiscate all the parameter registers to insure
1488/// this.
1489void
1490llvm::ARMTargetLowering::HandleByVal(CCState *State) const {
1491 static const unsigned RegList1[] = {
1492 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1493 };
1494 do {} while (State->AllocateReg(RegList1, 4));
1495}
1496
Dale Johannesen51e28e62010-06-03 21:09:53 +00001497/// MatchingStackOffset - Return true if the given stack call argument is
1498/// already available in the same position (relatively) of the caller's
1499/// incoming argument stack.
1500static
1501bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1502 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1503 const ARMInstrInfo *TII) {
1504 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1505 int FI = INT_MAX;
1506 if (Arg.getOpcode() == ISD::CopyFromReg) {
1507 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001508 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001509 return false;
1510 MachineInstr *Def = MRI->getVRegDef(VR);
1511 if (!Def)
1512 return false;
1513 if (!Flags.isByVal()) {
1514 if (!TII->isLoadFromStackSlot(Def, FI))
1515 return false;
1516 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001517 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001518 }
1519 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1520 if (Flags.isByVal())
1521 // ByVal argument is passed in as a pointer but it's now being
1522 // dereferenced. e.g.
1523 // define @foo(%struct.X* %A) {
1524 // tail call @bar(%struct.X* byval %A)
1525 // }
1526 return false;
1527 SDValue Ptr = Ld->getBasePtr();
1528 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1529 if (!FINode)
1530 return false;
1531 FI = FINode->getIndex();
1532 } else
1533 return false;
1534
1535 assert(FI != INT_MAX);
1536 if (!MFI->isFixedObjectIndex(FI))
1537 return false;
1538 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1539}
1540
1541/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1542/// for tail call optimization. Targets which want to do tail call
1543/// optimization should implement this function.
1544bool
1545ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1546 CallingConv::ID CalleeCC,
1547 bool isVarArg,
1548 bool isCalleeStructRet,
1549 bool isCallerStructRet,
1550 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001551 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001552 const SmallVectorImpl<ISD::InputArg> &Ins,
1553 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001554 const Function *CallerF = DAG.getMachineFunction().getFunction();
1555 CallingConv::ID CallerCC = CallerF->getCallingConv();
1556 bool CCMatch = CallerCC == CalleeCC;
1557
1558 // Look for obvious safe cases to perform tail call optimization that do not
1559 // require ABI changes. This is what gcc calls sibcall.
1560
Jim Grosbach7616b642010-06-16 23:45:49 +00001561 // Do not sibcall optimize vararg calls unless the call site is not passing
1562 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001563 if (isVarArg && !Outs.empty())
1564 return false;
1565
1566 // Also avoid sibcall optimization if either caller or callee uses struct
1567 // return semantics.
1568 if (isCalleeStructRet || isCallerStructRet)
1569 return false;
1570
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001571 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001572 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001573 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1574 // LR. This means if we need to reload LR, it takes an extra instructions,
1575 // which outweighs the value of the tail call; but here we don't know yet
1576 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001577 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001578 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001579
1580 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1581 // but we need to make sure there are enough registers; the only valid
1582 // registers are the 4 used for parameters. We don't currently do this
1583 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001584 if (Subtarget->isThumb1Only())
1585 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001586
Dale Johannesen51e28e62010-06-03 21:09:53 +00001587 // If the calling conventions do not match, then we'd better make sure the
1588 // results are returned in the same way as what the caller expects.
1589 if (!CCMatch) {
1590 SmallVector<CCValAssign, 16> RVLocs1;
1591 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1592 RVLocs1, *DAG.getContext());
1593 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1594
1595 SmallVector<CCValAssign, 16> RVLocs2;
1596 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1597 RVLocs2, *DAG.getContext());
1598 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1599
1600 if (RVLocs1.size() != RVLocs2.size())
1601 return false;
1602 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1603 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1604 return false;
1605 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1606 return false;
1607 if (RVLocs1[i].isRegLoc()) {
1608 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1609 return false;
1610 } else {
1611 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1612 return false;
1613 }
1614 }
1615 }
1616
1617 // If the callee takes no arguments then go on to check the results of the
1618 // call.
1619 if (!Outs.empty()) {
1620 // Check if stack adjustment is needed. For now, do not do this if any
1621 // argument is passed on the stack.
1622 SmallVector<CCValAssign, 16> ArgLocs;
1623 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1624 ArgLocs, *DAG.getContext());
1625 CCInfo.AnalyzeCallOperands(Outs,
1626 CCAssignFnForNode(CalleeCC, false, isVarArg));
1627 if (CCInfo.getNextStackOffset()) {
1628 MachineFunction &MF = DAG.getMachineFunction();
1629
1630 // Check if the arguments are already laid out in the right way as
1631 // the caller's fixed stack objects.
1632 MachineFrameInfo *MFI = MF.getFrameInfo();
1633 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1634 const ARMInstrInfo *TII =
1635 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001636 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1637 i != e;
1638 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001639 CCValAssign &VA = ArgLocs[i];
1640 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001641 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001642 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001643 if (VA.getLocInfo() == CCValAssign::Indirect)
1644 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001645 if (VA.needsCustom()) {
1646 // f64 and vector types are split into multiple registers or
1647 // register/stack-slot combinations. The types will not match
1648 // the registers; give up on memory f64 refs until we figure
1649 // out what to do about this.
1650 if (!VA.isRegLoc())
1651 return false;
1652 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001653 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001654 if (RegVT == MVT::v2f64) {
1655 if (!ArgLocs[++i].isRegLoc())
1656 return false;
1657 if (!ArgLocs[++i].isRegLoc())
1658 return false;
1659 }
1660 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001661 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1662 MFI, MRI, TII))
1663 return false;
1664 }
1665 }
1666 }
1667 }
1668
1669 return true;
1670}
1671
Dan Gohman98ca4f22009-08-05 01:29:28 +00001672SDValue
1673ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001674 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001675 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001676 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001677 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001678
Bob Wilsondee46d72009-04-17 20:35:10 +00001679 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001680 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001681
Bob Wilsondee46d72009-04-17 20:35:10 +00001682 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001683 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1684 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001685
Dan Gohman98ca4f22009-08-05 01:29:28 +00001686 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001687 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1688 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001689
1690 // If this is the first return lowered for this function, add
1691 // the regs to the liveout set for the function.
1692 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1693 for (unsigned i = 0; i != RVLocs.size(); ++i)
1694 if (RVLocs[i].isRegLoc())
1695 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001696 }
1697
Bob Wilson1f595bb2009-04-17 19:07:39 +00001698 SDValue Flag;
1699
1700 // Copy the result values into the output registers.
1701 for (unsigned i = 0, realRVLocIdx = 0;
1702 i != RVLocs.size();
1703 ++i, ++realRVLocIdx) {
1704 CCValAssign &VA = RVLocs[i];
1705 assert(VA.isRegLoc() && "Can only return in registers!");
1706
Dan Gohmanc9403652010-07-07 15:54:55 +00001707 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001708
1709 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001710 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001711 case CCValAssign::Full: break;
1712 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001713 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001714 break;
1715 }
1716
Bob Wilson1f595bb2009-04-17 19:07:39 +00001717 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001719 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001720 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1721 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001722 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001724
1725 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1726 Flag = Chain.getValue(1);
1727 VA = RVLocs[++i]; // skip ahead to next loc
1728 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1729 HalfGPRs.getValue(1), Flag);
1730 Flag = Chain.getValue(1);
1731 VA = RVLocs[++i]; // skip ahead to next loc
1732
1733 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001734 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1735 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001736 }
1737 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1738 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001739 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001740 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001741 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001742 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001743 VA = RVLocs[++i]; // skip ahead to next loc
1744 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1745 Flag);
1746 } else
1747 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1748
Bob Wilsondee46d72009-04-17 20:35:10 +00001749 // Guarantee that all emitted copies are
1750 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001751 Flag = Chain.getValue(1);
1752 }
1753
1754 SDValue result;
1755 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001756 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001757 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001759
1760 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001761}
1762
Evan Cheng3d2125c2010-11-30 23:55:39 +00001763bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1764 if (N->getNumValues() != 1)
1765 return false;
1766 if (!N->hasNUsesOfValue(1, 0))
1767 return false;
1768
1769 unsigned NumCopies = 0;
1770 SDNode* Copies[2];
1771 SDNode *Use = *N->use_begin();
1772 if (Use->getOpcode() == ISD::CopyToReg) {
1773 Copies[NumCopies++] = Use;
1774 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1775 // f64 returned in a pair of GPRs.
1776 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1777 UI != UE; ++UI) {
1778 if (UI->getOpcode() != ISD::CopyToReg)
1779 return false;
1780 Copies[UI.getUse().getResNo()] = *UI;
1781 ++NumCopies;
1782 }
1783 } else if (Use->getOpcode() == ISD::BITCAST) {
1784 // f32 returned in a single GPR.
1785 if (!Use->hasNUsesOfValue(1, 0))
1786 return false;
1787 Use = *Use->use_begin();
1788 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1789 return false;
1790 Copies[NumCopies++] = Use;
1791 } else {
1792 return false;
1793 }
1794
1795 if (NumCopies != 1 && NumCopies != 2)
1796 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001797
1798 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001799 for (unsigned i = 0; i < NumCopies; ++i) {
1800 SDNode *Copy = Copies[i];
1801 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1802 UI != UE; ++UI) {
1803 if (UI->getOpcode() == ISD::CopyToReg) {
1804 SDNode *Use = *UI;
1805 if (Use == Copies[0] || Use == Copies[1])
1806 continue;
1807 return false;
1808 }
1809 if (UI->getOpcode() != ARMISD::RET_FLAG)
1810 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001811 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001812 }
1813 }
1814
Evan Cheng1bf891a2010-12-01 22:59:46 +00001815 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001816}
1817
Evan Cheng485fafc2011-03-21 01:19:09 +00001818bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1819 if (!EnableARMTailCalls)
1820 return false;
1821
1822 if (!CI->isTailCall())
1823 return false;
1824
1825 return !Subtarget->isThumb1Only();
1826}
1827
Bob Wilsonb62d2572009-11-03 00:02:05 +00001828// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1829// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1830// one of the above mentioned nodes. It has to be wrapped because otherwise
1831// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1832// be used to form addressing mode. These wrapped nodes will be selected
1833// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001834static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001835 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001836 // FIXME there is no actual debug info here
1837 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001838 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001840 if (CP->isMachineConstantPoolEntry())
1841 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1842 CP->getAlignment());
1843 else
1844 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1845 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001846 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001847}
1848
Jim Grosbache1102ca2010-07-19 17:20:38 +00001849unsigned ARMTargetLowering::getJumpTableEncoding() const {
1850 return MachineJumpTableInfo::EK_Inline;
1851}
1852
Dan Gohmand858e902010-04-17 15:26:15 +00001853SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1854 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001855 MachineFunction &MF = DAG.getMachineFunction();
1856 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1857 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001858 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001859 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001860 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001861 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1862 SDValue CPAddr;
1863 if (RelocM == Reloc::Static) {
1864 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1865 } else {
1866 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001867 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001868 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1869 ARMCP::CPBlockAddress,
1870 PCAdj);
1871 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1872 }
1873 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1874 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001875 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001876 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001877 if (RelocM == Reloc::Static)
1878 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001879 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001880 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001881}
1882
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001883// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001884SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001885ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001886 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001887 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001888 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001889 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001890 MachineFunction &MF = DAG.getMachineFunction();
1891 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001892 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001893 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001894 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001895 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001896 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001898 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001899 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001900 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001901 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001902
Evan Chenge7e0d622009-11-06 22:24:13 +00001903 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001904 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001905
1906 // call __tls_get_addr.
1907 ArgListTy Args;
1908 ArgListEntry Entry;
1909 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001910 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001911 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001912 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001913 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001914 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1915 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001916 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001917 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001918 return CallResult.first;
1919}
1920
1921// Lower ISD::GlobalTLSAddress using the "initial exec" or
1922// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001923SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001924ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001925 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001926 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001927 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001928 SDValue Offset;
1929 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001930 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001931 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001932 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001933
Chris Lattner4fb63d02009-07-15 04:12:33 +00001934 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001935 MachineFunction &MF = DAG.getMachineFunction();
1936 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001937 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001938 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001939 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1940 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001941 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001942 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001943 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001945 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001946 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001947 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001948 Chain = Offset.getValue(1);
1949
Evan Chenge7e0d622009-11-06 22:24:13 +00001950 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001951 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001952
Evan Cheng9eda6892009-10-31 03:39:36 +00001953 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001954 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001955 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001956 } else {
1957 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001958 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001959 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001961 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001962 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001963 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001964 }
1965
1966 // The address of the thread local variable is the add of the thread
1967 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001968 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001969}
1970
Dan Gohman475871a2008-07-27 21:46:04 +00001971SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001972ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001973 // TODO: implement the "local dynamic" model
1974 assert(Subtarget->isTargetELF() &&
1975 "TLS not implemented for non-ELF targets");
1976 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1977 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1978 // otherwise use the "Local Exec" TLS Model
1979 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1980 return LowerToTLSGeneralDynamicModel(GA, DAG);
1981 else
1982 return LowerToTLSExecModels(GA, DAG);
1983}
1984
Dan Gohman475871a2008-07-27 21:46:04 +00001985SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001986 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001987 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001988 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001989 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001990 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1991 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001992 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001993 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001994 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001995 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001997 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001998 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001999 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002000 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002001 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002002 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002003 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002004 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002005 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002006 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002007 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002008 }
2009
2010 // If we have T2 ops, we can materialize the address directly via movt/movw
2011 // pair. This is always cheaper.
2012 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002013 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002014 // FIXME: Once remat is capable of dealing with instructions with register
2015 // operands, expand this into two nodes.
2016 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2017 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002018 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002019 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2020 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2021 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2022 MachinePointerInfo::getConstantPool(),
2023 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002024 }
2025}
2026
Dan Gohman475871a2008-07-27 21:46:04 +00002027SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002028 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002029 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002030 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002031 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002032 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002033 MachineFunction &MF = DAG.getMachineFunction();
2034 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2035
2036 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002037 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002038 // FIXME: Once remat is capable of dealing with instructions with register
2039 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002040 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002041 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2042 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2043
Evan Cheng53519f02011-01-21 18:55:51 +00002044 unsigned Wrapper = (RelocM == Reloc::PIC_)
2045 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2046 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002047 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002048 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2049 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2050 MachinePointerInfo::getGOT(), false, false, 0);
2051 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002052 }
2053
2054 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002055 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002056 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002057 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002058 } else {
2059 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002060 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2061 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002062 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002063 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002064 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002066
Evan Cheng9eda6892009-10-31 03:39:36 +00002067 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002068 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002069 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002070 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002071
2072 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002073 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002074 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002075 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002076
Evan Cheng63476a82009-09-03 07:04:02 +00002077 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002078 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002079 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002080
2081 return Result;
2082}
2083
Dan Gohman475871a2008-07-27 21:46:04 +00002084SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002085 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002086 assert(Subtarget->isTargetELF() &&
2087 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002088 MachineFunction &MF = DAG.getMachineFunction();
2089 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002090 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002091 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002092 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002093 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002094 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2095 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002096 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002097 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002098 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002099 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002100 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002101 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002102 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002103 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002104}
2105
Jim Grosbach0e0da732009-05-12 23:59:14 +00002106SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002107ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2108 const {
2109 DebugLoc dl = Op.getDebugLoc();
2110 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendlingf05b1dc2011-04-05 01:37:43 +00002111 Op.getOperand(0));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002112}
2113
2114SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002115ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2116 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002117 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002118 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2119 Op.getOperand(1), Val);
2120}
2121
2122SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002123ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2124 DebugLoc dl = Op.getDebugLoc();
2125 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2126 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2127}
2128
2129SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002130ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002131 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002132 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002133 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002134 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002135 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002136 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002137 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002138 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2139 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002140 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002141 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002142 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002143 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002144 EVT PtrVT = getPointerTy();
2145 DebugLoc dl = Op.getDebugLoc();
2146 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2147 SDValue CPAddr;
2148 unsigned PCAdj = (RelocM != Reloc::PIC_)
2149 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002150 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002151 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2152 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002153 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002154 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002155 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002156 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002157 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002158 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002159
2160 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002161 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002162 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2163 }
2164 return Result;
2165 }
Evan Cheng92e39162011-03-29 23:06:19 +00002166 case Intrinsic::arm_neon_vmulls:
2167 case Intrinsic::arm_neon_vmullu: {
2168 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2169 ? ARMISD::VMULLs : ARMISD::VMULLu;
2170 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2171 Op.getOperand(1), Op.getOperand(2));
2172 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002173 }
2174}
2175
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002176static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002177 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002178 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002179 if (!Subtarget->hasDataBarrier()) {
2180 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2181 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2182 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002183 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002184 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002185 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002186 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002187 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002188
2189 SDValue Op5 = Op.getOperand(5);
2190 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2191 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2192 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2193 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2194
2195 ARM_MB::MemBOpt DMBOpt;
2196 if (isDeviceBarrier)
2197 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2198 else
2199 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2200 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2201 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002202}
2203
Evan Chengdfed19f2010-11-03 06:34:55 +00002204static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2205 const ARMSubtarget *Subtarget) {
2206 // ARM pre v5TE and Thumb1 does not have preload instructions.
2207 if (!(Subtarget->isThumb2() ||
2208 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2209 // Just preserve the chain.
2210 return Op.getOperand(0);
2211
2212 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002213 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2214 if (!isRead &&
2215 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2216 // ARMv7 with MP extension has PLDW.
2217 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002218
2219 if (Subtarget->isThumb())
2220 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002221 isRead = ~isRead & 1;
2222 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002223
Evan Cheng416941d2010-11-04 05:19:35 +00002224 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002225 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002226 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2227 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002228}
2229
Dan Gohman1e93df62010-04-17 14:41:14 +00002230static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2231 MachineFunction &MF = DAG.getMachineFunction();
2232 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2233
Evan Chenga8e29892007-01-19 07:51:42 +00002234 // vastart just stores the address of the VarArgsFrameIndex slot into the
2235 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002236 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002237 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002238 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002239 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002240 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2241 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002242}
2243
Dan Gohman475871a2008-07-27 21:46:04 +00002244SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002245ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2246 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002247 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002248 MachineFunction &MF = DAG.getMachineFunction();
2249 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2250
2251 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002252 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002253 RC = ARM::tGPRRegisterClass;
2254 else
2255 RC = ARM::GPRRegisterClass;
2256
2257 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002258 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002259 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002260
2261 SDValue ArgValue2;
2262 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002263 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002264 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002265
2266 // Create load node to retrieve arguments from the stack.
2267 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002268 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002269 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002270 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002271 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002272 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002273 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002274 }
2275
Jim Grosbache5165492009-11-09 00:11:35 +00002276 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002277}
2278
2279SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002280ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002281 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002282 const SmallVectorImpl<ISD::InputArg>
2283 &Ins,
2284 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002285 SmallVectorImpl<SDValue> &InVals)
2286 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002287
Bob Wilson1f595bb2009-04-17 19:07:39 +00002288 MachineFunction &MF = DAG.getMachineFunction();
2289 MachineFrameInfo *MFI = MF.getFrameInfo();
2290
Bob Wilson1f595bb2009-04-17 19:07:39 +00002291 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2292
2293 // Assign locations to all of the incoming arguments.
2294 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002295 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2296 *DAG.getContext());
2297 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002298 CCAssignFnForNode(CallConv, /* Return*/ false,
2299 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002300
2301 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002302 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002303
Stuart Hastingsf222e592011-02-28 17:17:53 +00002304 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002305 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2306 CCValAssign &VA = ArgLocs[i];
2307
Bob Wilsondee46d72009-04-17 20:35:10 +00002308 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002309 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002310 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002311
Bob Wilson1f595bb2009-04-17 19:07:39 +00002312 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002313 // f64 and vector types are split up into multiple registers or
2314 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002315 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002316 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002317 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002318 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002319 SDValue ArgValue2;
2320 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002321 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002322 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2323 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002324 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002325 false, false, 0);
2326 } else {
2327 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2328 Chain, DAG, dl);
2329 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002330 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2331 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002332 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002333 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002334 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2335 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002336 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002337
Bob Wilson5bafff32009-06-22 23:27:02 +00002338 } else {
2339 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002340
Owen Anderson825b72b2009-08-11 20:47:22 +00002341 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002342 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002343 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002344 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002345 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002346 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002347 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002348 RC = (AFI->isThumb1OnlyFunction() ?
2349 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002350 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002351 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002352
2353 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002354 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002355 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002356 }
2357
2358 // If this is an 8 or 16-bit value, it is really passed promoted
2359 // to 32 bits. Insert an assert[sz]ext to capture this, then
2360 // truncate to the right size.
2361 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002362 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002363 case CCValAssign::Full: break;
2364 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002365 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002366 break;
2367 case CCValAssign::SExt:
2368 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2369 DAG.getValueType(VA.getValVT()));
2370 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2371 break;
2372 case CCValAssign::ZExt:
2373 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2374 DAG.getValueType(VA.getValVT()));
2375 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2376 break;
2377 }
2378
Dan Gohman98ca4f22009-08-05 01:29:28 +00002379 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002380
2381 } else { // VA.isRegLoc()
2382
2383 // sanity check
2384 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002385 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002386
Stuart Hastingsf222e592011-02-28 17:17:53 +00002387 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002388
Stuart Hastingsf222e592011-02-28 17:17:53 +00002389 // Some Ins[] entries become multiple ArgLoc[] entries.
2390 // Process them only once.
2391 if (index != lastInsIndex)
2392 {
2393 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2394 // FIXME: For now, all byval parameter objects are marked mutable. This can be
2395 // changed with more analysis.
2396 // In case of tail call optimization mark all arguments mutable. Since they
2397 // could be overwritten by lowering of arguments in case of a tail call.
2398 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00002399 unsigned Bytes = Flags.getByValSize();
2400 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
2401 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002402 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2403 } else {
2404 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2405 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002406
Stuart Hastingsf222e592011-02-28 17:17:53 +00002407 // Create load nodes to retrieve arguments from the stack.
2408 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2409 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2410 MachinePointerInfo::getFixedStack(FI),
2411 false, false, 0));
2412 }
2413 lastInsIndex = index;
2414 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002415 }
2416 }
2417
2418 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002419 if (isVarArg) {
2420 static const unsigned GPRArgRegs[] = {
2421 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2422 };
2423
Bob Wilsondee46d72009-04-17 20:35:10 +00002424 unsigned NumGPRs = CCInfo.getFirstUnallocated
2425 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002426
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002427 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002428 unsigned VARegSize = (4 - NumGPRs) * 4;
2429 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002430 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002431 if (VARegSaveSize) {
2432 // If this function is vararg, store any remaining integer argument regs
2433 // to their spots on the stack so that they may be loaded by deferencing
2434 // the result of va_next.
2435 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002436 AFI->setVarArgsFrameIndex(
2437 MFI->CreateFixedObject(VARegSaveSize,
2438 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002439 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002440 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2441 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002442
Dan Gohman475871a2008-07-27 21:46:04 +00002443 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002444 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002445 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002446 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002447 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002448 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002449 RC = ARM::GPRRegisterClass;
2450
Devang Patel68e6bee2011-02-21 23:21:26 +00002451 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002452 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002453 SDValue Store =
2454 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002455 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2456 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002457 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002458 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002459 DAG.getConstant(4, getPointerTy()));
2460 }
2461 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002462 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002464 } else
2465 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002466 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002467 }
2468
Dan Gohman98ca4f22009-08-05 01:29:28 +00002469 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002470}
2471
2472/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002473static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002474 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002475 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002476 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002477 // Maybe this has already been legalized into the constant pool?
2478 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002479 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002480 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002481 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002482 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002483 }
2484 }
2485 return false;
2486}
2487
Evan Chenga8e29892007-01-19 07:51:42 +00002488/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2489/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002490SDValue
2491ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002492 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002493 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002494 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002495 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002496 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002497 // Constant does not fit, try adjusting it by one?
2498 switch (CC) {
2499 default: break;
2500 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002501 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002502 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002503 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002504 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002505 }
2506 break;
2507 case ISD::SETULT:
2508 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002509 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002510 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002512 }
2513 break;
2514 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002515 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002516 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002517 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002518 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002519 }
2520 break;
2521 case ISD::SETULE:
2522 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002523 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002524 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002525 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002526 }
2527 break;
2528 }
2529 }
2530 }
2531
2532 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002533 ARMISD::NodeType CompareType;
2534 switch (CondCode) {
2535 default:
2536 CompareType = ARMISD::CMP;
2537 break;
2538 case ARMCC::EQ:
2539 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002540 // Uses only Z Flag
2541 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002542 break;
2543 }
Evan Cheng218977b2010-07-13 19:27:42 +00002544 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002545 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002546}
2547
2548/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002549SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002550ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002551 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002552 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002553 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002554 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002555 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002556 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2557 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002558}
2559
Bob Wilson79f56c92011-03-08 01:17:20 +00002560/// duplicateCmp - Glue values can have only one use, so this function
2561/// duplicates a comparison node.
2562SDValue
2563ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2564 unsigned Opc = Cmp.getOpcode();
2565 DebugLoc DL = Cmp.getDebugLoc();
2566 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2567 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2568
2569 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2570 Cmp = Cmp.getOperand(0);
2571 Opc = Cmp.getOpcode();
2572 if (Opc == ARMISD::CMPFP)
2573 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2574 else {
2575 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2576 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2577 }
2578 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2579}
2580
Bill Wendlingde2b1512010-08-11 08:43:16 +00002581SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2582 SDValue Cond = Op.getOperand(0);
2583 SDValue SelectTrue = Op.getOperand(1);
2584 SDValue SelectFalse = Op.getOperand(2);
2585 DebugLoc dl = Op.getDebugLoc();
2586
2587 // Convert:
2588 //
2589 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2590 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2591 //
2592 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2593 const ConstantSDNode *CMOVTrue =
2594 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2595 const ConstantSDNode *CMOVFalse =
2596 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2597
2598 if (CMOVTrue && CMOVFalse) {
2599 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2600 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2601
2602 SDValue True;
2603 SDValue False;
2604 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2605 True = SelectTrue;
2606 False = SelectFalse;
2607 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2608 True = SelectFalse;
2609 False = SelectTrue;
2610 }
2611
2612 if (True.getNode() && False.getNode()) {
2613 EVT VT = Cond.getValueType();
2614 SDValue ARMcc = Cond.getOperand(2);
2615 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002616 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002617 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2618 }
2619 }
2620 }
2621
2622 return DAG.getSelectCC(dl, Cond,
2623 DAG.getConstant(0, Cond.getValueType()),
2624 SelectTrue, SelectFalse, ISD::SETNE);
2625}
2626
Dan Gohmand858e902010-04-17 15:26:15 +00002627SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002628 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002629 SDValue LHS = Op.getOperand(0);
2630 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002631 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002632 SDValue TrueVal = Op.getOperand(2);
2633 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002634 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002635
Owen Anderson825b72b2009-08-11 20:47:22 +00002636 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002637 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002638 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002639 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2640 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002641 }
2642
2643 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002644 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002645
Evan Cheng218977b2010-07-13 19:27:42 +00002646 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2647 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002648 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002649 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002650 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002651 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002652 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002653 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002654 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002655 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002656 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002657 }
2658 return Result;
2659}
2660
Evan Cheng218977b2010-07-13 19:27:42 +00002661/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2662/// to morph to an integer compare sequence.
2663static bool canChangeToInt(SDValue Op, bool &SeenZero,
2664 const ARMSubtarget *Subtarget) {
2665 SDNode *N = Op.getNode();
2666 if (!N->hasOneUse())
2667 // Otherwise it requires moving the value from fp to integer registers.
2668 return false;
2669 if (!N->getNumValues())
2670 return false;
2671 EVT VT = Op.getValueType();
2672 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2673 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2674 // vmrs are very slow, e.g. cortex-a8.
2675 return false;
2676
2677 if (isFloatingPointZero(Op)) {
2678 SeenZero = true;
2679 return true;
2680 }
2681 return ISD::isNormalLoad(N);
2682}
2683
2684static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2685 if (isFloatingPointZero(Op))
2686 return DAG.getConstant(0, MVT::i32);
2687
2688 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2689 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002690 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002691 Ld->isVolatile(), Ld->isNonTemporal(),
2692 Ld->getAlignment());
2693
2694 llvm_unreachable("Unknown VFP cmp argument!");
2695}
2696
2697static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2698 SDValue &RetVal1, SDValue &RetVal2) {
2699 if (isFloatingPointZero(Op)) {
2700 RetVal1 = DAG.getConstant(0, MVT::i32);
2701 RetVal2 = DAG.getConstant(0, MVT::i32);
2702 return;
2703 }
2704
2705 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2706 SDValue Ptr = Ld->getBasePtr();
2707 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2708 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002709 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002710 Ld->isVolatile(), Ld->isNonTemporal(),
2711 Ld->getAlignment());
2712
2713 EVT PtrType = Ptr.getValueType();
2714 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2715 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2716 PtrType, Ptr, DAG.getConstant(4, PtrType));
2717 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2718 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002719 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002720 Ld->isVolatile(), Ld->isNonTemporal(),
2721 NewAlign);
2722 return;
2723 }
2724
2725 llvm_unreachable("Unknown VFP cmp argument!");
2726}
2727
2728/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2729/// f32 and even f64 comparisons to integer ones.
2730SDValue
2731ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2732 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002733 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002734 SDValue LHS = Op.getOperand(2);
2735 SDValue RHS = Op.getOperand(3);
2736 SDValue Dest = Op.getOperand(4);
2737 DebugLoc dl = Op.getDebugLoc();
2738
2739 bool SeenZero = false;
2740 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2741 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002742 // If one of the operand is zero, it's safe to ignore the NaN case since
2743 // we only care about equality comparisons.
2744 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002745 // If unsafe fp math optimization is enabled and there are no other uses of
2746 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002747 // to an integer comparison.
2748 if (CC == ISD::SETOEQ)
2749 CC = ISD::SETEQ;
2750 else if (CC == ISD::SETUNE)
2751 CC = ISD::SETNE;
2752
2753 SDValue ARMcc;
2754 if (LHS.getValueType() == MVT::f32) {
2755 LHS = bitcastf32Toi32(LHS, DAG);
2756 RHS = bitcastf32Toi32(RHS, DAG);
2757 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2758 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2759 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2760 Chain, Dest, ARMcc, CCR, Cmp);
2761 }
2762
2763 SDValue LHS1, LHS2;
2764 SDValue RHS1, RHS2;
2765 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2766 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2767 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2768 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002769 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002770 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2771 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2772 }
2773
2774 return SDValue();
2775}
2776
2777SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2778 SDValue Chain = Op.getOperand(0);
2779 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2780 SDValue LHS = Op.getOperand(2);
2781 SDValue RHS = Op.getOperand(3);
2782 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002783 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002784
Owen Anderson825b72b2009-08-11 20:47:22 +00002785 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002786 SDValue ARMcc;
2787 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002788 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002789 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002790 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002791 }
2792
Owen Anderson825b72b2009-08-11 20:47:22 +00002793 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002794
2795 if (UnsafeFPMath &&
2796 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2797 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2798 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2799 if (Result.getNode())
2800 return Result;
2801 }
2802
Evan Chenga8e29892007-01-19 07:51:42 +00002803 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002804 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002805
Evan Cheng218977b2010-07-13 19:27:42 +00002806 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2807 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002808 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002809 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002810 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002811 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002812 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002813 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2814 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002815 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002816 }
2817 return Res;
2818}
2819
Dan Gohmand858e902010-04-17 15:26:15 +00002820SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002821 SDValue Chain = Op.getOperand(0);
2822 SDValue Table = Op.getOperand(1);
2823 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002824 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002825
Owen Andersone50ed302009-08-10 22:56:29 +00002826 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002827 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2828 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002829 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002830 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002831 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002832 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2833 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002834 if (Subtarget->isThumb2()) {
2835 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2836 // which does another jump to the destination. This also makes it easier
2837 // to translate it to TBB / TBH later.
2838 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002839 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002840 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002841 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002842 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002843 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002844 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002845 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002846 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002847 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002848 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002849 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002850 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002851 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002852 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002853 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002854 }
Evan Chenga8e29892007-01-19 07:51:42 +00002855}
2856
Bob Wilson76a312b2010-03-19 22:51:32 +00002857static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2858 DebugLoc dl = Op.getDebugLoc();
2859 unsigned Opc;
2860
2861 switch (Op.getOpcode()) {
2862 default:
2863 assert(0 && "Invalid opcode!");
2864 case ISD::FP_TO_SINT:
2865 Opc = ARMISD::FTOSI;
2866 break;
2867 case ISD::FP_TO_UINT:
2868 Opc = ARMISD::FTOUI;
2869 break;
2870 }
2871 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002872 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002873}
2874
Cameron Zwarich3007d332011-03-29 21:41:55 +00002875static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2876 EVT VT = Op.getValueType();
2877 DebugLoc dl = Op.getDebugLoc();
2878
2879 EVT OperandVT = Op.getOperand(0).getValueType();
2880 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2881 if (VT != MVT::v4f32)
2882 return DAG.UnrollVectorOp(Op.getNode());
2883
2884 unsigned CastOpc;
2885 unsigned Opc;
2886 switch (Op.getOpcode()) {
2887 default:
2888 assert(0 && "Invalid opcode!");
2889 case ISD::SINT_TO_FP:
2890 CastOpc = ISD::SIGN_EXTEND;
2891 Opc = ISD::SINT_TO_FP;
2892 break;
2893 case ISD::UINT_TO_FP:
2894 CastOpc = ISD::ZERO_EXTEND;
2895 Opc = ISD::UINT_TO_FP;
2896 break;
2897 }
2898
2899 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2900 return DAG.getNode(Opc, dl, VT, Op);
2901}
2902
Bob Wilson76a312b2010-03-19 22:51:32 +00002903static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2904 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002905 if (VT.isVector())
2906 return LowerVectorINT_TO_FP(Op, DAG);
2907
Bob Wilson76a312b2010-03-19 22:51:32 +00002908 DebugLoc dl = Op.getDebugLoc();
2909 unsigned Opc;
2910
2911 switch (Op.getOpcode()) {
2912 default:
2913 assert(0 && "Invalid opcode!");
2914 case ISD::SINT_TO_FP:
2915 Opc = ARMISD::SITOF;
2916 break;
2917 case ISD::UINT_TO_FP:
2918 Opc = ARMISD::UITOF;
2919 break;
2920 }
2921
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002922 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002923 return DAG.getNode(Opc, dl, VT, Op);
2924}
2925
Evan Cheng515fe3a2010-07-08 02:08:50 +00002926SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002927 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002928 SDValue Tmp0 = Op.getOperand(0);
2929 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002930 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002931 EVT VT = Op.getValueType();
2932 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00002933 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
2934 Tmp0.getOpcode() == ARMISD::VMOVDRR;
2935 bool UseNEON = !InGPR && Subtarget->hasNEON();
2936
2937 if (UseNEON) {
2938 // Use VBSL to copy the sign bit.
2939 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
2940 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
2941 DAG.getTargetConstant(EncodedVal, MVT::i32));
2942 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
2943 if (VT == MVT::f64)
2944 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
2945 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
2946 DAG.getConstant(32, MVT::i32));
2947 else /*if (VT == MVT::f32)*/
2948 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
2949 if (SrcVT == MVT::f32) {
2950 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
2951 if (VT == MVT::f64)
2952 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
2953 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
2954 DAG.getConstant(32, MVT::i32));
2955 }
2956 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
2957 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
2958
2959 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
2960 MVT::i32);
2961 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
2962 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
2963 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00002964
Evan Chenge573fb32011-02-23 02:24:55 +00002965 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
2966 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
2967 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00002968 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00002969 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
2970 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
2971 DAG.getConstant(0, MVT::i32));
2972 } else {
2973 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
2974 }
2975
2976 return Res;
2977 }
Evan Chengc143dd42011-02-11 02:28:55 +00002978
2979 // Bitcast operand 1 to i32.
2980 if (SrcVT == MVT::f64)
2981 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2982 &Tmp1, 1).getValue(1);
2983 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
2984
Evan Chenge573fb32011-02-23 02:24:55 +00002985 // Or in the signbit with integer operations.
2986 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
2987 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
2988 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
2989 if (VT == MVT::f32) {
2990 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
2991 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
2992 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
2993 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00002994 }
2995
Evan Chenge573fb32011-02-23 02:24:55 +00002996 // f64: Or the high part with signbit and then combine two parts.
2997 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2998 &Tmp0, 1);
2999 SDValue Lo = Tmp0.getValue(0);
3000 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3001 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3002 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003003}
3004
Evan Cheng2457f2c2010-05-22 01:47:14 +00003005SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3006 MachineFunction &MF = DAG.getMachineFunction();
3007 MachineFrameInfo *MFI = MF.getFrameInfo();
3008 MFI->setReturnAddressIsTaken(true);
3009
3010 EVT VT = Op.getValueType();
3011 DebugLoc dl = Op.getDebugLoc();
3012 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3013 if (Depth) {
3014 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3015 SDValue Offset = DAG.getConstant(4, MVT::i32);
3016 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3017 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003018 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003019 }
3020
3021 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003022 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003023 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3024}
3025
Dan Gohmand858e902010-04-17 15:26:15 +00003026SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003027 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3028 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003029
Owen Andersone50ed302009-08-10 22:56:29 +00003030 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003031 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3032 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003033 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003034 ? ARM::R7 : ARM::R11;
3035 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3036 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003037 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3038 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003039 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003040 return FrameAddr;
3041}
3042
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003043/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003044/// expand a bit convert where either the source or destination type is i64 to
3045/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3046/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3047/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003048static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003049 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3050 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003051 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003052
Bob Wilson9f3f0612010-04-17 05:30:19 +00003053 // This function is only supposed to be called for i64 types, either as the
3054 // source or destination of the bit convert.
3055 EVT SrcVT = Op.getValueType();
3056 EVT DstVT = N->getValueType(0);
3057 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003058 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003059
Bob Wilson9f3f0612010-04-17 05:30:19 +00003060 // Turn i64->f64 into VMOVDRR.
3061 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003062 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3063 DAG.getConstant(0, MVT::i32));
3064 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3065 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003066 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003067 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003068 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003069
Jim Grosbache5165492009-11-09 00:11:35 +00003070 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003071 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3072 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3073 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3074 // Merge the pieces into a single i64 value.
3075 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3076 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003077
Bob Wilson9f3f0612010-04-17 05:30:19 +00003078 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003079}
3080
Bob Wilson5bafff32009-06-22 23:27:02 +00003081/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003082/// Zero vectors are used to represent vector negation and in those cases
3083/// will be implemented with the NEON VNEG instruction. However, VNEG does
3084/// not support i64 elements, so sometimes the zero vectors will need to be
3085/// explicitly constructed. Regardless, use a canonical VMOV to create the
3086/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003087static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003088 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003089 // The canonical modified immediate encoding of a zero vector is....0!
3090 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3091 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3092 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003093 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003094}
3095
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003096/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3097/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003098SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3099 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003100 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3101 EVT VT = Op.getValueType();
3102 unsigned VTBits = VT.getSizeInBits();
3103 DebugLoc dl = Op.getDebugLoc();
3104 SDValue ShOpLo = Op.getOperand(0);
3105 SDValue ShOpHi = Op.getOperand(1);
3106 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003107 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003108 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003109
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003110 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3111
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003112 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3113 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3114 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3115 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3116 DAG.getConstant(VTBits, MVT::i32));
3117 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3118 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003119 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003120
3121 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3122 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003123 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003124 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003125 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003126 CCR, Cmp);
3127
3128 SDValue Ops[2] = { Lo, Hi };
3129 return DAG.getMergeValues(Ops, 2, dl);
3130}
3131
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003132/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3133/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003134SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3135 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003136 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3137 EVT VT = Op.getValueType();
3138 unsigned VTBits = VT.getSizeInBits();
3139 DebugLoc dl = Op.getDebugLoc();
3140 SDValue ShOpLo = Op.getOperand(0);
3141 SDValue ShOpHi = Op.getOperand(1);
3142 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003143 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003144
3145 assert(Op.getOpcode() == ISD::SHL_PARTS);
3146 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3147 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3148 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3149 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3150 DAG.getConstant(VTBits, MVT::i32));
3151 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3152 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3153
3154 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3155 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3156 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003157 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003158 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003159 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003160 CCR, Cmp);
3161
3162 SDValue Ops[2] = { Lo, Hi };
3163 return DAG.getMergeValues(Ops, 2, dl);
3164}
3165
Jim Grosbach4725ca72010-09-08 03:54:02 +00003166SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003167 SelectionDAG &DAG) const {
3168 // The rounding mode is in bits 23:22 of the FPSCR.
3169 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3170 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3171 // so that the shift + and get folded into a bitfield extract.
3172 DebugLoc dl = Op.getDebugLoc();
3173 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3174 DAG.getConstant(Intrinsic::arm_get_fpscr,
3175 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003176 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003177 DAG.getConstant(1U << 22, MVT::i32));
3178 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3179 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003180 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003181 DAG.getConstant(3, MVT::i32));
3182}
3183
Jim Grosbach3482c802010-01-18 19:58:49 +00003184static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3185 const ARMSubtarget *ST) {
3186 EVT VT = N->getValueType(0);
3187 DebugLoc dl = N->getDebugLoc();
3188
3189 if (!ST->hasV6T2Ops())
3190 return SDValue();
3191
3192 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3193 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3194}
3195
Bob Wilson5bafff32009-06-22 23:27:02 +00003196static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3197 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003198 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003199 DebugLoc dl = N->getDebugLoc();
3200
Bob Wilsond5448bb2010-11-18 21:16:28 +00003201 if (!VT.isVector())
3202 return SDValue();
3203
Bob Wilson5bafff32009-06-22 23:27:02 +00003204 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003205 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003206
Bob Wilsond5448bb2010-11-18 21:16:28 +00003207 // Left shifts translate directly to the vshiftu intrinsic.
3208 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003209 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003210 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3211 N->getOperand(0), N->getOperand(1));
3212
3213 assert((N->getOpcode() == ISD::SRA ||
3214 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3215
3216 // NEON uses the same intrinsics for both left and right shifts. For
3217 // right shifts, the shift amounts are negative, so negate the vector of
3218 // shift amounts.
3219 EVT ShiftVT = N->getOperand(1).getValueType();
3220 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3221 getZeroVector(ShiftVT, DAG, dl),
3222 N->getOperand(1));
3223 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3224 Intrinsic::arm_neon_vshifts :
3225 Intrinsic::arm_neon_vshiftu);
3226 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3227 DAG.getConstant(vshiftInt, MVT::i32),
3228 N->getOperand(0), NegatedCount);
3229}
3230
3231static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3232 const ARMSubtarget *ST) {
3233 EVT VT = N->getValueType(0);
3234 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003235
Eli Friedmance392eb2009-08-22 03:13:10 +00003236 // We can get here for a node like i32 = ISD::SHL i32, i64
3237 if (VT != MVT::i64)
3238 return SDValue();
3239
3240 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003241 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003242
Chris Lattner27a6c732007-11-24 07:07:01 +00003243 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3244 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003245 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003246 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003247
Chris Lattner27a6c732007-11-24 07:07:01 +00003248 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003249 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003250
Chris Lattner27a6c732007-11-24 07:07:01 +00003251 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003252 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003253 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003254 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003255 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003256
Chris Lattner27a6c732007-11-24 07:07:01 +00003257 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3258 // captures the result into a carry flag.
3259 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003260 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003261
Chris Lattner27a6c732007-11-24 07:07:01 +00003262 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003263 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003264
Chris Lattner27a6c732007-11-24 07:07:01 +00003265 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003266 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003267}
3268
Bob Wilson5bafff32009-06-22 23:27:02 +00003269static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3270 SDValue TmpOp0, TmpOp1;
3271 bool Invert = false;
3272 bool Swap = false;
3273 unsigned Opc = 0;
3274
3275 SDValue Op0 = Op.getOperand(0);
3276 SDValue Op1 = Op.getOperand(1);
3277 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003278 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003279 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3280 DebugLoc dl = Op.getDebugLoc();
3281
3282 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3283 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003284 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003285 case ISD::SETUNE:
3286 case ISD::SETNE: Invert = true; // Fallthrough
3287 case ISD::SETOEQ:
3288 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3289 case ISD::SETOLT:
3290 case ISD::SETLT: Swap = true; // Fallthrough
3291 case ISD::SETOGT:
3292 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3293 case ISD::SETOLE:
3294 case ISD::SETLE: Swap = true; // Fallthrough
3295 case ISD::SETOGE:
3296 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3297 case ISD::SETUGE: Swap = true; // Fallthrough
3298 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3299 case ISD::SETUGT: Swap = true; // Fallthrough
3300 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3301 case ISD::SETUEQ: Invert = true; // Fallthrough
3302 case ISD::SETONE:
3303 // Expand this to (OLT | OGT).
3304 TmpOp0 = Op0;
3305 TmpOp1 = Op1;
3306 Opc = ISD::OR;
3307 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3308 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3309 break;
3310 case ISD::SETUO: Invert = true; // Fallthrough
3311 case ISD::SETO:
3312 // Expand this to (OLT | OGE).
3313 TmpOp0 = Op0;
3314 TmpOp1 = Op1;
3315 Opc = ISD::OR;
3316 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3317 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3318 break;
3319 }
3320 } else {
3321 // Integer comparisons.
3322 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003323 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003324 case ISD::SETNE: Invert = true;
3325 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3326 case ISD::SETLT: Swap = true;
3327 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3328 case ISD::SETLE: Swap = true;
3329 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3330 case ISD::SETULT: Swap = true;
3331 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3332 case ISD::SETULE: Swap = true;
3333 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3334 }
3335
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003336 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003337 if (Opc == ARMISD::VCEQ) {
3338
3339 SDValue AndOp;
3340 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3341 AndOp = Op0;
3342 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3343 AndOp = Op1;
3344
3345 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003346 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003347 AndOp = AndOp.getOperand(0);
3348
3349 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3350 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003351 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3352 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003353 Invert = !Invert;
3354 }
3355 }
3356 }
3357
3358 if (Swap)
3359 std::swap(Op0, Op1);
3360
Owen Andersonc24cb352010-11-08 23:21:22 +00003361 // If one of the operands is a constant vector zero, attempt to fold the
3362 // comparison to a specialized compare-against-zero form.
3363 SDValue SingleOp;
3364 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3365 SingleOp = Op0;
3366 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3367 if (Opc == ARMISD::VCGE)
3368 Opc = ARMISD::VCLEZ;
3369 else if (Opc == ARMISD::VCGT)
3370 Opc = ARMISD::VCLTZ;
3371 SingleOp = Op1;
3372 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003373
Owen Andersonc24cb352010-11-08 23:21:22 +00003374 SDValue Result;
3375 if (SingleOp.getNode()) {
3376 switch (Opc) {
3377 case ARMISD::VCEQ:
3378 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3379 case ARMISD::VCGE:
3380 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3381 case ARMISD::VCLEZ:
3382 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3383 case ARMISD::VCGT:
3384 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3385 case ARMISD::VCLTZ:
3386 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3387 default:
3388 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3389 }
3390 } else {
3391 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3392 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003393
3394 if (Invert)
3395 Result = DAG.getNOT(dl, Result, VT);
3396
3397 return Result;
3398}
3399
Bob Wilsond3c42842010-06-14 22:19:57 +00003400/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3401/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003402/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003403static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3404 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003405 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003406 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003407
Bob Wilson827b2102010-06-15 19:05:35 +00003408 // SplatBitSize is set to the smallest size that splats the vector, so a
3409 // zero vector will always have SplatBitSize == 8. However, NEON modified
3410 // immediate instructions others than VMOV do not support the 8-bit encoding
3411 // of a zero vector, and the default encoding of zero is supposed to be the
3412 // 32-bit version.
3413 if (SplatBits == 0)
3414 SplatBitSize = 32;
3415
Bob Wilson5bafff32009-06-22 23:27:02 +00003416 switch (SplatBitSize) {
3417 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003418 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003419 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003420 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003421 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003422 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003423 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003424 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003425 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003426
3427 case 16:
3428 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003429 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003430 if ((SplatBits & ~0xff) == 0) {
3431 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003432 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003433 Imm = SplatBits;
3434 break;
3435 }
3436 if ((SplatBits & ~0xff00) == 0) {
3437 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003438 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003439 Imm = SplatBits >> 8;
3440 break;
3441 }
3442 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003443
3444 case 32:
3445 // NEON's 32-bit VMOV supports splat values where:
3446 // * only one byte is nonzero, or
3447 // * the least significant byte is 0xff and the second byte is nonzero, or
3448 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003449 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003450 if ((SplatBits & ~0xff) == 0) {
3451 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003452 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003453 Imm = SplatBits;
3454 break;
3455 }
3456 if ((SplatBits & ~0xff00) == 0) {
3457 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003458 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003459 Imm = SplatBits >> 8;
3460 break;
3461 }
3462 if ((SplatBits & ~0xff0000) == 0) {
3463 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003464 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003465 Imm = SplatBits >> 16;
3466 break;
3467 }
3468 if ((SplatBits & ~0xff000000) == 0) {
3469 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003470 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003471 Imm = SplatBits >> 24;
3472 break;
3473 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003474
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003475 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3476 if (type == OtherModImm) return SDValue();
3477
Bob Wilson5bafff32009-06-22 23:27:02 +00003478 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003479 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3480 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003481 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003482 Imm = SplatBits >> 8;
3483 SplatBits |= 0xff;
3484 break;
3485 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003486
3487 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003488 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3489 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003490 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003491 Imm = SplatBits >> 16;
3492 SplatBits |= 0xffff;
3493 break;
3494 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003495
3496 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3497 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3498 // VMOV.I32. A (very) minor optimization would be to replicate the value
3499 // and fall through here to test for a valid 64-bit splat. But, then the
3500 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003501 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003502
3503 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003504 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003505 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003506 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003507 uint64_t BitMask = 0xff;
3508 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003509 unsigned ImmMask = 1;
3510 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003511 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003512 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003513 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003514 Imm |= ImmMask;
3515 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003516 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003517 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003518 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003519 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003520 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003521 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003522 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003523 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003524 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003525 break;
3526 }
3527
Bob Wilson1a913ed2010-06-11 21:34:50 +00003528 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003529 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003530 return SDValue();
3531 }
3532
Bob Wilsoncba270d2010-07-13 21:16:48 +00003533 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3534 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003535}
3536
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003537static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3538 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003539 unsigned NumElts = VT.getVectorNumElements();
3540 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003541
3542 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3543 if (M[0] < 0)
3544 return false;
3545
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003546 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003547
3548 // If this is a VEXT shuffle, the immediate value is the index of the first
3549 // element. The other shuffle indices must be the successive elements after
3550 // the first one.
3551 unsigned ExpectedElt = Imm;
3552 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003553 // Increment the expected index. If it wraps around, it may still be
3554 // a VEXT but the source vectors must be swapped.
3555 ExpectedElt += 1;
3556 if (ExpectedElt == NumElts * 2) {
3557 ExpectedElt = 0;
3558 ReverseVEXT = true;
3559 }
3560
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003561 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003562 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003563 return false;
3564 }
3565
3566 // Adjust the index value if the source operands will be swapped.
3567 if (ReverseVEXT)
3568 Imm -= NumElts;
3569
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003570 return true;
3571}
3572
Bob Wilson8bb9e482009-07-26 00:39:34 +00003573/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3574/// instruction with the specified blocksize. (The order of the elements
3575/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003576static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3577 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003578 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3579 "Only possible block sizes for VREV are: 16, 32, 64");
3580
Bob Wilson8bb9e482009-07-26 00:39:34 +00003581 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003582 if (EltSz == 64)
3583 return false;
3584
3585 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003586 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003587 // If the first shuffle index is UNDEF, be optimistic.
3588 if (M[0] < 0)
3589 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003590
3591 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3592 return false;
3593
3594 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003595 if (M[i] < 0) continue; // ignore UNDEF indices
3596 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003597 return false;
3598 }
3599
3600 return true;
3601}
3602
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003603static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3604 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3605 // range, then 0 is placed into the resulting vector. So pretty much any mask
3606 // of 8 elements can work here.
3607 return VT == MVT::v8i8 && M.size() == 8;
3608}
3609
Bob Wilsonc692cb72009-08-21 20:54:19 +00003610static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3611 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003612 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3613 if (EltSz == 64)
3614 return false;
3615
Bob Wilsonc692cb72009-08-21 20:54:19 +00003616 unsigned NumElts = VT.getVectorNumElements();
3617 WhichResult = (M[0] == 0 ? 0 : 1);
3618 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003619 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3620 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003621 return false;
3622 }
3623 return true;
3624}
3625
Bob Wilson324f4f12009-12-03 06:40:55 +00003626/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3627/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3628/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3629static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3630 unsigned &WhichResult) {
3631 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3632 if (EltSz == 64)
3633 return false;
3634
3635 unsigned NumElts = VT.getVectorNumElements();
3636 WhichResult = (M[0] == 0 ? 0 : 1);
3637 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003638 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3639 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003640 return false;
3641 }
3642 return true;
3643}
3644
Bob Wilsonc692cb72009-08-21 20:54:19 +00003645static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3646 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003647 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3648 if (EltSz == 64)
3649 return false;
3650
Bob Wilsonc692cb72009-08-21 20:54:19 +00003651 unsigned NumElts = VT.getVectorNumElements();
3652 WhichResult = (M[0] == 0 ? 0 : 1);
3653 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003654 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003655 if ((unsigned) M[i] != 2 * i + WhichResult)
3656 return false;
3657 }
3658
3659 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003660 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003661 return false;
3662
3663 return true;
3664}
3665
Bob Wilson324f4f12009-12-03 06:40:55 +00003666/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3667/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3668/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3669static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3670 unsigned &WhichResult) {
3671 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3672 if (EltSz == 64)
3673 return false;
3674
3675 unsigned Half = VT.getVectorNumElements() / 2;
3676 WhichResult = (M[0] == 0 ? 0 : 1);
3677 for (unsigned j = 0; j != 2; ++j) {
3678 unsigned Idx = WhichResult;
3679 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003680 int MIdx = M[i + j * Half];
3681 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003682 return false;
3683 Idx += 2;
3684 }
3685 }
3686
3687 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3688 if (VT.is64BitVector() && EltSz == 32)
3689 return false;
3690
3691 return true;
3692}
3693
Bob Wilsonc692cb72009-08-21 20:54:19 +00003694static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3695 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003696 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3697 if (EltSz == 64)
3698 return false;
3699
Bob Wilsonc692cb72009-08-21 20:54:19 +00003700 unsigned NumElts = VT.getVectorNumElements();
3701 WhichResult = (M[0] == 0 ? 0 : 1);
3702 unsigned Idx = WhichResult * NumElts / 2;
3703 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003704 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3705 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003706 return false;
3707 Idx += 1;
3708 }
3709
3710 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003711 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003712 return false;
3713
3714 return true;
3715}
3716
Bob Wilson324f4f12009-12-03 06:40:55 +00003717/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3718/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3719/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3720static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3721 unsigned &WhichResult) {
3722 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3723 if (EltSz == 64)
3724 return false;
3725
3726 unsigned NumElts = VT.getVectorNumElements();
3727 WhichResult = (M[0] == 0 ? 0 : 1);
3728 unsigned Idx = WhichResult * NumElts / 2;
3729 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003730 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3731 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003732 return false;
3733 Idx += 1;
3734 }
3735
3736 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3737 if (VT.is64BitVector() && EltSz == 32)
3738 return false;
3739
3740 return true;
3741}
3742
Dale Johannesenf630c712010-07-29 20:10:08 +00003743// If N is an integer constant that can be moved into a register in one
3744// instruction, return an SDValue of such a constant (will become a MOV
3745// instruction). Otherwise return null.
3746static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3747 const ARMSubtarget *ST, DebugLoc dl) {
3748 uint64_t Val;
3749 if (!isa<ConstantSDNode>(N))
3750 return SDValue();
3751 Val = cast<ConstantSDNode>(N)->getZExtValue();
3752
3753 if (ST->isThumb1Only()) {
3754 if (Val <= 255 || ~Val <= 255)
3755 return DAG.getConstant(Val, MVT::i32);
3756 } else {
3757 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3758 return DAG.getConstant(Val, MVT::i32);
3759 }
3760 return SDValue();
3761}
3762
Bob Wilson5bafff32009-06-22 23:27:02 +00003763// If this is a case we can't handle, return null and let the default
3764// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003765SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3766 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003767 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003768 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003769 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003770
3771 APInt SplatBits, SplatUndef;
3772 unsigned SplatBitSize;
3773 bool HasAnyUndefs;
3774 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003775 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003776 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003777 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003778 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003779 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003780 DAG, VmovVT, VT.is128BitVector(),
3781 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003782 if (Val.getNode()) {
3783 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003784 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003785 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003786
3787 // Try an immediate VMVN.
3788 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3789 ((1LL << SplatBitSize) - 1));
3790 Val = isNEONModifiedImm(NegatedImm,
3791 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003792 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003793 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003794 if (Val.getNode()) {
3795 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003796 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003797 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003798 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003799 }
3800
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003801 // Scan through the operands to see if only one value is used.
3802 unsigned NumElts = VT.getVectorNumElements();
3803 bool isOnlyLowElement = true;
3804 bool usesOnlyOneValue = true;
3805 bool isConstant = true;
3806 SDValue Value;
3807 for (unsigned i = 0; i < NumElts; ++i) {
3808 SDValue V = Op.getOperand(i);
3809 if (V.getOpcode() == ISD::UNDEF)
3810 continue;
3811 if (i > 0)
3812 isOnlyLowElement = false;
3813 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3814 isConstant = false;
3815
3816 if (!Value.getNode())
3817 Value = V;
3818 else if (V != Value)
3819 usesOnlyOneValue = false;
3820 }
3821
3822 if (!Value.getNode())
3823 return DAG.getUNDEF(VT);
3824
3825 if (isOnlyLowElement)
3826 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3827
Dale Johannesenf630c712010-07-29 20:10:08 +00003828 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3829
Dale Johannesen575cd142010-10-19 20:00:17 +00003830 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3831 // i32 and try again.
3832 if (usesOnlyOneValue && EltSize <= 32) {
3833 if (!isConstant)
3834 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3835 if (VT.getVectorElementType().isFloatingPoint()) {
3836 SmallVector<SDValue, 8> Ops;
3837 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003838 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003839 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003840 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3841 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003842 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3843 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003844 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003845 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003846 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3847 if (Val.getNode())
3848 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003849 }
3850
3851 // If all elements are constants and the case above didn't get hit, fall back
3852 // to the default expansion, which will generate a load from the constant
3853 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003854 if (isConstant)
3855 return SDValue();
3856
Bob Wilson11a1dff2011-01-07 21:37:30 +00003857 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3858 if (NumElts >= 4) {
3859 SDValue shuffle = ReconstructShuffle(Op, DAG);
3860 if (shuffle != SDValue())
3861 return shuffle;
3862 }
3863
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003864 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003865 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3866 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003867 if (EltSize >= 32) {
3868 // Do the expansion with floating-point types, since that is what the VFP
3869 // registers are defined to use, and since i64 is not legal.
3870 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3871 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003872 SmallVector<SDValue, 8> Ops;
3873 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003874 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003875 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003876 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003877 }
3878
3879 return SDValue();
3880}
3881
Bob Wilson11a1dff2011-01-07 21:37:30 +00003882// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003883// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003884SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3885 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003886 DebugLoc dl = Op.getDebugLoc();
3887 EVT VT = Op.getValueType();
3888 unsigned NumElts = VT.getVectorNumElements();
3889
3890 SmallVector<SDValue, 2> SourceVecs;
3891 SmallVector<unsigned, 2> MinElts;
3892 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003893
Bob Wilson11a1dff2011-01-07 21:37:30 +00003894 for (unsigned i = 0; i < NumElts; ++i) {
3895 SDValue V = Op.getOperand(i);
3896 if (V.getOpcode() == ISD::UNDEF)
3897 continue;
3898 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3899 // A shuffle can only come from building a vector from various
3900 // elements of other vectors.
3901 return SDValue();
3902 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003903
Bob Wilson11a1dff2011-01-07 21:37:30 +00003904 // Record this extraction against the appropriate vector if possible...
3905 SDValue SourceVec = V.getOperand(0);
3906 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3907 bool FoundSource = false;
3908 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3909 if (SourceVecs[j] == SourceVec) {
3910 if (MinElts[j] > EltNo)
3911 MinElts[j] = EltNo;
3912 if (MaxElts[j] < EltNo)
3913 MaxElts[j] = EltNo;
3914 FoundSource = true;
3915 break;
3916 }
3917 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003918
Bob Wilson11a1dff2011-01-07 21:37:30 +00003919 // Or record a new source if not...
3920 if (!FoundSource) {
3921 SourceVecs.push_back(SourceVec);
3922 MinElts.push_back(EltNo);
3923 MaxElts.push_back(EltNo);
3924 }
3925 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003926
Bob Wilson11a1dff2011-01-07 21:37:30 +00003927 // Currently only do something sane when at most two source vectors
3928 // involved.
3929 if (SourceVecs.size() > 2)
3930 return SDValue();
3931
3932 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3933 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003934
Bob Wilson11a1dff2011-01-07 21:37:30 +00003935 // This loop extracts the usage patterns of the source vectors
3936 // and prepares appropriate SDValues for a shuffle if possible.
3937 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3938 if (SourceVecs[i].getValueType() == VT) {
3939 // No VEXT necessary
3940 ShuffleSrcs[i] = SourceVecs[i];
3941 VEXTOffsets[i] = 0;
3942 continue;
3943 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3944 // It probably isn't worth padding out a smaller vector just to
3945 // break it down again in a shuffle.
3946 return SDValue();
3947 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003948
Bob Wilson11a1dff2011-01-07 21:37:30 +00003949 // Since only 64-bit and 128-bit vectors are legal on ARM and
3950 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003951 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3952 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003953
Bob Wilson11a1dff2011-01-07 21:37:30 +00003954 if (MaxElts[i] - MinElts[i] >= NumElts) {
3955 // Span too large for a VEXT to cope
3956 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003957 }
3958
Bob Wilson11a1dff2011-01-07 21:37:30 +00003959 if (MinElts[i] >= NumElts) {
3960 // The extraction can just take the second half
3961 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003962 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3963 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003964 DAG.getIntPtrConstant(NumElts));
3965 } else if (MaxElts[i] < NumElts) {
3966 // The extraction can just take the first half
3967 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003968 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3969 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003970 DAG.getIntPtrConstant(0));
3971 } else {
3972 // An actual VEXT is needed
3973 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003974 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3975 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003976 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003977 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3978 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003979 DAG.getIntPtrConstant(NumElts));
3980 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3981 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3982 }
3983 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003984
Bob Wilson11a1dff2011-01-07 21:37:30 +00003985 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003986
Bob Wilson11a1dff2011-01-07 21:37:30 +00003987 for (unsigned i = 0; i < NumElts; ++i) {
3988 SDValue Entry = Op.getOperand(i);
3989 if (Entry.getOpcode() == ISD::UNDEF) {
3990 Mask.push_back(-1);
3991 continue;
3992 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003993
Bob Wilson11a1dff2011-01-07 21:37:30 +00003994 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003995 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3996 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003997 if (ExtractVec == SourceVecs[0]) {
3998 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3999 } else {
4000 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4001 }
4002 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004003
Bob Wilson11a1dff2011-01-07 21:37:30 +00004004 // Final check before we try to produce nonsense...
4005 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004006 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4007 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004008
Bob Wilson11a1dff2011-01-07 21:37:30 +00004009 return SDValue();
4010}
4011
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004012/// isShuffleMaskLegal - Targets can use this to indicate that they only
4013/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4014/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4015/// are assumed to be legal.
4016bool
4017ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4018 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004019 if (VT.getVectorNumElements() == 4 &&
4020 (VT.is128BitVector() || VT.is64BitVector())) {
4021 unsigned PFIndexes[4];
4022 for (unsigned i = 0; i != 4; ++i) {
4023 if (M[i] < 0)
4024 PFIndexes[i] = 8;
4025 else
4026 PFIndexes[i] = M[i];
4027 }
4028
4029 // Compute the index in the perfect shuffle table.
4030 unsigned PFTableIndex =
4031 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4032 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4033 unsigned Cost = (PFEntry >> 30);
4034
4035 if (Cost <= 4)
4036 return true;
4037 }
4038
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004039 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004040 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004041
Bob Wilson53dd2452010-06-07 23:53:38 +00004042 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4043 return (EltSize >= 32 ||
4044 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004045 isVREVMask(M, VT, 64) ||
4046 isVREVMask(M, VT, 32) ||
4047 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004048 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004049 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004050 isVTRNMask(M, VT, WhichResult) ||
4051 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004052 isVZIPMask(M, VT, WhichResult) ||
4053 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4054 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4055 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004056}
4057
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004058/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4059/// the specified operations to build the shuffle.
4060static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4061 SDValue RHS, SelectionDAG &DAG,
4062 DebugLoc dl) {
4063 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4064 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4065 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4066
4067 enum {
4068 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4069 OP_VREV,
4070 OP_VDUP0,
4071 OP_VDUP1,
4072 OP_VDUP2,
4073 OP_VDUP3,
4074 OP_VEXT1,
4075 OP_VEXT2,
4076 OP_VEXT3,
4077 OP_VUZPL, // VUZP, left result
4078 OP_VUZPR, // VUZP, right result
4079 OP_VZIPL, // VZIP, left result
4080 OP_VZIPR, // VZIP, right result
4081 OP_VTRNL, // VTRN, left result
4082 OP_VTRNR // VTRN, right result
4083 };
4084
4085 if (OpNum == OP_COPY) {
4086 if (LHSID == (1*9+2)*9+3) return LHS;
4087 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4088 return RHS;
4089 }
4090
4091 SDValue OpLHS, OpRHS;
4092 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4093 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4094 EVT VT = OpLHS.getValueType();
4095
4096 switch (OpNum) {
4097 default: llvm_unreachable("Unknown shuffle opcode!");
4098 case OP_VREV:
4099 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4100 case OP_VDUP0:
4101 case OP_VDUP1:
4102 case OP_VDUP2:
4103 case OP_VDUP3:
4104 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004105 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004106 case OP_VEXT1:
4107 case OP_VEXT2:
4108 case OP_VEXT3:
4109 return DAG.getNode(ARMISD::VEXT, dl, VT,
4110 OpLHS, OpRHS,
4111 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4112 case OP_VUZPL:
4113 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004114 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004115 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4116 case OP_VZIPL:
4117 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004118 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004119 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4120 case OP_VTRNL:
4121 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004122 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4123 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004124 }
4125}
4126
Bill Wendling69a05a72011-03-14 23:02:38 +00004127static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4128 SmallVectorImpl<int> &ShuffleMask,
4129 SelectionDAG &DAG) {
4130 // Check to see if we can use the VTBL instruction.
4131 SDValue V1 = Op.getOperand(0);
4132 SDValue V2 = Op.getOperand(1);
4133 DebugLoc DL = Op.getDebugLoc();
4134
4135 SmallVector<SDValue, 8> VTBLMask;
4136 for (SmallVectorImpl<int>::iterator
4137 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4138 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4139
4140 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4141 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4142 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4143 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004144
Owen Anderson76706012011-04-05 21:48:57 +00004145 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004146 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4147 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004148}
4149
Bob Wilson5bafff32009-06-22 23:27:02 +00004150static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004151 SDValue V1 = Op.getOperand(0);
4152 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004153 DebugLoc dl = Op.getDebugLoc();
4154 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004155 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004156 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004157
Bob Wilson28865062009-08-13 02:13:04 +00004158 // Convert shuffles that are directly supported on NEON to target-specific
4159 // DAG nodes, instead of keeping them as shuffles and matching them again
4160 // during code selection. This is more efficient and avoids the possibility
4161 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004162 // FIXME: floating-point vectors should be canonicalized to integer vectors
4163 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004164 SVN->getMask(ShuffleMask);
4165
Bob Wilson53dd2452010-06-07 23:53:38 +00004166 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4167 if (EltSize <= 32) {
4168 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4169 int Lane = SVN->getSplatIndex();
4170 // If this is undef splat, generate it via "just" vdup, if possible.
4171 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004172
Bob Wilson53dd2452010-06-07 23:53:38 +00004173 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4174 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4175 }
4176 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4177 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004178 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004179
4180 bool ReverseVEXT;
4181 unsigned Imm;
4182 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4183 if (ReverseVEXT)
4184 std::swap(V1, V2);
4185 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4186 DAG.getConstant(Imm, MVT::i32));
4187 }
4188
4189 if (isVREVMask(ShuffleMask, VT, 64))
4190 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4191 if (isVREVMask(ShuffleMask, VT, 32))
4192 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4193 if (isVREVMask(ShuffleMask, VT, 16))
4194 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4195
4196 // Check for Neon shuffles that modify both input vectors in place.
4197 // If both results are used, i.e., if there are two shuffles with the same
4198 // source operands and with masks corresponding to both results of one of
4199 // these operations, DAG memoization will ensure that a single node is
4200 // used for both shuffles.
4201 unsigned WhichResult;
4202 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4203 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4204 V1, V2).getValue(WhichResult);
4205 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4206 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4207 V1, V2).getValue(WhichResult);
4208 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4209 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4210 V1, V2).getValue(WhichResult);
4211
4212 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4213 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4214 V1, V1).getValue(WhichResult);
4215 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4216 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4217 V1, V1).getValue(WhichResult);
4218 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4219 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4220 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004221 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004222
Bob Wilsonc692cb72009-08-21 20:54:19 +00004223 // If the shuffle is not directly supported and it has 4 elements, use
4224 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004225 unsigned NumElts = VT.getVectorNumElements();
4226 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004227 unsigned PFIndexes[4];
4228 for (unsigned i = 0; i != 4; ++i) {
4229 if (ShuffleMask[i] < 0)
4230 PFIndexes[i] = 8;
4231 else
4232 PFIndexes[i] = ShuffleMask[i];
4233 }
4234
4235 // Compute the index in the perfect shuffle table.
4236 unsigned PFTableIndex =
4237 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004238 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4239 unsigned Cost = (PFEntry >> 30);
4240
4241 if (Cost <= 4)
4242 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4243 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004244
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004245 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004246 if (EltSize >= 32) {
4247 // Do the expansion with floating-point types, since that is what the VFP
4248 // registers are defined to use, and since i64 is not legal.
4249 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4250 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004251 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4252 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004253 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004254 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004255 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004256 Ops.push_back(DAG.getUNDEF(EltVT));
4257 else
4258 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4259 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4260 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4261 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004262 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004263 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004264 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004265 }
4266
Bill Wendling69a05a72011-03-14 23:02:38 +00004267 if (VT == MVT::v8i8) {
4268 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4269 if (NewOp.getNode())
4270 return NewOp;
4271 }
4272
Bob Wilson22cac0d2009-08-14 05:16:33 +00004273 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004274}
4275
Bob Wilson5bafff32009-06-22 23:27:02 +00004276static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004277 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004278 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004279 if (!isa<ConstantSDNode>(Lane))
4280 return SDValue();
4281
4282 SDValue Vec = Op.getOperand(0);
4283 if (Op.getValueType() == MVT::i32 &&
4284 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4285 DebugLoc dl = Op.getDebugLoc();
4286 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4287 }
4288
4289 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004290}
4291
Bob Wilsona6d65862009-08-03 20:36:38 +00004292static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4293 // The only time a CONCAT_VECTORS operation can have legal types is when
4294 // two 64-bit vectors are concatenated to a 128-bit vector.
4295 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4296 "unexpected CONCAT_VECTORS");
4297 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004298 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004299 SDValue Op0 = Op.getOperand(0);
4300 SDValue Op1 = Op.getOperand(1);
4301 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004302 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004303 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004304 DAG.getIntPtrConstant(0));
4305 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004306 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004307 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004308 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004309 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004310}
4311
Bob Wilson626613d2010-11-23 19:38:38 +00004312/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4313/// element has been zero/sign-extended, depending on the isSigned parameter,
4314/// from an integer type half its size.
4315static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4316 bool isSigned) {
4317 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4318 EVT VT = N->getValueType(0);
4319 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4320 SDNode *BVN = N->getOperand(0).getNode();
4321 if (BVN->getValueType(0) != MVT::v4i32 ||
4322 BVN->getOpcode() != ISD::BUILD_VECTOR)
4323 return false;
4324 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4325 unsigned HiElt = 1 - LoElt;
4326 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4327 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4328 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4329 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4330 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4331 return false;
4332 if (isSigned) {
4333 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4334 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4335 return true;
4336 } else {
4337 if (Hi0->isNullValue() && Hi1->isNullValue())
4338 return true;
4339 }
4340 return false;
4341 }
4342
4343 if (N->getOpcode() != ISD::BUILD_VECTOR)
4344 return false;
4345
4346 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4347 SDNode *Elt = N->getOperand(i).getNode();
4348 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4349 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4350 unsigned HalfSize = EltSize / 2;
4351 if (isSigned) {
4352 int64_t SExtVal = C->getSExtValue();
4353 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4354 return false;
4355 } else {
4356 if ((C->getZExtValue() >> HalfSize) != 0)
4357 return false;
4358 }
4359 continue;
4360 }
4361 return false;
4362 }
4363
4364 return true;
4365}
4366
4367/// isSignExtended - Check if a node is a vector value that is sign-extended
4368/// or a constant BUILD_VECTOR with sign-extended elements.
4369static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4370 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4371 return true;
4372 if (isExtendedBUILD_VECTOR(N, DAG, true))
4373 return true;
4374 return false;
4375}
4376
4377/// isZeroExtended - Check if a node is a vector value that is zero-extended
4378/// or a constant BUILD_VECTOR with zero-extended elements.
4379static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4380 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4381 return true;
4382 if (isExtendedBUILD_VECTOR(N, DAG, false))
4383 return true;
4384 return false;
4385}
4386
4387/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4388/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004389static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4390 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4391 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004392 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4393 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4394 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4395 LD->isNonTemporal(), LD->getAlignment());
4396 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4397 // have been legalized as a BITCAST from v4i32.
4398 if (N->getOpcode() == ISD::BITCAST) {
4399 SDNode *BVN = N->getOperand(0).getNode();
4400 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4401 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4402 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4403 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4404 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4405 }
4406 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4407 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4408 EVT VT = N->getValueType(0);
4409 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4410 unsigned NumElts = VT.getVectorNumElements();
4411 MVT TruncVT = MVT::getIntegerVT(EltSize);
4412 SmallVector<SDValue, 8> Ops;
4413 for (unsigned i = 0; i != NumElts; ++i) {
4414 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4415 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004416 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004417 }
4418 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4419 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004420}
4421
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004422static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4423 unsigned Opcode = N->getOpcode();
4424 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4425 SDNode *N0 = N->getOperand(0).getNode();
4426 SDNode *N1 = N->getOperand(1).getNode();
4427 return N0->hasOneUse() && N1->hasOneUse() &&
4428 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4429 }
4430 return false;
4431}
4432
4433static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4434 unsigned Opcode = N->getOpcode();
4435 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4436 SDNode *N0 = N->getOperand(0).getNode();
4437 SDNode *N1 = N->getOperand(1).getNode();
4438 return N0->hasOneUse() && N1->hasOneUse() &&
4439 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4440 }
4441 return false;
4442}
4443
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004444static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4445 // Multiplications are only custom-lowered for 128-bit vectors so that
4446 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4447 EVT VT = Op.getValueType();
4448 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4449 SDNode *N0 = Op.getOperand(0).getNode();
4450 SDNode *N1 = Op.getOperand(1).getNode();
4451 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004452 bool isMLA = false;
4453 bool isN0SExt = isSignExtended(N0, DAG);
4454 bool isN1SExt = isSignExtended(N1, DAG);
4455 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004456 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004457 else {
4458 bool isN0ZExt = isZeroExtended(N0, DAG);
4459 bool isN1ZExt = isZeroExtended(N1, DAG);
4460 if (isN0ZExt && isN1ZExt)
4461 NewOpc = ARMISD::VMULLu;
4462 else if (isN1SExt || isN1ZExt) {
4463 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4464 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4465 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4466 NewOpc = ARMISD::VMULLs;
4467 isMLA = true;
4468 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4469 NewOpc = ARMISD::VMULLu;
4470 isMLA = true;
4471 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4472 std::swap(N0, N1);
4473 NewOpc = ARMISD::VMULLu;
4474 isMLA = true;
4475 }
4476 }
4477
4478 if (!NewOpc) {
4479 if (VT == MVT::v2i64)
4480 // Fall through to expand this. It is not legal.
4481 return SDValue();
4482 else
4483 // Other vector multiplications are legal.
4484 return Op;
4485 }
4486 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004487
4488 // Legalize to a VMULL instruction.
4489 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004490 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004491 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004492 if (!isMLA) {
4493 Op0 = SkipExtension(N0, DAG);
4494 assert(Op0.getValueType().is64BitVector() &&
4495 Op1.getValueType().is64BitVector() &&
4496 "unexpected types for extended operands to VMULL");
4497 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4498 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004499
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004500 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4501 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4502 // vmull q0, d4, d6
4503 // vmlal q0, d5, d6
4504 // is faster than
4505 // vaddl q0, d4, d5
4506 // vmovl q1, d6
4507 // vmul q0, q0, q1
4508 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4509 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4510 EVT Op1VT = Op1.getValueType();
4511 return DAG.getNode(N0->getOpcode(), DL, VT,
4512 DAG.getNode(NewOpc, DL, VT,
4513 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4514 DAG.getNode(NewOpc, DL, VT,
4515 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004516}
4517
Owen Anderson76706012011-04-05 21:48:57 +00004518static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004519LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4520 // Convert to float
4521 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4522 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4523 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4524 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4525 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4526 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4527 // Get reciprocal estimate.
4528 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004529 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004530 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4531 // Because char has a smaller range than uchar, we can actually get away
4532 // without any newton steps. This requires that we use a weird bias
4533 // of 0xb000, however (again, this has been exhaustively tested).
4534 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4535 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4536 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4537 Y = DAG.getConstant(0xb000, MVT::i32);
4538 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4539 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4540 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4541 // Convert back to short.
4542 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4543 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4544 return X;
4545}
4546
Owen Anderson76706012011-04-05 21:48:57 +00004547static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004548LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4549 SDValue N2;
4550 // Convert to float.
4551 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4552 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4553 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4554 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4555 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4556 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004557
Nate Begeman7973f352011-02-11 20:53:29 +00004558 // Use reciprocal estimate and one refinement step.
4559 // float4 recip = vrecpeq_f32(yf);
4560 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004561 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004562 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004563 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004564 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4565 N1, N2);
4566 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4567 // Because short has a smaller range than ushort, we can actually get away
4568 // with only a single newton step. This requires that we use a weird bias
4569 // of 89, however (again, this has been exhaustively tested).
4570 // float4 result = as_float4(as_int4(xf*recip) + 89);
4571 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4572 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4573 N1 = DAG.getConstant(89, MVT::i32);
4574 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4575 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4576 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4577 // Convert back to integer and return.
4578 // return vmovn_s32(vcvt_s32_f32(result));
4579 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4580 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4581 return N0;
4582}
4583
4584static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4585 EVT VT = Op.getValueType();
4586 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4587 "unexpected type for custom-lowering ISD::SDIV");
4588
4589 DebugLoc dl = Op.getDebugLoc();
4590 SDValue N0 = Op.getOperand(0);
4591 SDValue N1 = Op.getOperand(1);
4592 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004593
Nate Begeman7973f352011-02-11 20:53:29 +00004594 if (VT == MVT::v8i8) {
4595 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4596 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004597
Nate Begeman7973f352011-02-11 20:53:29 +00004598 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4599 DAG.getIntPtrConstant(4));
4600 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004601 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004602 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4603 DAG.getIntPtrConstant(0));
4604 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4605 DAG.getIntPtrConstant(0));
4606
4607 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4608 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4609
4610 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4611 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004612
Nate Begeman7973f352011-02-11 20:53:29 +00004613 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4614 return N0;
4615 }
4616 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4617}
4618
4619static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4620 EVT VT = Op.getValueType();
4621 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4622 "unexpected type for custom-lowering ISD::UDIV");
4623
4624 DebugLoc dl = Op.getDebugLoc();
4625 SDValue N0 = Op.getOperand(0);
4626 SDValue N1 = Op.getOperand(1);
4627 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004628
Nate Begeman7973f352011-02-11 20:53:29 +00004629 if (VT == MVT::v8i8) {
4630 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4631 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004632
Nate Begeman7973f352011-02-11 20:53:29 +00004633 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4634 DAG.getIntPtrConstant(4));
4635 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004636 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004637 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4638 DAG.getIntPtrConstant(0));
4639 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4640 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004641
Nate Begeman7973f352011-02-11 20:53:29 +00004642 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4643 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004644
Nate Begeman7973f352011-02-11 20:53:29 +00004645 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4646 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004647
4648 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004649 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4650 N0);
4651 return N0;
4652 }
Owen Anderson76706012011-04-05 21:48:57 +00004653
Nate Begeman7973f352011-02-11 20:53:29 +00004654 // v4i16 sdiv ... Convert to float.
4655 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4656 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4657 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4658 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4659 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4660 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4661
4662 // Use reciprocal estimate and two refinement steps.
4663 // float4 recip = vrecpeq_f32(yf);
4664 // recip *= vrecpsq_f32(yf, recip);
4665 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004666 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004667 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004668 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004669 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4670 N1, N2);
4671 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004672 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004673 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4674 N1, N2);
4675 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4676 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4677 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4678 // and that it will never cause us to return an answer too large).
4679 // float4 result = as_float4(as_int4(xf*recip) + 89);
4680 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4681 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4682 N1 = DAG.getConstant(2, MVT::i32);
4683 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4684 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4685 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4686 // Convert back to integer and return.
4687 // return vmovn_u32(vcvt_s32_f32(result));
4688 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4689 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4690 return N0;
4691}
4692
Dan Gohmand858e902010-04-17 15:26:15 +00004693SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004694 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004695 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004696 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004697 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004698 case ISD::GlobalAddress:
4699 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4700 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004701 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004702 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004703 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4704 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004705 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004706 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004707 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004708 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004709 case ISD::SINT_TO_FP:
4710 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4711 case ISD::FP_TO_SINT:
4712 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004713 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004714 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004715 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004716 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004717 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004718 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004719 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004720 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4721 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004722 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004723 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004724 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004725 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004726 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004727 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004728 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004729 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004730 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004731 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004732 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004733 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004734 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004735 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004736 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004737 case ISD::SDIV: return LowerSDIV(Op, DAG);
4738 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004739 }
Dan Gohman475871a2008-07-27 21:46:04 +00004740 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004741}
4742
Duncan Sands1607f052008-12-01 11:39:25 +00004743/// ReplaceNodeResults - Replace the results of node with an illegal result
4744/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004745void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4746 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004747 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004748 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004749 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004750 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004751 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004752 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004753 case ISD::BITCAST:
4754 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004755 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004756 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004757 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004758 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004759 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004760 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004761 if (Res.getNode())
4762 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004763}
Chris Lattner27a6c732007-11-24 07:07:01 +00004764
Evan Chenga8e29892007-01-19 07:51:42 +00004765//===----------------------------------------------------------------------===//
4766// ARM Scheduler Hooks
4767//===----------------------------------------------------------------------===//
4768
4769MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004770ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4771 MachineBasicBlock *BB,
4772 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004773 unsigned dest = MI->getOperand(0).getReg();
4774 unsigned ptr = MI->getOperand(1).getReg();
4775 unsigned oldval = MI->getOperand(2).getReg();
4776 unsigned newval = MI->getOperand(3).getReg();
4777 unsigned scratch = BB->getParent()->getRegInfo()
4778 .createVirtualRegister(ARM::GPRRegisterClass);
4779 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4780 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004781 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004782
4783 unsigned ldrOpc, strOpc;
4784 switch (Size) {
4785 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004786 case 1:
4787 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004788 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004789 break;
4790 case 2:
4791 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4792 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4793 break;
4794 case 4:
4795 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4796 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4797 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004798 }
4799
4800 MachineFunction *MF = BB->getParent();
4801 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4802 MachineFunction::iterator It = BB;
4803 ++It; // insert the new blocks after the current block
4804
4805 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4806 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4807 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4808 MF->insert(It, loop1MBB);
4809 MF->insert(It, loop2MBB);
4810 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004811
4812 // Transfer the remainder of BB and its successor edges to exitMBB.
4813 exitMBB->splice(exitMBB->begin(), BB,
4814 llvm::next(MachineBasicBlock::iterator(MI)),
4815 BB->end());
4816 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004817
4818 // thisMBB:
4819 // ...
4820 // fallthrough --> loop1MBB
4821 BB->addSuccessor(loop1MBB);
4822
4823 // loop1MBB:
4824 // ldrex dest, [ptr]
4825 // cmp dest, oldval
4826 // bne exitMBB
4827 BB = loop1MBB;
4828 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004829 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004830 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004831 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4832 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004833 BB->addSuccessor(loop2MBB);
4834 BB->addSuccessor(exitMBB);
4835
4836 // loop2MBB:
4837 // strex scratch, newval, [ptr]
4838 // cmp scratch, #0
4839 // bne loop1MBB
4840 BB = loop2MBB;
4841 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4842 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004843 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004844 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004845 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4846 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004847 BB->addSuccessor(loop1MBB);
4848 BB->addSuccessor(exitMBB);
4849
4850 // exitMBB:
4851 // ...
4852 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004853
Dan Gohman14152b42010-07-06 20:24:04 +00004854 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004855
Jim Grosbach5278eb82009-12-11 01:42:04 +00004856 return BB;
4857}
4858
4859MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004860ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4861 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004862 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4863 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4864
4865 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004866 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004867 MachineFunction::iterator It = BB;
4868 ++It;
4869
4870 unsigned dest = MI->getOperand(0).getReg();
4871 unsigned ptr = MI->getOperand(1).getReg();
4872 unsigned incr = MI->getOperand(2).getReg();
4873 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004874
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004875 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004876 unsigned ldrOpc, strOpc;
4877 switch (Size) {
4878 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004879 case 1:
4880 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004881 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004882 break;
4883 case 2:
4884 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4885 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4886 break;
4887 case 4:
4888 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4889 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4890 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004891 }
4892
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004893 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4894 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4895 MF->insert(It, loopMBB);
4896 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004897
4898 // Transfer the remainder of BB and its successor edges to exitMBB.
4899 exitMBB->splice(exitMBB->begin(), BB,
4900 llvm::next(MachineBasicBlock::iterator(MI)),
4901 BB->end());
4902 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004903
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004904 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004905 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4906 unsigned scratch2 = (!BinOpcode) ? incr :
4907 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4908
4909 // thisMBB:
4910 // ...
4911 // fallthrough --> loopMBB
4912 BB->addSuccessor(loopMBB);
4913
4914 // loopMBB:
4915 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004916 // <binop> scratch2, dest, incr
4917 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004918 // cmp scratch, #0
4919 // bne- loopMBB
4920 // fallthrough --> exitMBB
4921 BB = loopMBB;
4922 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004923 if (BinOpcode) {
4924 // operand order needs to go the other way for NAND
4925 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4926 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4927 addReg(incr).addReg(dest)).addReg(0);
4928 else
4929 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4930 addReg(dest).addReg(incr)).addReg(0);
4931 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004932
4933 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4934 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004935 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004936 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004937 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4938 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004939
4940 BB->addSuccessor(loopMBB);
4941 BB->addSuccessor(exitMBB);
4942
4943 // exitMBB:
4944 // ...
4945 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004946
Dan Gohman14152b42010-07-06 20:24:04 +00004947 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004948
Jim Grosbachc3c23542009-12-14 04:22:04 +00004949 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004950}
4951
Evan Cheng218977b2010-07-13 19:27:42 +00004952static
4953MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4954 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4955 E = MBB->succ_end(); I != E; ++I)
4956 if (*I != Succ)
4957 return *I;
4958 llvm_unreachable("Expecting a BB with two successors!");
4959}
4960
Jim Grosbache801dc42009-12-12 01:40:06 +00004961MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004962ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004963 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004964 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004965 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004966 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004967 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004968 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004969 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004970 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004971
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004972 case ARM::ATOMIC_LOAD_ADD_I8:
4973 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4974 case ARM::ATOMIC_LOAD_ADD_I16:
4975 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4976 case ARM::ATOMIC_LOAD_ADD_I32:
4977 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004978
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004979 case ARM::ATOMIC_LOAD_AND_I8:
4980 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4981 case ARM::ATOMIC_LOAD_AND_I16:
4982 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4983 case ARM::ATOMIC_LOAD_AND_I32:
4984 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004985
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004986 case ARM::ATOMIC_LOAD_OR_I8:
4987 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4988 case ARM::ATOMIC_LOAD_OR_I16:
4989 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4990 case ARM::ATOMIC_LOAD_OR_I32:
4991 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004992
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004993 case ARM::ATOMIC_LOAD_XOR_I8:
4994 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4995 case ARM::ATOMIC_LOAD_XOR_I16:
4996 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4997 case ARM::ATOMIC_LOAD_XOR_I32:
4998 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004999
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005000 case ARM::ATOMIC_LOAD_NAND_I8:
5001 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5002 case ARM::ATOMIC_LOAD_NAND_I16:
5003 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5004 case ARM::ATOMIC_LOAD_NAND_I32:
5005 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005006
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005007 case ARM::ATOMIC_LOAD_SUB_I8:
5008 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5009 case ARM::ATOMIC_LOAD_SUB_I16:
5010 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5011 case ARM::ATOMIC_LOAD_SUB_I32:
5012 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005013
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005014 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5015 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5016 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005017
5018 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5019 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5020 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005021
Owen Anderson76706012011-04-05 21:48:57 +00005022 case ARM::ADCSSri:
5023 case ARM::ADCSSrr:
5024 case ARM::ADCSSrs:
5025 case ARM::SBCSSri:
5026 case ARM::SBCSSrr:
Owen Andersonb48c7912011-04-05 23:55:28 +00005027 case ARM::SBCSSrs:
5028 case ARM::RSBSri:
5029 case ARM::RSBSrr:
5030 case ARM::RSBSrs:
5031 case ARM::RSCSri:
5032 case ARM::RSCSrs: {
Owen Anderson76706012011-04-05 21:48:57 +00005033 unsigned OldOpc = MI->getOpcode();
5034 unsigned Opc = 0;
5035 switch (OldOpc) {
5036 case ARM::ADCSSrr:
5037 Opc = ARM::ADCrr;
5038 break;
5039 case ARM::ADCSSri:
5040 Opc = ARM::ADCri;
5041 break;
5042 case ARM::ADCSSrs:
5043 Opc = ARM::ADCrs;
5044 break;
5045 case ARM::SBCSSrr:
5046 Opc = ARM::SBCrr;
5047 break;
5048 case ARM::SBCSSri:
5049 Opc = ARM::SBCri;
5050 break;
5051 case ARM::SBCSSrs:
5052 Opc = ARM::SBCrs;
5053 break;
Owen Andersonb48c7912011-04-05 23:55:28 +00005054 case ARM::RSBSri:
5055 Opc = ARM::RSBri;
5056 break;
5057 case ARM::RSBSrr:
5058 Opc = ARM::RSBrr;
5059 break;
5060 case ARM::RSBSrs:
5061 Opc = ARM::RSBrs;
5062 break;
5063 case ARM::RSCSri:
5064 Opc = ARM::RSCri;
5065 break;
5066 case ARM::RSCSrs:
5067 Opc = ARM::RSCrs;
5068 break;
Owen Anderson76706012011-04-05 21:48:57 +00005069 default:
5070 llvm_unreachable("Unknown opcode?");
5071 }
5072
5073 MachineInstrBuilder MIB =
5074 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(Opc));
5075 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5076 MIB.addOperand(MI->getOperand(i));
5077 AddDefaultPred(MIB);
5078 MIB.addReg(ARM::CPSR, RegState::Define); // S bit
5079 MI->eraseFromParent();
5080 return BB;
5081 }
5082
5083
Evan Cheng007ea272009-08-12 05:17:19 +00005084 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005085 // To "insert" a SELECT_CC instruction, we actually have to insert the
5086 // diamond control-flow pattern. The incoming instruction knows the
5087 // destination vreg to set, the condition code register to branch on, the
5088 // true/false values to select between, and a branch opcode to use.
5089 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005090 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005091 ++It;
5092
5093 // thisMBB:
5094 // ...
5095 // TrueVal = ...
5096 // cmpTY ccX, r1, r2
5097 // bCC copy1MBB
5098 // fallthrough --> copy0MBB
5099 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005100 MachineFunction *F = BB->getParent();
5101 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5102 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005103 F->insert(It, copy0MBB);
5104 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005105
5106 // Transfer the remainder of BB and its successor edges to sinkMBB.
5107 sinkMBB->splice(sinkMBB->begin(), BB,
5108 llvm::next(MachineBasicBlock::iterator(MI)),
5109 BB->end());
5110 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5111
Dan Gohman258c58c2010-07-06 15:49:48 +00005112 BB->addSuccessor(copy0MBB);
5113 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005114
Dan Gohman14152b42010-07-06 20:24:04 +00005115 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5116 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5117
Evan Chenga8e29892007-01-19 07:51:42 +00005118 // copy0MBB:
5119 // %FalseValue = ...
5120 // # fallthrough to sinkMBB
5121 BB = copy0MBB;
5122
5123 // Update machine-CFG edges
5124 BB->addSuccessor(sinkMBB);
5125
5126 // sinkMBB:
5127 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5128 // ...
5129 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005130 BuildMI(*BB, BB->begin(), dl,
5131 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005132 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5133 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5134
Dan Gohman14152b42010-07-06 20:24:04 +00005135 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005136 return BB;
5137 }
Evan Cheng86198642009-08-07 00:34:42 +00005138
Evan Cheng218977b2010-07-13 19:27:42 +00005139 case ARM::BCCi64:
5140 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005141 // If there is an unconditional branch to the other successor, remove it.
5142 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005143
Evan Cheng218977b2010-07-13 19:27:42 +00005144 // Compare both parts that make up the double comparison separately for
5145 // equality.
5146 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5147
5148 unsigned LHS1 = MI->getOperand(1).getReg();
5149 unsigned LHS2 = MI->getOperand(2).getReg();
5150 if (RHSisZero) {
5151 AddDefaultPred(BuildMI(BB, dl,
5152 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5153 .addReg(LHS1).addImm(0));
5154 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5155 .addReg(LHS2).addImm(0)
5156 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5157 } else {
5158 unsigned RHS1 = MI->getOperand(3).getReg();
5159 unsigned RHS2 = MI->getOperand(4).getReg();
5160 AddDefaultPred(BuildMI(BB, dl,
5161 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5162 .addReg(LHS1).addReg(RHS1));
5163 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5164 .addReg(LHS2).addReg(RHS2)
5165 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5166 }
5167
5168 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5169 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5170 if (MI->getOperand(0).getImm() == ARMCC::NE)
5171 std::swap(destMBB, exitMBB);
5172
5173 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5174 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5175 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5176 .addMBB(exitMBB);
5177
5178 MI->eraseFromParent(); // The pseudo instruction is gone now.
5179 return BB;
5180 }
Evan Chenga8e29892007-01-19 07:51:42 +00005181 }
5182}
5183
5184//===----------------------------------------------------------------------===//
5185// ARM Optimization Hooks
5186//===----------------------------------------------------------------------===//
5187
Chris Lattnerd1980a52009-03-12 06:52:53 +00005188static
5189SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5190 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005191 SelectionDAG &DAG = DCI.DAG;
5192 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005193 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005194 unsigned Opc = N->getOpcode();
5195 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5196 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5197 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5198 ISD::CondCode CC = ISD::SETCC_INVALID;
5199
5200 if (isSlctCC) {
5201 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5202 } else {
5203 SDValue CCOp = Slct.getOperand(0);
5204 if (CCOp.getOpcode() == ISD::SETCC)
5205 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5206 }
5207
5208 bool DoXform = false;
5209 bool InvCC = false;
5210 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5211 "Bad input!");
5212
5213 if (LHS.getOpcode() == ISD::Constant &&
5214 cast<ConstantSDNode>(LHS)->isNullValue()) {
5215 DoXform = true;
5216 } else if (CC != ISD::SETCC_INVALID &&
5217 RHS.getOpcode() == ISD::Constant &&
5218 cast<ConstantSDNode>(RHS)->isNullValue()) {
5219 std::swap(LHS, RHS);
5220 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005221 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005222 Op0.getOperand(0).getValueType();
5223 bool isInt = OpVT.isInteger();
5224 CC = ISD::getSetCCInverse(CC, isInt);
5225
5226 if (!TLI.isCondCodeLegal(CC, OpVT))
5227 return SDValue(); // Inverse operator isn't legal.
5228
5229 DoXform = true;
5230 InvCC = true;
5231 }
5232
5233 if (DoXform) {
5234 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5235 if (isSlctCC)
5236 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5237 Slct.getOperand(0), Slct.getOperand(1), CC);
5238 SDValue CCOp = Slct.getOperand(0);
5239 if (InvCC)
5240 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5241 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5242 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5243 CCOp, OtherOp, Result);
5244 }
5245 return SDValue();
5246}
5247
Bob Wilson3d5792a2010-07-29 20:34:14 +00005248/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5249/// operands N0 and N1. This is a helper for PerformADDCombine that is
5250/// called with the default operands, and if that fails, with commuted
5251/// operands.
5252static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5253 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005254 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5255 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5256 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5257 if (Result.getNode()) return Result;
5258 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005259 return SDValue();
5260}
5261
Bob Wilson3d5792a2010-07-29 20:34:14 +00005262/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5263///
5264static SDValue PerformADDCombine(SDNode *N,
5265 TargetLowering::DAGCombinerInfo &DCI) {
5266 SDValue N0 = N->getOperand(0);
5267 SDValue N1 = N->getOperand(1);
5268
5269 // First try with the default operand order.
5270 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5271 if (Result.getNode())
5272 return Result;
5273
5274 // If that didn't work, try again with the operands commuted.
5275 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5276}
5277
Chris Lattnerd1980a52009-03-12 06:52:53 +00005278/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005279///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005280static SDValue PerformSUBCombine(SDNode *N,
5281 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005282 SDValue N0 = N->getOperand(0);
5283 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005284
Chris Lattnerd1980a52009-03-12 06:52:53 +00005285 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5286 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5287 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5288 if (Result.getNode()) return Result;
5289 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005290
Chris Lattnerd1980a52009-03-12 06:52:53 +00005291 return SDValue();
5292}
5293
Evan Cheng463d3582011-03-31 19:38:48 +00005294/// PerformVMULCombine
5295/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5296/// special multiplier accumulator forwarding.
5297/// vmul d3, d0, d2
5298/// vmla d3, d1, d2
5299/// is faster than
5300/// vadd d3, d0, d1
5301/// vmul d3, d3, d2
5302static SDValue PerformVMULCombine(SDNode *N,
5303 TargetLowering::DAGCombinerInfo &DCI,
5304 const ARMSubtarget *Subtarget) {
5305 if (!Subtarget->hasVMLxForwarding())
5306 return SDValue();
5307
5308 SelectionDAG &DAG = DCI.DAG;
5309 SDValue N0 = N->getOperand(0);
5310 SDValue N1 = N->getOperand(1);
5311 unsigned Opcode = N0.getOpcode();
5312 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5313 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
5314 Opcode = N0.getOpcode();
5315 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
5316 Opcode != ISD::FADD && Opcode != ISD::FSUB)
5317 return SDValue();
5318 std::swap(N0, N1);
5319 }
5320
5321 EVT VT = N->getValueType(0);
5322 DebugLoc DL = N->getDebugLoc();
5323 SDValue N00 = N0->getOperand(0);
5324 SDValue N01 = N0->getOperand(1);
5325 return DAG.getNode(Opcode, DL, VT,
5326 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
5327 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
5328}
5329
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005330static SDValue PerformMULCombine(SDNode *N,
5331 TargetLowering::DAGCombinerInfo &DCI,
5332 const ARMSubtarget *Subtarget) {
5333 SelectionDAG &DAG = DCI.DAG;
5334
5335 if (Subtarget->isThumb1Only())
5336 return SDValue();
5337
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005338 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5339 return SDValue();
5340
5341 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00005342 if (VT.is64BitVector() || VT.is128BitVector())
5343 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005344 if (VT != MVT::i32)
5345 return SDValue();
5346
5347 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5348 if (!C)
5349 return SDValue();
5350
5351 uint64_t MulAmt = C->getZExtValue();
5352 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5353 ShiftAmt = ShiftAmt & (32 - 1);
5354 SDValue V = N->getOperand(0);
5355 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005356
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005357 SDValue Res;
5358 MulAmt >>= ShiftAmt;
5359 if (isPowerOf2_32(MulAmt - 1)) {
5360 // (mul x, 2^N + 1) => (add (shl x, N), x)
5361 Res = DAG.getNode(ISD::ADD, DL, VT,
5362 V, DAG.getNode(ISD::SHL, DL, VT,
5363 V, DAG.getConstant(Log2_32(MulAmt-1),
5364 MVT::i32)));
5365 } else if (isPowerOf2_32(MulAmt + 1)) {
5366 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5367 Res = DAG.getNode(ISD::SUB, DL, VT,
5368 DAG.getNode(ISD::SHL, DL, VT,
5369 V, DAG.getConstant(Log2_32(MulAmt+1),
5370 MVT::i32)),
5371 V);
5372 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005373 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005374
5375 if (ShiftAmt != 0)
5376 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5377 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005378
5379 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005380 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005381 return SDValue();
5382}
5383
Owen Anderson080c0922010-11-05 19:27:46 +00005384static SDValue PerformANDCombine(SDNode *N,
5385 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00005386
Owen Anderson080c0922010-11-05 19:27:46 +00005387 // Attempt to use immediate-form VBIC
5388 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5389 DebugLoc dl = N->getDebugLoc();
5390 EVT VT = N->getValueType(0);
5391 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005392
Tanya Lattner0433b212011-04-07 15:24:20 +00005393 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5394 return SDValue();
5395
Owen Anderson080c0922010-11-05 19:27:46 +00005396 APInt SplatBits, SplatUndef;
5397 unsigned SplatBitSize;
5398 bool HasAnyUndefs;
5399 if (BVN &&
5400 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5401 if (SplatBitSize <= 64) {
5402 EVT VbicVT;
5403 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5404 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005405 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005406 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005407 if (Val.getNode()) {
5408 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005409 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005410 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005411 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005412 }
5413 }
5414 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005415
Owen Anderson080c0922010-11-05 19:27:46 +00005416 return SDValue();
5417}
5418
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005419/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5420static SDValue PerformORCombine(SDNode *N,
5421 TargetLowering::DAGCombinerInfo &DCI,
5422 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005423 // Attempt to use immediate-form VORR
5424 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5425 DebugLoc dl = N->getDebugLoc();
5426 EVT VT = N->getValueType(0);
5427 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005428
Tanya Lattner0433b212011-04-07 15:24:20 +00005429 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
5430 return SDValue();
5431
Owen Anderson60f48702010-11-03 23:15:26 +00005432 APInt SplatBits, SplatUndef;
5433 unsigned SplatBitSize;
5434 bool HasAnyUndefs;
5435 if (BVN && Subtarget->hasNEON() &&
5436 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5437 if (SplatBitSize <= 64) {
5438 EVT VorrVT;
5439 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5440 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005441 DAG, VorrVT, VT.is128BitVector(),
5442 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005443 if (Val.getNode()) {
5444 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005445 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005446 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005447 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005448 }
5449 }
5450 }
5451
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00005452 SDValue N0 = N->getOperand(0);
5453 if (N0.getOpcode() != ISD::AND)
5454 return SDValue();
5455 SDValue N1 = N->getOperand(1);
5456
5457 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
5458 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
5459 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
5460 APInt SplatUndef;
5461 unsigned SplatBitSize;
5462 bool HasAnyUndefs;
5463
5464 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
5465 APInt SplatBits0;
5466 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
5467 HasAnyUndefs) && !HasAnyUndefs) {
5468 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
5469 APInt SplatBits1;
5470 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
5471 HasAnyUndefs) && !HasAnyUndefs &&
5472 SplatBits0 == ~SplatBits1) {
5473 // Canonicalize the vector type to make instruction selection simpler.
5474 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5475 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
5476 N0->getOperand(1), N0->getOperand(0),
5477 N1->getOperand(1));
5478 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
5479 }
5480 }
5481 }
5482
Jim Grosbach54238562010-07-17 03:30:54 +00005483 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5484 // reasonable.
5485
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005486 // BFI is only available on V6T2+
5487 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5488 return SDValue();
5489
Jim Grosbach54238562010-07-17 03:30:54 +00005490 DebugLoc DL = N->getDebugLoc();
5491 // 1) or (and A, mask), val => ARMbfi A, val, mask
5492 // iff (val & mask) == val
5493 //
5494 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5495 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005496 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005497 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005498 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005499 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005500
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005501 if (VT != MVT::i32)
5502 return SDValue();
5503
Evan Cheng30fb13f2010-12-13 20:32:54 +00005504 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005505
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005506 // The value and the mask need to be constants so we can verify this is
5507 // actually a bitfield set. If the mask is 0xffff, we can do better
5508 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005509 SDValue MaskOp = N0.getOperand(1);
5510 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5511 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005512 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005513 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005514 if (Mask == 0xffff)
5515 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005516 SDValue Res;
5517 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005518 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5519 if (N1C) {
5520 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005521 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005522 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005523
Evan Chenga9688c42010-12-11 04:11:38 +00005524 if (ARM::isBitFieldInvertedMask(Mask)) {
5525 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005526
Evan Cheng30fb13f2010-12-13 20:32:54 +00005527 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005528 DAG.getConstant(Val, MVT::i32),
5529 DAG.getConstant(Mask, MVT::i32));
5530
5531 // Do not add new nodes to DAG combiner worklist.
5532 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005533 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005534 }
Jim Grosbach54238562010-07-17 03:30:54 +00005535 } else if (N1.getOpcode() == ISD::AND) {
5536 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005537 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5538 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005539 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005540 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005541
Eric Christopher29aeed12011-03-26 01:21:03 +00005542 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5543 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005544 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005545 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005546 // The pack halfword instruction works better for masks that fit it,
5547 // so use that when it's available.
5548 if (Subtarget->hasT2ExtractPack() &&
5549 (Mask == 0xffff || Mask == 0xffff0000))
5550 return SDValue();
5551 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005552 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005553 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005554 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005555 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005556 DAG.getConstant(Mask, MVT::i32));
5557 // Do not add new nodes to DAG combiner worklist.
5558 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005559 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005560 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005561 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005562 // The pack halfword instruction works better for masks that fit it,
5563 // so use that when it's available.
5564 if (Subtarget->hasT2ExtractPack() &&
5565 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5566 return SDValue();
5567 // 2b
5568 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005569 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005570 DAG.getConstant(lsb, MVT::i32));
5571 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005572 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005573 // Do not add new nodes to DAG combiner worklist.
5574 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005575 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005576 }
5577 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005578
Evan Cheng30fb13f2010-12-13 20:32:54 +00005579 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5580 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5581 ARM::isBitFieldInvertedMask(~Mask)) {
5582 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5583 // where lsb(mask) == #shamt and masked bits of B are known zero.
5584 SDValue ShAmt = N00.getOperand(1);
5585 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5586 unsigned LSB = CountTrailingZeros_32(Mask);
5587 if (ShAmtC != LSB)
5588 return SDValue();
5589
5590 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5591 DAG.getConstant(~Mask, MVT::i32));
5592
5593 // Do not add new nodes to DAG combiner worklist.
5594 DCI.CombineTo(N, Res, false);
5595 }
5596
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005597 return SDValue();
5598}
5599
Evan Cheng0c1aec12010-12-14 03:22:07 +00005600/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5601/// C1 & C2 == C1.
5602static SDValue PerformBFICombine(SDNode *N,
5603 TargetLowering::DAGCombinerInfo &DCI) {
5604 SDValue N1 = N->getOperand(1);
5605 if (N1.getOpcode() == ISD::AND) {
5606 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5607 if (!N11C)
5608 return SDValue();
5609 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5610 unsigned Mask2 = N11C->getZExtValue();
5611 if ((Mask & Mask2) == Mask2)
5612 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5613 N->getOperand(0), N1.getOperand(0),
5614 N->getOperand(2));
5615 }
5616 return SDValue();
5617}
5618
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005619/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5620/// ARMISD::VMOVRRD.
5621static SDValue PerformVMOVRRDCombine(SDNode *N,
5622 TargetLowering::DAGCombinerInfo &DCI) {
5623 // vmovrrd(vmovdrr x, y) -> x,y
5624 SDValue InDouble = N->getOperand(0);
5625 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5626 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00005627
5628 // vmovrrd(load f64) -> (load i32), (load i32)
5629 SDNode *InNode = InDouble.getNode();
5630 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
5631 InNode->getValueType(0) == MVT::f64 &&
5632 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
5633 !cast<LoadSDNode>(InNode)->isVolatile()) {
5634 // TODO: Should this be done for non-FrameIndex operands?
5635 LoadSDNode *LD = cast<LoadSDNode>(InNode);
5636
5637 SelectionDAG &DAG = DCI.DAG;
5638 DebugLoc DL = LD->getDebugLoc();
5639 SDValue BasePtr = LD->getBasePtr();
5640 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
5641 LD->getPointerInfo(), LD->isVolatile(),
5642 LD->isNonTemporal(), LD->getAlignment());
5643
5644 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
5645 DAG.getConstant(4, MVT::i32));
5646 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
5647 LD->getPointerInfo(), LD->isVolatile(),
5648 LD->isNonTemporal(),
5649 std::min(4U, LD->getAlignment() / 2));
5650
5651 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
5652 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
5653 DCI.RemoveFromWorklist(LD);
5654 DAG.DeleteNode(LD);
5655 return Result;
5656 }
5657
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005658 return SDValue();
5659}
5660
5661/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5662/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5663static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5664 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5665 SDValue Op0 = N->getOperand(0);
5666 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005667 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005668 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005669 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005670 Op1 = Op1.getOperand(0);
5671 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5672 Op0.getNode() == Op1.getNode() &&
5673 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005674 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005675 N->getValueType(0), Op0.getOperand(0));
5676 return SDValue();
5677}
5678
Bob Wilson31600902010-12-21 06:43:19 +00005679/// PerformSTORECombine - Target-specific dag combine xforms for
5680/// ISD::STORE.
5681static SDValue PerformSTORECombine(SDNode *N,
5682 TargetLowering::DAGCombinerInfo &DCI) {
5683 // Bitcast an i64 store extracted from a vector to f64.
5684 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5685 StoreSDNode *St = cast<StoreSDNode>(N);
5686 SDValue StVal = St->getValue();
5687 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5688 StVal.getValueType() != MVT::i64 ||
5689 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5690 return SDValue();
5691
5692 SelectionDAG &DAG = DCI.DAG;
5693 DebugLoc dl = StVal.getDebugLoc();
5694 SDValue IntVec = StVal.getOperand(0);
5695 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5696 IntVec.getValueType().getVectorNumElements());
5697 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5698 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5699 Vec, StVal.getOperand(1));
5700 dl = N->getDebugLoc();
5701 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5702 // Make the DAGCombiner fold the bitcasts.
5703 DCI.AddToWorklist(Vec.getNode());
5704 DCI.AddToWorklist(ExtElt.getNode());
5705 DCI.AddToWorklist(V.getNode());
5706 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5707 St->getPointerInfo(), St->isVolatile(),
5708 St->isNonTemporal(), St->getAlignment(),
5709 St->getTBAAInfo());
5710}
5711
5712/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5713/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5714/// i64 vector to have f64 elements, since the value can then be loaded
5715/// directly into a VFP register.
5716static bool hasNormalLoadOperand(SDNode *N) {
5717 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5718 for (unsigned i = 0; i < NumElts; ++i) {
5719 SDNode *Elt = N->getOperand(i).getNode();
5720 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5721 return true;
5722 }
5723 return false;
5724}
5725
Bob Wilson75f02882010-09-17 22:59:05 +00005726/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5727/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005728static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5729 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005730 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5731 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5732 // into a pair of GPRs, which is fine when the value is used as a scalar,
5733 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005734 SelectionDAG &DAG = DCI.DAG;
5735 if (N->getNumOperands() == 2) {
5736 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5737 if (RV.getNode())
5738 return RV;
5739 }
Bob Wilson75f02882010-09-17 22:59:05 +00005740
Bob Wilson31600902010-12-21 06:43:19 +00005741 // Load i64 elements as f64 values so that type legalization does not split
5742 // them up into i32 values.
5743 EVT VT = N->getValueType(0);
5744 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5745 return SDValue();
5746 DebugLoc dl = N->getDebugLoc();
5747 SmallVector<SDValue, 8> Ops;
5748 unsigned NumElts = VT.getVectorNumElements();
5749 for (unsigned i = 0; i < NumElts; ++i) {
5750 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5751 Ops.push_back(V);
5752 // Make the DAGCombiner fold the bitcast.
5753 DCI.AddToWorklist(V.getNode());
5754 }
5755 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5756 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5757 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5758}
5759
5760/// PerformInsertEltCombine - Target-specific dag combine xforms for
5761/// ISD::INSERT_VECTOR_ELT.
5762static SDValue PerformInsertEltCombine(SDNode *N,
5763 TargetLowering::DAGCombinerInfo &DCI) {
5764 // Bitcast an i64 load inserted into a vector to f64.
5765 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5766 EVT VT = N->getValueType(0);
5767 SDNode *Elt = N->getOperand(1).getNode();
5768 if (VT.getVectorElementType() != MVT::i64 ||
5769 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5770 return SDValue();
5771
5772 SelectionDAG &DAG = DCI.DAG;
5773 DebugLoc dl = N->getDebugLoc();
5774 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5775 VT.getVectorNumElements());
5776 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5777 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5778 // Make the DAGCombiner fold the bitcasts.
5779 DCI.AddToWorklist(Vec.getNode());
5780 DCI.AddToWorklist(V.getNode());
5781 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5782 Vec, V, N->getOperand(2));
5783 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005784}
5785
Bob Wilsonf20700c2010-10-27 20:38:28 +00005786/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5787/// ISD::VECTOR_SHUFFLE.
5788static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5789 // The LLVM shufflevector instruction does not require the shuffle mask
5790 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5791 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5792 // operands do not match the mask length, they are extended by concatenating
5793 // them with undef vectors. That is probably the right thing for other
5794 // targets, but for NEON it is better to concatenate two double-register
5795 // size vector operands into a single quad-register size vector. Do that
5796 // transformation here:
5797 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5798 // shuffle(concat(v1, v2), undef)
5799 SDValue Op0 = N->getOperand(0);
5800 SDValue Op1 = N->getOperand(1);
5801 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5802 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5803 Op0.getNumOperands() != 2 ||
5804 Op1.getNumOperands() != 2)
5805 return SDValue();
5806 SDValue Concat0Op1 = Op0.getOperand(1);
5807 SDValue Concat1Op1 = Op1.getOperand(1);
5808 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5809 Concat1Op1.getOpcode() != ISD::UNDEF)
5810 return SDValue();
5811 // Skip the transformation if any of the types are illegal.
5812 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5813 EVT VT = N->getValueType(0);
5814 if (!TLI.isTypeLegal(VT) ||
5815 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5816 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5817 return SDValue();
5818
5819 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5820 Op0.getOperand(0), Op1.getOperand(0));
5821 // Translate the shuffle mask.
5822 SmallVector<int, 16> NewMask;
5823 unsigned NumElts = VT.getVectorNumElements();
5824 unsigned HalfElts = NumElts/2;
5825 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5826 for (unsigned n = 0; n < NumElts; ++n) {
5827 int MaskElt = SVN->getMaskElt(n);
5828 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005829 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005830 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005831 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005832 NewElt = HalfElts + MaskElt - NumElts;
5833 NewMask.push_back(NewElt);
5834 }
5835 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5836 DAG.getUNDEF(VT), NewMask.data());
5837}
5838
Bob Wilson1c3ef902011-02-07 17:43:21 +00005839/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5840/// NEON load/store intrinsics to merge base address updates.
5841static SDValue CombineBaseUpdate(SDNode *N,
5842 TargetLowering::DAGCombinerInfo &DCI) {
5843 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5844 return SDValue();
5845
5846 SelectionDAG &DAG = DCI.DAG;
5847 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5848 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5849 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5850 SDValue Addr = N->getOperand(AddrOpIdx);
5851
5852 // Search for a use of the address operand that is an increment.
5853 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5854 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5855 SDNode *User = *UI;
5856 if (User->getOpcode() != ISD::ADD ||
5857 UI.getUse().getResNo() != Addr.getResNo())
5858 continue;
5859
5860 // Check that the add is independent of the load/store. Otherwise, folding
5861 // it would create a cycle.
5862 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5863 continue;
5864
5865 // Find the new opcode for the updating load/store.
5866 bool isLoad = true;
5867 bool isLaneOp = false;
5868 unsigned NewOpc = 0;
5869 unsigned NumVecs = 0;
5870 if (isIntrinsic) {
5871 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5872 switch (IntNo) {
5873 default: assert(0 && "unexpected intrinsic for Neon base update");
5874 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5875 NumVecs = 1; break;
5876 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5877 NumVecs = 2; break;
5878 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5879 NumVecs = 3; break;
5880 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5881 NumVecs = 4; break;
5882 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5883 NumVecs = 2; isLaneOp = true; break;
5884 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5885 NumVecs = 3; isLaneOp = true; break;
5886 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5887 NumVecs = 4; isLaneOp = true; break;
5888 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5889 NumVecs = 1; isLoad = false; break;
5890 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
5891 NumVecs = 2; isLoad = false; break;
5892 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
5893 NumVecs = 3; isLoad = false; break;
5894 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
5895 NumVecs = 4; isLoad = false; break;
5896 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
5897 NumVecs = 2; isLoad = false; isLaneOp = true; break;
5898 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
5899 NumVecs = 3; isLoad = false; isLaneOp = true; break;
5900 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
5901 NumVecs = 4; isLoad = false; isLaneOp = true; break;
5902 }
5903 } else {
5904 isLaneOp = true;
5905 switch (N->getOpcode()) {
5906 default: assert(0 && "unexpected opcode for Neon base update");
5907 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
5908 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
5909 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
5910 }
5911 }
5912
5913 // Find the size of memory referenced by the load/store.
5914 EVT VecTy;
5915 if (isLoad)
5916 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00005917 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00005918 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
5919 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
5920 if (isLaneOp)
5921 NumBytes /= VecTy.getVectorNumElements();
5922
5923 // If the increment is a constant, it must match the memory ref size.
5924 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
5925 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
5926 uint64_t IncVal = CInc->getZExtValue();
5927 if (IncVal != NumBytes)
5928 continue;
5929 } else if (NumBytes >= 3 * 16) {
5930 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
5931 // separate instructions that make it harder to use a non-constant update.
5932 continue;
5933 }
5934
5935 // Create the new updating load/store node.
5936 EVT Tys[6];
5937 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
5938 unsigned n;
5939 for (n = 0; n < NumResultVecs; ++n)
5940 Tys[n] = VecTy;
5941 Tys[n++] = MVT::i32;
5942 Tys[n] = MVT::Other;
5943 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
5944 SmallVector<SDValue, 8> Ops;
5945 Ops.push_back(N->getOperand(0)); // incoming chain
5946 Ops.push_back(N->getOperand(AddrOpIdx));
5947 Ops.push_back(Inc);
5948 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
5949 Ops.push_back(N->getOperand(i));
5950 }
5951 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
5952 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
5953 Ops.data(), Ops.size(),
5954 MemInt->getMemoryVT(),
5955 MemInt->getMemOperand());
5956
5957 // Update the uses.
5958 std::vector<SDValue> NewResults;
5959 for (unsigned i = 0; i < NumResultVecs; ++i) {
5960 NewResults.push_back(SDValue(UpdN.getNode(), i));
5961 }
5962 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
5963 DCI.CombineTo(N, NewResults);
5964 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
5965
5966 break;
Owen Anderson76706012011-04-05 21:48:57 +00005967 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00005968 return SDValue();
5969}
5970
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005971/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5972/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5973/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5974/// return true.
5975static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5976 SelectionDAG &DAG = DCI.DAG;
5977 EVT VT = N->getValueType(0);
5978 // vldN-dup instructions only support 64-bit vectors for N > 1.
5979 if (!VT.is64BitVector())
5980 return false;
5981
5982 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5983 SDNode *VLD = N->getOperand(0).getNode();
5984 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5985 return false;
5986 unsigned NumVecs = 0;
5987 unsigned NewOpc = 0;
5988 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5989 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5990 NumVecs = 2;
5991 NewOpc = ARMISD::VLD2DUP;
5992 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5993 NumVecs = 3;
5994 NewOpc = ARMISD::VLD3DUP;
5995 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5996 NumVecs = 4;
5997 NewOpc = ARMISD::VLD4DUP;
5998 } else {
5999 return false;
6000 }
6001
6002 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6003 // numbers match the load.
6004 unsigned VLDLaneNo =
6005 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6006 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6007 UI != UE; ++UI) {
6008 // Ignore uses of the chain result.
6009 if (UI.getUse().getResNo() == NumVecs)
6010 continue;
6011 SDNode *User = *UI;
6012 if (User->getOpcode() != ARMISD::VDUPLANE ||
6013 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6014 return false;
6015 }
6016
6017 // Create the vldN-dup node.
6018 EVT Tys[5];
6019 unsigned n;
6020 for (n = 0; n < NumVecs; ++n)
6021 Tys[n] = VT;
6022 Tys[n] = MVT::Other;
6023 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6024 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6025 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6026 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6027 Ops, 2, VLDMemInt->getMemoryVT(),
6028 VLDMemInt->getMemOperand());
6029
6030 // Update the uses.
6031 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6032 UI != UE; ++UI) {
6033 unsigned ResNo = UI.getUse().getResNo();
6034 // Ignore uses of the chain result.
6035 if (ResNo == NumVecs)
6036 continue;
6037 SDNode *User = *UI;
6038 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6039 }
6040
6041 // Now the vldN-lane intrinsic is dead except for its chain result.
6042 // Update uses of the chain.
6043 std::vector<SDValue> VLDDupResults;
6044 for (unsigned n = 0; n < NumVecs; ++n)
6045 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6046 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6047 DCI.CombineTo(VLD, VLDDupResults);
6048
6049 return true;
6050}
6051
Bob Wilson9e82bf12010-07-14 01:22:12 +00006052/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6053/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006054static SDValue PerformVDUPLANECombine(SDNode *N,
6055 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006056 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006057
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006058 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6059 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6060 if (CombineVLDDUP(N, DCI))
6061 return SDValue(N, 0);
6062
6063 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6064 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006065 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006066 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006067 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006068 return SDValue();
6069
6070 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6071 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6072 // The canonical VMOV for a zero vector uses a 32-bit element size.
6073 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6074 unsigned EltBits;
6075 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6076 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006077 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006078 if (EltSize > VT.getVectorElementType().getSizeInBits())
6079 return SDValue();
6080
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006081 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006082}
6083
Bob Wilson5bafff32009-06-22 23:27:02 +00006084/// getVShiftImm - Check if this is a valid build_vector for the immediate
6085/// operand of a vector shift operation, where all the elements of the
6086/// build_vector must have the same constant integer value.
6087static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6088 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006089 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006090 Op = Op.getOperand(0);
6091 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6092 APInt SplatBits, SplatUndef;
6093 unsigned SplatBitSize;
6094 bool HasAnyUndefs;
6095 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6096 HasAnyUndefs, ElementBits) ||
6097 SplatBitSize > ElementBits)
6098 return false;
6099 Cnt = SplatBits.getSExtValue();
6100 return true;
6101}
6102
6103/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6104/// operand of a vector shift left operation. That value must be in the range:
6105/// 0 <= Value < ElementBits for a left shift; or
6106/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006107static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006108 assert(VT.isVector() && "vector shift count is not a vector type");
6109 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6110 if (! getVShiftImm(Op, ElementBits, Cnt))
6111 return false;
6112 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6113}
6114
6115/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6116/// operand of a vector shift right operation. For a shift opcode, the value
6117/// is positive, but for an intrinsic the value count must be negative. The
6118/// absolute value must be in the range:
6119/// 1 <= |Value| <= ElementBits for a right shift; or
6120/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006121static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006122 int64_t &Cnt) {
6123 assert(VT.isVector() && "vector shift count is not a vector type");
6124 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6125 if (! getVShiftImm(Op, ElementBits, Cnt))
6126 return false;
6127 if (isIntrinsic)
6128 Cnt = -Cnt;
6129 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6130}
6131
6132/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6133static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6134 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6135 switch (IntNo) {
6136 default:
6137 // Don't do anything for most intrinsics.
6138 break;
6139
6140 // Vector shifts: check for immediate versions and lower them.
6141 // Note: This is done during DAG combining instead of DAG legalizing because
6142 // the build_vectors for 64-bit vector element shift counts are generally
6143 // not legal, and it is hard to see their values after they get legalized to
6144 // loads from a constant pool.
6145 case Intrinsic::arm_neon_vshifts:
6146 case Intrinsic::arm_neon_vshiftu:
6147 case Intrinsic::arm_neon_vshiftls:
6148 case Intrinsic::arm_neon_vshiftlu:
6149 case Intrinsic::arm_neon_vshiftn:
6150 case Intrinsic::arm_neon_vrshifts:
6151 case Intrinsic::arm_neon_vrshiftu:
6152 case Intrinsic::arm_neon_vrshiftn:
6153 case Intrinsic::arm_neon_vqshifts:
6154 case Intrinsic::arm_neon_vqshiftu:
6155 case Intrinsic::arm_neon_vqshiftsu:
6156 case Intrinsic::arm_neon_vqshiftns:
6157 case Intrinsic::arm_neon_vqshiftnu:
6158 case Intrinsic::arm_neon_vqshiftnsu:
6159 case Intrinsic::arm_neon_vqrshiftns:
6160 case Intrinsic::arm_neon_vqrshiftnu:
6161 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006162 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006163 int64_t Cnt;
6164 unsigned VShiftOpc = 0;
6165
6166 switch (IntNo) {
6167 case Intrinsic::arm_neon_vshifts:
6168 case Intrinsic::arm_neon_vshiftu:
6169 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6170 VShiftOpc = ARMISD::VSHL;
6171 break;
6172 }
6173 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6174 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6175 ARMISD::VSHRs : ARMISD::VSHRu);
6176 break;
6177 }
6178 return SDValue();
6179
6180 case Intrinsic::arm_neon_vshiftls:
6181 case Intrinsic::arm_neon_vshiftlu:
6182 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6183 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006184 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006185
6186 case Intrinsic::arm_neon_vrshifts:
6187 case Intrinsic::arm_neon_vrshiftu:
6188 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6189 break;
6190 return SDValue();
6191
6192 case Intrinsic::arm_neon_vqshifts:
6193 case Intrinsic::arm_neon_vqshiftu:
6194 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6195 break;
6196 return SDValue();
6197
6198 case Intrinsic::arm_neon_vqshiftsu:
6199 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6200 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006201 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006202
6203 case Intrinsic::arm_neon_vshiftn:
6204 case Intrinsic::arm_neon_vrshiftn:
6205 case Intrinsic::arm_neon_vqshiftns:
6206 case Intrinsic::arm_neon_vqshiftnu:
6207 case Intrinsic::arm_neon_vqshiftnsu:
6208 case Intrinsic::arm_neon_vqrshiftns:
6209 case Intrinsic::arm_neon_vqrshiftnu:
6210 case Intrinsic::arm_neon_vqrshiftnsu:
6211 // Narrowing shifts require an immediate right shift.
6212 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6213 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006214 llvm_unreachable("invalid shift count for narrowing vector shift "
6215 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006216
6217 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006218 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006219 }
6220
6221 switch (IntNo) {
6222 case Intrinsic::arm_neon_vshifts:
6223 case Intrinsic::arm_neon_vshiftu:
6224 // Opcode already set above.
6225 break;
6226 case Intrinsic::arm_neon_vshiftls:
6227 case Intrinsic::arm_neon_vshiftlu:
6228 if (Cnt == VT.getVectorElementType().getSizeInBits())
6229 VShiftOpc = ARMISD::VSHLLi;
6230 else
6231 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6232 ARMISD::VSHLLs : ARMISD::VSHLLu);
6233 break;
6234 case Intrinsic::arm_neon_vshiftn:
6235 VShiftOpc = ARMISD::VSHRN; break;
6236 case Intrinsic::arm_neon_vrshifts:
6237 VShiftOpc = ARMISD::VRSHRs; break;
6238 case Intrinsic::arm_neon_vrshiftu:
6239 VShiftOpc = ARMISD::VRSHRu; break;
6240 case Intrinsic::arm_neon_vrshiftn:
6241 VShiftOpc = ARMISD::VRSHRN; break;
6242 case Intrinsic::arm_neon_vqshifts:
6243 VShiftOpc = ARMISD::VQSHLs; break;
6244 case Intrinsic::arm_neon_vqshiftu:
6245 VShiftOpc = ARMISD::VQSHLu; break;
6246 case Intrinsic::arm_neon_vqshiftsu:
6247 VShiftOpc = ARMISD::VQSHLsu; break;
6248 case Intrinsic::arm_neon_vqshiftns:
6249 VShiftOpc = ARMISD::VQSHRNs; break;
6250 case Intrinsic::arm_neon_vqshiftnu:
6251 VShiftOpc = ARMISD::VQSHRNu; break;
6252 case Intrinsic::arm_neon_vqshiftnsu:
6253 VShiftOpc = ARMISD::VQSHRNsu; break;
6254 case Intrinsic::arm_neon_vqrshiftns:
6255 VShiftOpc = ARMISD::VQRSHRNs; break;
6256 case Intrinsic::arm_neon_vqrshiftnu:
6257 VShiftOpc = ARMISD::VQRSHRNu; break;
6258 case Intrinsic::arm_neon_vqrshiftnsu:
6259 VShiftOpc = ARMISD::VQRSHRNsu; break;
6260 }
6261
6262 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006263 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006264 }
6265
6266 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006267 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006268 int64_t Cnt;
6269 unsigned VShiftOpc = 0;
6270
6271 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6272 VShiftOpc = ARMISD::VSLI;
6273 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6274 VShiftOpc = ARMISD::VSRI;
6275 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006276 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006277 }
6278
6279 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6280 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006281 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006282 }
6283
6284 case Intrinsic::arm_neon_vqrshifts:
6285 case Intrinsic::arm_neon_vqrshiftu:
6286 // No immediate versions of these to check for.
6287 break;
6288 }
6289
6290 return SDValue();
6291}
6292
6293/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6294/// lowers them. As with the vector shift intrinsics, this is done during DAG
6295/// combining instead of DAG legalizing because the build_vectors for 64-bit
6296/// vector element shift counts are generally not legal, and it is hard to see
6297/// their values after they get legalized to loads from a constant pool.
6298static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6299 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006300 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006301
6302 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006303 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6304 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006305 return SDValue();
6306
6307 assert(ST->hasNEON() && "unexpected vector shift");
6308 int64_t Cnt;
6309
6310 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006311 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006312
6313 case ISD::SHL:
6314 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6315 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006316 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006317 break;
6318
6319 case ISD::SRA:
6320 case ISD::SRL:
6321 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6322 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6323 ARMISD::VSHRs : ARMISD::VSHRu);
6324 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006325 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006326 }
6327 }
6328 return SDValue();
6329}
6330
6331/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6332/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6333static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6334 const ARMSubtarget *ST) {
6335 SDValue N0 = N->getOperand(0);
6336
6337 // Check for sign- and zero-extensions of vector extract operations of 8-
6338 // and 16-bit vector elements. NEON supports these directly. They are
6339 // handled during DAG combining because type legalization will promote them
6340 // to 32-bit types and it is messy to recognize the operations after that.
6341 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6342 SDValue Vec = N0.getOperand(0);
6343 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006344 EVT VT = N->getValueType(0);
6345 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006346 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6347
Owen Anderson825b72b2009-08-11 20:47:22 +00006348 if (VT == MVT::i32 &&
6349 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006350 TLI.isTypeLegal(Vec.getValueType()) &&
6351 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006352
6353 unsigned Opc = 0;
6354 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006355 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006356 case ISD::SIGN_EXTEND:
6357 Opc = ARMISD::VGETLANEs;
6358 break;
6359 case ISD::ZERO_EXTEND:
6360 case ISD::ANY_EXTEND:
6361 Opc = ARMISD::VGETLANEu;
6362 break;
6363 }
6364 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6365 }
6366 }
6367
6368 return SDValue();
6369}
6370
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006371/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6372/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6373static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6374 const ARMSubtarget *ST) {
6375 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006376 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006377 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6378 // a NaN; only do the transformation when it matches that behavior.
6379
6380 // For now only do this when using NEON for FP operations; if using VFP, it
6381 // is not obvious that the benefit outweighs the cost of switching to the
6382 // NEON pipeline.
6383 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6384 N->getValueType(0) != MVT::f32)
6385 return SDValue();
6386
6387 SDValue CondLHS = N->getOperand(0);
6388 SDValue CondRHS = N->getOperand(1);
6389 SDValue LHS = N->getOperand(2);
6390 SDValue RHS = N->getOperand(3);
6391 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6392
6393 unsigned Opcode = 0;
6394 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006395 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006396 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006397 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006398 IsReversed = true ; // x CC y ? y : x
6399 } else {
6400 return SDValue();
6401 }
6402
Bob Wilsone742bb52010-02-24 22:15:53 +00006403 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006404 switch (CC) {
6405 default: break;
6406 case ISD::SETOLT:
6407 case ISD::SETOLE:
6408 case ISD::SETLT:
6409 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006410 case ISD::SETULT:
6411 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006412 // If LHS is NaN, an ordered comparison will be false and the result will
6413 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6414 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6415 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6416 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6417 break;
6418 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6419 // will return -0, so vmin can only be used for unsafe math or if one of
6420 // the operands is known to be nonzero.
6421 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6422 !UnsafeFPMath &&
6423 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6424 break;
6425 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006426 break;
6427
6428 case ISD::SETOGT:
6429 case ISD::SETOGE:
6430 case ISD::SETGT:
6431 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006432 case ISD::SETUGT:
6433 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006434 // If LHS is NaN, an ordered comparison will be false and the result will
6435 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6436 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6437 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6438 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6439 break;
6440 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6441 // will return +0, so vmax can only be used for unsafe math or if one of
6442 // the operands is known to be nonzero.
6443 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6444 !UnsafeFPMath &&
6445 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6446 break;
6447 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006448 break;
6449 }
6450
6451 if (!Opcode)
6452 return SDValue();
6453 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6454}
6455
Dan Gohman475871a2008-07-27 21:46:04 +00006456SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006457 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006458 switch (N->getOpcode()) {
6459 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006460 case ISD::ADD: return PerformADDCombine(N, DCI);
6461 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006462 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006463 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006464 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006465 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006466 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006467 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006468 case ISD::STORE: return PerformSTORECombine(N, DCI);
6469 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6470 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006471 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006472 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006473 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006474 case ISD::SHL:
6475 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006476 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006477 case ISD::SIGN_EXTEND:
6478 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006479 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6480 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006481 case ARMISD::VLD2DUP:
6482 case ARMISD::VLD3DUP:
6483 case ARMISD::VLD4DUP:
6484 return CombineBaseUpdate(N, DCI);
6485 case ISD::INTRINSIC_VOID:
6486 case ISD::INTRINSIC_W_CHAIN:
6487 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6488 case Intrinsic::arm_neon_vld1:
6489 case Intrinsic::arm_neon_vld2:
6490 case Intrinsic::arm_neon_vld3:
6491 case Intrinsic::arm_neon_vld4:
6492 case Intrinsic::arm_neon_vld2lane:
6493 case Intrinsic::arm_neon_vld3lane:
6494 case Intrinsic::arm_neon_vld4lane:
6495 case Intrinsic::arm_neon_vst1:
6496 case Intrinsic::arm_neon_vst2:
6497 case Intrinsic::arm_neon_vst3:
6498 case Intrinsic::arm_neon_vst4:
6499 case Intrinsic::arm_neon_vst2lane:
6500 case Intrinsic::arm_neon_vst3lane:
6501 case Intrinsic::arm_neon_vst4lane:
6502 return CombineBaseUpdate(N, DCI);
6503 default: break;
6504 }
6505 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006506 }
Dan Gohman475871a2008-07-27 21:46:04 +00006507 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006508}
6509
Evan Cheng31959b12011-02-02 01:06:55 +00006510bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6511 EVT VT) const {
6512 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6513}
6514
Bill Wendlingaf566342009-08-15 21:21:19 +00006515bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006516 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006517 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006518
6519 switch (VT.getSimpleVT().SimpleTy) {
6520 default:
6521 return false;
6522 case MVT::i8:
6523 case MVT::i16:
6524 case MVT::i32:
6525 return true;
6526 // FIXME: VLD1 etc with standard alignment is legal.
6527 }
6528}
6529
Evan Chenge6c835f2009-08-14 20:09:37 +00006530static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6531 if (V < 0)
6532 return false;
6533
6534 unsigned Scale = 1;
6535 switch (VT.getSimpleVT().SimpleTy) {
6536 default: return false;
6537 case MVT::i1:
6538 case MVT::i8:
6539 // Scale == 1;
6540 break;
6541 case MVT::i16:
6542 // Scale == 2;
6543 Scale = 2;
6544 break;
6545 case MVT::i32:
6546 // Scale == 4;
6547 Scale = 4;
6548 break;
6549 }
6550
6551 if ((V & (Scale - 1)) != 0)
6552 return false;
6553 V /= Scale;
6554 return V == (V & ((1LL << 5) - 1));
6555}
6556
6557static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6558 const ARMSubtarget *Subtarget) {
6559 bool isNeg = false;
6560 if (V < 0) {
6561 isNeg = true;
6562 V = - V;
6563 }
6564
6565 switch (VT.getSimpleVT().SimpleTy) {
6566 default: return false;
6567 case MVT::i1:
6568 case MVT::i8:
6569 case MVT::i16:
6570 case MVT::i32:
6571 // + imm12 or - imm8
6572 if (isNeg)
6573 return V == (V & ((1LL << 8) - 1));
6574 return V == (V & ((1LL << 12) - 1));
6575 case MVT::f32:
6576 case MVT::f64:
6577 // Same as ARM mode. FIXME: NEON?
6578 if (!Subtarget->hasVFP2())
6579 return false;
6580 if ((V & 3) != 0)
6581 return false;
6582 V >>= 2;
6583 return V == (V & ((1LL << 8) - 1));
6584 }
6585}
6586
Evan Chengb01fad62007-03-12 23:30:29 +00006587/// isLegalAddressImmediate - Return true if the integer value can be used
6588/// as the offset of the target addressing mode for load / store of the
6589/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006590static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006591 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006592 if (V == 0)
6593 return true;
6594
Evan Cheng65011532009-03-09 19:15:00 +00006595 if (!VT.isSimple())
6596 return false;
6597
Evan Chenge6c835f2009-08-14 20:09:37 +00006598 if (Subtarget->isThumb1Only())
6599 return isLegalT1AddressImmediate(V, VT);
6600 else if (Subtarget->isThumb2())
6601 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006602
Evan Chenge6c835f2009-08-14 20:09:37 +00006603 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006604 if (V < 0)
6605 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006606 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006607 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006608 case MVT::i1:
6609 case MVT::i8:
6610 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006611 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006612 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006613 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006614 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006615 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006616 case MVT::f32:
6617 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006618 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006619 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006620 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006621 return false;
6622 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006623 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006624 }
Evan Chenga8e29892007-01-19 07:51:42 +00006625}
6626
Evan Chenge6c835f2009-08-14 20:09:37 +00006627bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6628 EVT VT) const {
6629 int Scale = AM.Scale;
6630 if (Scale < 0)
6631 return false;
6632
6633 switch (VT.getSimpleVT().SimpleTy) {
6634 default: return false;
6635 case MVT::i1:
6636 case MVT::i8:
6637 case MVT::i16:
6638 case MVT::i32:
6639 if (Scale == 1)
6640 return true;
6641 // r + r << imm
6642 Scale = Scale & ~1;
6643 return Scale == 2 || Scale == 4 || Scale == 8;
6644 case MVT::i64:
6645 // r + r
6646 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6647 return true;
6648 return false;
6649 case MVT::isVoid:
6650 // Note, we allow "void" uses (basically, uses that aren't loads or
6651 // stores), because arm allows folding a scale into many arithmetic
6652 // operations. This should be made more precise and revisited later.
6653
6654 // Allow r << imm, but the imm has to be a multiple of two.
6655 if (Scale & 1) return false;
6656 return isPowerOf2_32(Scale);
6657 }
6658}
6659
Chris Lattner37caf8c2007-04-09 23:33:39 +00006660/// isLegalAddressingMode - Return true if the addressing mode represented
6661/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006662bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006663 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006664 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006665 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006666 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006667
Chris Lattner37caf8c2007-04-09 23:33:39 +00006668 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006669 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006670 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006671
Chris Lattner37caf8c2007-04-09 23:33:39 +00006672 switch (AM.Scale) {
6673 case 0: // no scale reg, must be "r+i" or "r", or "i".
6674 break;
6675 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006676 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006677 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006678 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006679 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006680 // ARM doesn't support any R+R*scale+imm addr modes.
6681 if (AM.BaseOffs)
6682 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006683
Bob Wilson2c7dab12009-04-08 17:55:28 +00006684 if (!VT.isSimple())
6685 return false;
6686
Evan Chenge6c835f2009-08-14 20:09:37 +00006687 if (Subtarget->isThumb2())
6688 return isLegalT2ScaledAddressingMode(AM, VT);
6689
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006690 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006691 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006692 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006693 case MVT::i1:
6694 case MVT::i8:
6695 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006696 if (Scale < 0) Scale = -Scale;
6697 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006698 return true;
6699 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006700 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006701 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006702 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006703 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006704 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006705 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006706 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006707
Owen Anderson825b72b2009-08-11 20:47:22 +00006708 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006709 // Note, we allow "void" uses (basically, uses that aren't loads or
6710 // stores), because arm allows folding a scale into many arithmetic
6711 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006712
Chris Lattner37caf8c2007-04-09 23:33:39 +00006713 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006714 if (Scale & 1) return false;
6715 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006716 }
6717 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006718 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006719 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006720}
6721
Evan Cheng77e47512009-11-11 19:05:52 +00006722/// isLegalICmpImmediate - Return true if the specified immediate is legal
6723/// icmp immediate, that is the target has icmp instructions which can compare
6724/// a register against the immediate without having to materialize the
6725/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006726bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006727 if (!Subtarget->isThumb())
6728 return ARM_AM::getSOImmVal(Imm) != -1;
6729 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006730 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006731 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006732}
6733
Owen Andersone50ed302009-08-10 22:56:29 +00006734static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006735 bool isSEXTLoad, SDValue &Base,
6736 SDValue &Offset, bool &isInc,
6737 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006738 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6739 return false;
6740
Owen Anderson825b72b2009-08-11 20:47:22 +00006741 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006742 // AddressingMode 3
6743 Base = Ptr->getOperand(0);
6744 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006745 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006746 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006747 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006748 isInc = false;
6749 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6750 return true;
6751 }
6752 }
6753 isInc = (Ptr->getOpcode() == ISD::ADD);
6754 Offset = Ptr->getOperand(1);
6755 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006756 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006757 // AddressingMode 2
6758 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006759 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006760 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006761 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006762 isInc = false;
6763 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6764 Base = Ptr->getOperand(0);
6765 return true;
6766 }
6767 }
6768
6769 if (Ptr->getOpcode() == ISD::ADD) {
6770 isInc = true;
6771 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6772 if (ShOpcVal != ARM_AM::no_shift) {
6773 Base = Ptr->getOperand(1);
6774 Offset = Ptr->getOperand(0);
6775 } else {
6776 Base = Ptr->getOperand(0);
6777 Offset = Ptr->getOperand(1);
6778 }
6779 return true;
6780 }
6781
6782 isInc = (Ptr->getOpcode() == ISD::ADD);
6783 Base = Ptr->getOperand(0);
6784 Offset = Ptr->getOperand(1);
6785 return true;
6786 }
6787
Jim Grosbache5165492009-11-09 00:11:35 +00006788 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006789 return false;
6790}
6791
Owen Andersone50ed302009-08-10 22:56:29 +00006792static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006793 bool isSEXTLoad, SDValue &Base,
6794 SDValue &Offset, bool &isInc,
6795 SelectionDAG &DAG) {
6796 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6797 return false;
6798
6799 Base = Ptr->getOperand(0);
6800 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6801 int RHSC = (int)RHS->getZExtValue();
6802 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6803 assert(Ptr->getOpcode() == ISD::ADD);
6804 isInc = false;
6805 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6806 return true;
6807 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6808 isInc = Ptr->getOpcode() == ISD::ADD;
6809 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6810 return true;
6811 }
6812 }
6813
6814 return false;
6815}
6816
Evan Chenga8e29892007-01-19 07:51:42 +00006817/// getPreIndexedAddressParts - returns true by value, base pointer and
6818/// offset pointer and addressing mode by reference if the node's address
6819/// can be legally represented as pre-indexed load / store address.
6820bool
Dan Gohman475871a2008-07-27 21:46:04 +00006821ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6822 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006823 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006824 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006825 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006826 return false;
6827
Owen Andersone50ed302009-08-10 22:56:29 +00006828 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006829 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006830 bool isSEXTLoad = false;
6831 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6832 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006833 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006834 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6835 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6836 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006837 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006838 } else
6839 return false;
6840
6841 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006842 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006843 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006844 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6845 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006846 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006847 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006848 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006849 if (!isLegal)
6850 return false;
6851
6852 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6853 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006854}
6855
6856/// getPostIndexedAddressParts - returns true by value, base pointer and
6857/// offset pointer and addressing mode by reference if this node can be
6858/// combined with a load / store to form a post-indexed load / store.
6859bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006860 SDValue &Base,
6861 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006862 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006863 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006864 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006865 return false;
6866
Owen Andersone50ed302009-08-10 22:56:29 +00006867 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006868 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006869 bool isSEXTLoad = false;
6870 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006871 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006872 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006873 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6874 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006875 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006876 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006877 } else
6878 return false;
6879
6880 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006881 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006882 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006883 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006884 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006885 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006886 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6887 isInc, DAG);
6888 if (!isLegal)
6889 return false;
6890
Evan Cheng28dad2a2010-05-18 21:31:17 +00006891 if (Ptr != Base) {
6892 // Swap base ptr and offset to catch more post-index load / store when
6893 // it's legal. In Thumb2 mode, offset must be an immediate.
6894 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6895 !Subtarget->isThumb2())
6896 std::swap(Base, Offset);
6897
6898 // Post-indexed load / store update the base pointer.
6899 if (Ptr != Base)
6900 return false;
6901 }
6902
Evan Chenge88d5ce2009-07-02 07:28:31 +00006903 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6904 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006905}
6906
Dan Gohman475871a2008-07-27 21:46:04 +00006907void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006908 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006909 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006910 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006911 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006912 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006913 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006914 switch (Op.getOpcode()) {
6915 default: break;
6916 case ARMISD::CMOV: {
6917 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006918 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006919 if (KnownZero == 0 && KnownOne == 0) return;
6920
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006921 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006922 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6923 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006924 KnownZero &= KnownZeroRHS;
6925 KnownOne &= KnownOneRHS;
6926 return;
6927 }
6928 }
6929}
6930
6931//===----------------------------------------------------------------------===//
6932// ARM Inline Assembly Support
6933//===----------------------------------------------------------------------===//
6934
Evan Cheng55d42002011-01-08 01:24:27 +00006935bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6936 // Looking for "rev" which is V6+.
6937 if (!Subtarget->hasV6Ops())
6938 return false;
6939
6940 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6941 std::string AsmStr = IA->getAsmString();
6942 SmallVector<StringRef, 4> AsmPieces;
6943 SplitString(AsmStr, AsmPieces, ";\n");
6944
6945 switch (AsmPieces.size()) {
6946 default: return false;
6947 case 1:
6948 AsmStr = AsmPieces[0];
6949 AsmPieces.clear();
6950 SplitString(AsmStr, AsmPieces, " \t,");
6951
6952 // rev $0, $1
6953 if (AsmPieces.size() == 3 &&
6954 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6955 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6956 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6957 if (Ty && Ty->getBitWidth() == 32)
6958 return IntrinsicLowering::LowerToByteSwap(CI);
6959 }
6960 break;
6961 }
6962
6963 return false;
6964}
6965
Evan Chenga8e29892007-01-19 07:51:42 +00006966/// getConstraintType - Given a constraint letter, return the type of
6967/// constraint it is for this target.
6968ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006969ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6970 if (Constraint.size() == 1) {
6971 switch (Constraint[0]) {
6972 default: break;
6973 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006974 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006975 }
Evan Chenga8e29892007-01-19 07:51:42 +00006976 }
Chris Lattner4234f572007-03-25 02:14:49 +00006977 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006978}
6979
John Thompson44ab89e2010-10-29 17:29:13 +00006980/// Examine constraint type and operand type and determine a weight value.
6981/// This object must already have been set up with the operand type
6982/// and the current alternative constraint selected.
6983TargetLowering::ConstraintWeight
6984ARMTargetLowering::getSingleConstraintMatchWeight(
6985 AsmOperandInfo &info, const char *constraint) const {
6986 ConstraintWeight weight = CW_Invalid;
6987 Value *CallOperandVal = info.CallOperandVal;
6988 // If we don't have a value, we can't do a match,
6989 // but allow it at the lowest weight.
6990 if (CallOperandVal == NULL)
6991 return CW_Default;
6992 const Type *type = CallOperandVal->getType();
6993 // Look at the constraint type.
6994 switch (*constraint) {
6995 default:
6996 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6997 break;
6998 case 'l':
6999 if (type->isIntegerTy()) {
7000 if (Subtarget->isThumb())
7001 weight = CW_SpecificReg;
7002 else
7003 weight = CW_Register;
7004 }
7005 break;
7006 case 'w':
7007 if (type->isFloatingPointTy())
7008 weight = CW_Register;
7009 break;
7010 }
7011 return weight;
7012}
7013
Bob Wilson2dc4f542009-03-20 22:42:55 +00007014std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00007015ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007016 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007017 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007018 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007019 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007020 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007021 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007022 return std::make_pair(0U, ARM::tGPRRegisterClass);
7023 else
7024 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007025 case 'r':
7026 return std::make_pair(0U, ARM::GPRRegisterClass);
7027 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007028 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007029 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007030 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007031 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007032 if (VT.getSizeInBits() == 128)
7033 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007034 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007035 }
7036 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007037 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007038 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007039
Evan Chenga8e29892007-01-19 07:51:42 +00007040 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7041}
7042
7043std::vector<unsigned> ARMTargetLowering::
7044getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007045 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007046 if (Constraint.size() != 1)
7047 return std::vector<unsigned>();
7048
7049 switch (Constraint[0]) { // GCC ARM Constraint Letters
7050 default: break;
7051 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007052 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7053 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7054 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007055 case 'r':
7056 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
7057 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
7058 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
7059 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007060 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007061 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007062 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
7063 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
7064 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
7065 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
7066 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
7067 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
7068 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
7069 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00007070 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007071 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
7072 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
7073 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
7074 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00007075 if (VT.getSizeInBits() == 128)
7076 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
7077 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007078 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007079 }
7080
7081 return std::vector<unsigned>();
7082}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007083
7084/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7085/// vector. If it is invalid, don't add anything to Ops.
7086void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
7087 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007088 std::vector<SDValue>&Ops,
7089 SelectionDAG &DAG) const {
7090 SDValue Result(0, 0);
7091
7092 switch (Constraint) {
7093 default: break;
7094 case 'I': case 'J': case 'K': case 'L':
7095 case 'M': case 'N': case 'O':
7096 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7097 if (!C)
7098 return;
7099
7100 int64_t CVal64 = C->getSExtValue();
7101 int CVal = (int) CVal64;
7102 // None of these constraints allow values larger than 32 bits. Check
7103 // that the value fits in an int.
7104 if (CVal != CVal64)
7105 return;
7106
7107 switch (Constraint) {
7108 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007109 if (Subtarget->isThumb1Only()) {
7110 // This must be a constant between 0 and 255, for ADD
7111 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007112 if (CVal >= 0 && CVal <= 255)
7113 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007114 } else if (Subtarget->isThumb2()) {
7115 // A constant that can be used as an immediate value in a
7116 // data-processing instruction.
7117 if (ARM_AM::getT2SOImmVal(CVal) != -1)
7118 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007119 } else {
7120 // A constant that can be used as an immediate value in a
7121 // data-processing instruction.
7122 if (ARM_AM::getSOImmVal(CVal) != -1)
7123 break;
7124 }
7125 return;
7126
7127 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007128 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007129 // This must be a constant between -255 and -1, for negated ADD
7130 // immediates. This can be used in GCC with an "n" modifier that
7131 // prints the negated value, for use with SUB instructions. It is
7132 // not useful otherwise but is implemented for compatibility.
7133 if (CVal >= -255 && CVal <= -1)
7134 break;
7135 } else {
7136 // This must be a constant between -4095 and 4095. It is not clear
7137 // what this constraint is intended for. Implemented for
7138 // compatibility with GCC.
7139 if (CVal >= -4095 && CVal <= 4095)
7140 break;
7141 }
7142 return;
7143
7144 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007145 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007146 // A 32-bit value where only one byte has a nonzero value. Exclude
7147 // zero to match GCC. This constraint is used by GCC internally for
7148 // constants that can be loaded with a move/shift combination.
7149 // It is not useful otherwise but is implemented for compatibility.
7150 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
7151 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007152 } else if (Subtarget->isThumb2()) {
7153 // A constant whose bitwise inverse can be used as an immediate
7154 // value in a data-processing instruction. This can be used in GCC
7155 // with a "B" modifier that prints the inverted value, for use with
7156 // BIC and MVN instructions. It is not useful otherwise but is
7157 // implemented for compatibility.
7158 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
7159 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007160 } else {
7161 // A constant whose bitwise inverse can be used as an immediate
7162 // value in a data-processing instruction. This can be used in GCC
7163 // with a "B" modifier that prints the inverted value, for use with
7164 // BIC and MVN instructions. It is not useful otherwise but is
7165 // implemented for compatibility.
7166 if (ARM_AM::getSOImmVal(~CVal) != -1)
7167 break;
7168 }
7169 return;
7170
7171 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007172 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007173 // This must be a constant between -7 and 7,
7174 // for 3-operand ADD/SUB immediate instructions.
7175 if (CVal >= -7 && CVal < 7)
7176 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00007177 } else if (Subtarget->isThumb2()) {
7178 // A constant whose negation can be used as an immediate value in a
7179 // data-processing instruction. This can be used in GCC with an "n"
7180 // modifier that prints the negated value, for use with SUB
7181 // instructions. It is not useful otherwise but is implemented for
7182 // compatibility.
7183 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7184 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007185 } else {
7186 // A constant whose negation can be used as an immediate value in a
7187 // data-processing instruction. This can be used in GCC with an "n"
7188 // modifier that prints the negated value, for use with SUB
7189 // instructions. It is not useful otherwise but is implemented for
7190 // compatibility.
7191 if (ARM_AM::getSOImmVal(-CVal) != -1)
7192 break;
7193 }
7194 return;
7195
7196 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007197 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007198 // This must be a multiple of 4 between 0 and 1020, for
7199 // ADD sp + immediate.
7200 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7201 break;
7202 } else {
7203 // A power of two or a constant between 0 and 32. This is used in
7204 // GCC for the shift amount on shifted register operands, but it is
7205 // useful in general for any shift amounts.
7206 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7207 break;
7208 }
7209 return;
7210
7211 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007212 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007213 // This must be a constant between 0 and 31, for shift amounts.
7214 if (CVal >= 0 && CVal <= 31)
7215 break;
7216 }
7217 return;
7218
7219 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007220 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007221 // This must be a multiple of 4 between -508 and 508, for
7222 // ADD/SUB sp = sp + immediate.
7223 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7224 break;
7225 }
7226 return;
7227 }
7228 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7229 break;
7230 }
7231
7232 if (Result.getNode()) {
7233 Ops.push_back(Result);
7234 return;
7235 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007236 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007237}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007238
7239bool
7240ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7241 // The ARM target isn't yet aware of offsets.
7242 return false;
7243}
Evan Cheng39382422009-10-28 01:44:26 +00007244
7245int ARM::getVFPf32Imm(const APFloat &FPImm) {
7246 APInt Imm = FPImm.bitcastToAPInt();
7247 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7248 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7249 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7250
7251 // We can handle 4 bits of mantissa.
7252 // mantissa = (16+UInt(e:f:g:h))/16.
7253 if (Mantissa & 0x7ffff)
7254 return -1;
7255 Mantissa >>= 19;
7256 if ((Mantissa & 0xf) != Mantissa)
7257 return -1;
7258
7259 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7260 if (Exp < -3 || Exp > 4)
7261 return -1;
7262 Exp = ((Exp+3) & 0x7) ^ 4;
7263
7264 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7265}
7266
7267int ARM::getVFPf64Imm(const APFloat &FPImm) {
7268 APInt Imm = FPImm.bitcastToAPInt();
7269 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7270 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7271 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7272
7273 // We can handle 4 bits of mantissa.
7274 // mantissa = (16+UInt(e:f:g:h))/16.
7275 if (Mantissa & 0xffffffffffffLL)
7276 return -1;
7277 Mantissa >>= 48;
7278 if ((Mantissa & 0xf) != Mantissa)
7279 return -1;
7280
7281 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7282 if (Exp < -3 || Exp > 4)
7283 return -1;
7284 Exp = ((Exp+3) & 0x7) ^ 4;
7285
7286 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7287}
7288
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007289bool ARM::isBitFieldInvertedMask(unsigned v) {
7290 if (v == 0xffffffff)
7291 return 0;
7292 // there can be 1's on either or both "outsides", all the "inside"
7293 // bits must be 0's
7294 unsigned int lsb = 0, msb = 31;
7295 while (v & (1 << msb)) --msb;
7296 while (v & (1 << lsb)) ++lsb;
7297 for (unsigned int i = lsb; i <= msb; ++i) {
7298 if (v & (1 << i))
7299 return 0;
7300 }
7301 return 1;
7302}
7303
Evan Cheng39382422009-10-28 01:44:26 +00007304/// isFPImmLegal - Returns true if the target can instruction select the
7305/// specified FP immediate natively. If false, the legalizer will
7306/// materialize the FP immediate as a load from a constant pool.
7307bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7308 if (!Subtarget->hasVFP3())
7309 return false;
7310 if (VT == MVT::f32)
7311 return ARM::getVFPf32Imm(Imm) != -1;
7312 if (VT == MVT::f64)
7313 return ARM::getVFPf64Imm(Imm) != -1;
7314 return false;
7315}
Bob Wilson65ffec42010-09-21 17:56:22 +00007316
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007317/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007318/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7319/// specified in the intrinsic calls.
7320bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7321 const CallInst &I,
7322 unsigned Intrinsic) const {
7323 switch (Intrinsic) {
7324 case Intrinsic::arm_neon_vld1:
7325 case Intrinsic::arm_neon_vld2:
7326 case Intrinsic::arm_neon_vld3:
7327 case Intrinsic::arm_neon_vld4:
7328 case Intrinsic::arm_neon_vld2lane:
7329 case Intrinsic::arm_neon_vld3lane:
7330 case Intrinsic::arm_neon_vld4lane: {
7331 Info.opc = ISD::INTRINSIC_W_CHAIN;
7332 // Conservatively set memVT to the entire set of vectors loaded.
7333 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7334 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7335 Info.ptrVal = I.getArgOperand(0);
7336 Info.offset = 0;
7337 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7338 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7339 Info.vol = false; // volatile loads with NEON intrinsics not supported
7340 Info.readMem = true;
7341 Info.writeMem = false;
7342 return true;
7343 }
7344 case Intrinsic::arm_neon_vst1:
7345 case Intrinsic::arm_neon_vst2:
7346 case Intrinsic::arm_neon_vst3:
7347 case Intrinsic::arm_neon_vst4:
7348 case Intrinsic::arm_neon_vst2lane:
7349 case Intrinsic::arm_neon_vst3lane:
7350 case Intrinsic::arm_neon_vst4lane: {
7351 Info.opc = ISD::INTRINSIC_VOID;
7352 // Conservatively set memVT to the entire set of vectors stored.
7353 unsigned NumElts = 0;
7354 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7355 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7356 if (!ArgTy->isVectorTy())
7357 break;
7358 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7359 }
7360 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7361 Info.ptrVal = I.getArgOperand(0);
7362 Info.offset = 0;
7363 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7364 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7365 Info.vol = false; // volatile stores with NEON intrinsics not supported
7366 Info.readMem = false;
7367 Info.writeMem = true;
7368 return true;
7369 }
7370 default:
7371 break;
7372 }
7373
7374 return false;
7375}