blob: 5ecd9b9920eb4b9dc693e14b12cc31ec798e389e [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
17#include "ARMAddressingModes.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
20#include "ARMISelLowering.h"
21#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000022#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000023#include "ARMRegisterInfo.h"
24#include "ARMSubtarget.h"
25#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000026#include "ARMTargetObjectFile.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Owen Andersone50ed302009-08-10 22:56:29 +000075void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
76 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000077 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000078 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000079 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
80 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000081
Owen Anderson70671842009-08-10 20:18:46 +000082 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +000083 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +000084 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000085 }
86
Owen Andersone50ed302009-08-10 22:56:29 +000087 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +000088 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Owen Anderson70671842009-08-10 20:18:46 +000089 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +000090 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +000091 if (ElemTy != MVT::i32) {
92 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
94 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
95 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
96 }
Owen Anderson70671842009-08-10 20:18:46 +000097 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
98 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +000099 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000100 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000101 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
102 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000103 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000104 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
105 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000107 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
108 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000109 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
110 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
111 setTruncStoreAction(VT.getSimpleVT(),
112 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000113 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000114 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000115
116 // Promote all bit-wise operations.
117 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000118 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000119 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
120 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000121 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000122 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000123 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000125 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000126 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000127 }
Bob Wilson16330762009-09-16 00:17:28 +0000128
129 // Neon does not support vector divide/remainder operations.
130 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
131 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
132 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
133 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
134 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
135 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000136}
137
Owen Andersone50ed302009-08-10 22:56:29 +0000138void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000139 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000141}
142
Owen Andersone50ed302009-08-10 22:56:29 +0000143void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000144 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000146}
147
Chris Lattnerf0144122009-07-28 03:13:23 +0000148static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
149 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000150 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000151
Chris Lattner80ec2792009-08-02 00:34:36 +0000152 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000153}
154
Evan Chenga8e29892007-01-19 07:51:42 +0000155ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000156 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000157 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000158 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000159 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000160
Evan Chengb1df8f22007-04-27 08:15:43 +0000161 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000162 // Uses VFP for Thumb libfuncs if available.
163 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
164 // Single-precision floating-point arithmetic.
165 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
166 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
167 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
168 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000169
Evan Chengb1df8f22007-04-27 08:15:43 +0000170 // Double-precision floating-point arithmetic.
171 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
172 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
173 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
174 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000175
Evan Chengb1df8f22007-04-27 08:15:43 +0000176 // Single-precision comparisons.
177 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
178 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
179 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
180 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
181 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
182 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
183 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
184 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000185
Evan Chengb1df8f22007-04-27 08:15:43 +0000186 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000194
Evan Chengb1df8f22007-04-27 08:15:43 +0000195 // Double-precision comparisons.
196 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
197 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
198 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
199 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
200 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
201 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
202 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
203 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000204
Evan Chengb1df8f22007-04-27 08:15:43 +0000205 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Floating-point to integer conversions.
215 // i64 conversions are done via library routines even when generating VFP
216 // instructions, so use the same ones.
217 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
218 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
219 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
220 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000221
Evan Chengb1df8f22007-04-27 08:15:43 +0000222 // Conversions between floating types.
223 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
224 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
225
226 // Integer to floating-point conversions.
227 // i64 conversions are done via library routines even when generating VFP
228 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000229 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
230 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000231 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
232 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
233 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
234 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
235 }
Evan Chenga8e29892007-01-19 07:51:42 +0000236 }
237
Bob Wilson2f954612009-05-22 17:38:41 +0000238 // These libcalls are not available in 32-bit.
239 setLibcallName(RTLIB::SHL_I128, 0);
240 setLibcallName(RTLIB::SRL_I128, 0);
241 setLibcallName(RTLIB::SRA_I128, 0);
242
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000243 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000245 // RTABI chapter 4.1.2, Table 2
246 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
247 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
248 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
249 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
250 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
251 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
252 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
253 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
254
255 // Double-precision floating-point comparison helper functions
256 // RTABI chapter 4.1.2, Table 3
257 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
258 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
259 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
260 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
261 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
262 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
263 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
264 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
265 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
266 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
267 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
268 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
269 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
270 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
271 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
272 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
273 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
276 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
277 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
278 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
279 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
280 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
281
282 // Single-precision floating-point arithmetic helper functions
283 // RTABI chapter 4.1.2, Table 4
284 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
285 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
286 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
287 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
288 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
292
293 // Single-precision floating-point comparison helper functions
294 // RTABI chapter 4.1.2, Table 5
295 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
296 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
297 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
298 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
299 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
300 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
301 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
302 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
303 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
304 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
305 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
306 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
307 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
308 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
309 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
310 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
311 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
314 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
315 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
316 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
317 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
318 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
319
320 // Floating-point to integer conversions.
321 // RTABI chapter 4.1.2, Table 6
322 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
323 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
324 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
325 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
326 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
327 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
328 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
329 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
330 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
338
339 // Conversions between floating types.
340 // RTABI chapter 4.1.2, Table 7
341 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
342 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
343 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000344 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000345
346 // Integer to floating-point conversions.
347 // RTABI chapter 4.1.2, Table 8
348 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
349 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
350 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
351 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
352 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
353 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
354 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
355 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
356 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
360 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
361 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
362 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
363 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
364
365 // Long long helper functions
366 // RTABI chapter 4.2, Table 9
367 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
368 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
369 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
370 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
371 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
372 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
373 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
379
380 // Integer division functions
381 // RTABI chapter 4.3.1
382 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
383 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
384 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
385 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
386 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
387 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
388 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000393 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000394 }
395
David Goodwinf1daf7d2009-07-08 23:10:31 +0000396 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000398 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000400 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000402 if (!Subtarget->isFPOnlySP())
403 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000404
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000406 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000407
408 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 addDRTypeForNEON(MVT::v2f32);
410 addDRTypeForNEON(MVT::v8i8);
411 addDRTypeForNEON(MVT::v4i16);
412 addDRTypeForNEON(MVT::v2i32);
413 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000414
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 addQRTypeForNEON(MVT::v4f32);
416 addQRTypeForNEON(MVT::v2f64);
417 addQRTypeForNEON(MVT::v16i8);
418 addQRTypeForNEON(MVT::v8i16);
419 addQRTypeForNEON(MVT::v4i32);
420 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000421
Bob Wilson74dc72e2009-09-15 23:55:57 +0000422 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
423 // neither Neon nor VFP support any arithmetic operations on it.
424 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
425 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
426 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
427 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
428 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
429 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
430 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
431 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
432 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
433 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
435 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
436 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
437 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
438 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
439 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
440 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
441 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
442 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
443 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
444 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
445 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
446 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
447 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
448
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000449 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
450
Bob Wilson642b3292009-09-16 00:32:15 +0000451 // Neon does not support some operations on v1i64 and v2i64 types.
452 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000453 // Custom handling for some quad-vector types to detect VMULL.
454 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
455 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
456 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000457 // Custom handling for some vector types to avoid expensive expansions
458 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
459 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
460 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
461 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000462 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
463 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000464 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
465 // a destination type that is wider than the source.
466 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
467 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000468
Bob Wilson1c3ef902011-02-07 17:43:21 +0000469 setTargetDAGCombine(ISD::INTRINSIC_VOID);
470 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000471 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
472 setTargetDAGCombine(ISD::SHL);
473 setTargetDAGCombine(ISD::SRL);
474 setTargetDAGCombine(ISD::SRA);
475 setTargetDAGCombine(ISD::SIGN_EXTEND);
476 setTargetDAGCombine(ISD::ZERO_EXTEND);
477 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000478 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000479 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000480 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000481 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
482 setTargetDAGCombine(ISD::STORE);
Bob Wilson5bafff32009-06-22 23:27:02 +0000483 }
484
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000485 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000486
487 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000489
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000490 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000492
Evan Chenga8e29892007-01-19 07:51:42 +0000493 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000494 if (!Subtarget->isThumb1Only()) {
495 for (unsigned im = (unsigned)ISD::PRE_INC;
496 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000497 setIndexedLoadAction(im, MVT::i1, Legal);
498 setIndexedLoadAction(im, MVT::i8, Legal);
499 setIndexedLoadAction(im, MVT::i16, Legal);
500 setIndexedLoadAction(im, MVT::i32, Legal);
501 setIndexedStoreAction(im, MVT::i1, Legal);
502 setIndexedStoreAction(im, MVT::i8, Legal);
503 setIndexedStoreAction(im, MVT::i16, Legal);
504 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000505 }
Evan Chenga8e29892007-01-19 07:51:42 +0000506 }
507
508 // i64 operation support.
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000509 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::MUL, MVT::i64, Expand);
511 setOperationAction(ISD::MULHU, MVT::i32, Expand);
512 setOperationAction(ISD::MULHS, MVT::i32, Expand);
513 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
514 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000515 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 setOperationAction(ISD::MUL, MVT::i64, Expand);
517 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb6207242009-08-01 00:16:10 +0000518 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setOperationAction(ISD::MULHS, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000520 }
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000521 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000522 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000523 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::SRL, MVT::i64, Custom);
525 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000526
527 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000529 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000531 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000532 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000533
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000534 // Only ARMv6 has BSWAP.
535 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000537
Evan Chenga8e29892007-01-19 07:51:42 +0000538 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000539 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000540 // v7M has a hardware divider
541 setOperationAction(ISD::SDIV, MVT::i32, Expand);
542 setOperationAction(ISD::UDIV, MVT::i32, Expand);
543 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::SREM, MVT::i32, Expand);
545 setOperationAction(ISD::UREM, MVT::i32, Expand);
546 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
547 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000548
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
550 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
551 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
552 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000553 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000554
Evan Chengfb3611d2010-05-11 07:26:32 +0000555 setOperationAction(ISD::TRAP, MVT::Other, Legal);
556
Evan Chenga8e29892007-01-19 07:51:42 +0000557 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 setOperationAction(ISD::VASTART, MVT::Other, Custom);
559 setOperationAction(ISD::VAARG, MVT::Other, Expand);
560 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
561 setOperationAction(ISD::VAEND, MVT::Other, Expand);
562 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
563 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000564 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000565 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
566 setExceptionPointerRegister(ARM::R0);
567 setExceptionSelectorRegister(ARM::R1);
568
Evan Cheng3a1588a2010-04-15 22:20:34 +0000569 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000570 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
571 // the default expansion.
572 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000573 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000574 // membarrier needs custom lowering; the rest are legal and handled
575 // normally.
576 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
577 } else {
578 // Set them all for expansion, which will force libcalls.
579 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
580 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Expand);
581 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Expand);
582 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000583 setOperationAction(ISD::ATOMIC_SWAP, MVT::i8, Expand);
584 setOperationAction(ISD::ATOMIC_SWAP, MVT::i16, Expand);
585 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000586 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i8, Expand);
587 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i16, Expand);
588 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
589 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Expand);
590 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Expand);
591 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
592 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i8, Expand);
593 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i16, Expand);
594 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
595 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i8, Expand);
596 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i16, Expand);
597 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
598 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i8, Expand);
599 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i16, Expand);
600 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
601 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i8, Expand);
602 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i16, Expand);
603 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000604 // Since the libcalls include locking, fold in the fences
605 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000606 }
607 // 64-bit versions are always libcalls (for now)
608 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000609 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000610 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Expand);
611 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Expand);
612 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Expand);
613 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Expand);
614 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Expand);
615 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000616
Evan Cheng416941d2010-11-04 05:19:35 +0000617 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000618
Eli Friedmana2c6f452010-06-26 04:36:50 +0000619 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
620 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
622 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000623 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000625
Nate Begemand1fb5832010-08-03 21:31:55 +0000626 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000627 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
628 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000629 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000630 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
631 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000632
633 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000635 if (Subtarget->isTargetDarwin()) {
636 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
637 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000638 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000639 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000640
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 setOperationAction(ISD::SETCC, MVT::i32, Expand);
642 setOperationAction(ISD::SETCC, MVT::f32, Expand);
643 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000644 setOperationAction(ISD::SELECT, MVT::i32, Custom);
645 setOperationAction(ISD::SELECT, MVT::f32, Custom);
646 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
648 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
649 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
652 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
653 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
654 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
655 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000656
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000657 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::FSIN, MVT::f64, Expand);
659 setOperationAction(ISD::FSIN, MVT::f32, Expand);
660 setOperationAction(ISD::FCOS, MVT::f32, Expand);
661 setOperationAction(ISD::FCOS, MVT::f64, Expand);
662 setOperationAction(ISD::FREM, MVT::f64, Expand);
663 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000664 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
666 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000667 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::FPOW, MVT::f64, Expand);
669 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000670
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000671 // Various VFP goodness
672 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000673 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
674 if (Subtarget->hasVFP2()) {
675 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
676 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
677 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
678 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
679 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000680 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000681 if (!Subtarget->hasFP16()) {
682 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
683 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000684 }
Evan Cheng110cf482008-04-01 01:50:16 +0000685 }
Evan Chenga8e29892007-01-19 07:51:42 +0000686
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000687 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000688 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000689 setTargetDAGCombine(ISD::ADD);
690 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000691 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000692
Owen Anderson080c0922010-11-05 19:27:46 +0000693 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000694 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000695 if (Subtarget->hasNEON())
696 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000697
Evan Chenga8e29892007-01-19 07:51:42 +0000698 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000699
Evan Chengf7d87ee2010-05-21 00:43:17 +0000700 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
701 setSchedulingPreference(Sched::RegPressure);
702 else
703 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000704
Evan Cheng05219282011-01-06 06:52:41 +0000705 //// temporary - rewrite interface to use type
706 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000707
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000708 // On ARM arguments smaller than 4 bytes are extended, so all arguments
709 // are at least 4 bytes aligned.
710 setMinStackArgumentAlignment(4);
711
Evan Chengfff606d2010-09-24 19:07:23 +0000712 benefitFromCodePlacementOpt = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000713}
714
Andrew Trick32cec0a2011-01-19 02:35:27 +0000715// FIXME: It might make sense to define the representative register class as the
716// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
717// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
718// SPR's representative would be DPR_VFP2. This should work well if register
719// pressure tracking were modified such that a register use would increment the
720// pressure of the register class's representative and all of it's super
721// classes' representatives transitively. We have not implemented this because
722// of the difficulty prior to coalescing of modeling operand register classes
723// due to the common occurence of cross class copies and subregister insertions
724// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000725std::pair<const TargetRegisterClass*, uint8_t>
726ARMTargetLowering::findRepresentativeClass(EVT VT) const{
727 const TargetRegisterClass *RRC = 0;
728 uint8_t Cost = 1;
729 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000730 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000731 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000732 // Use DPR as representative register class for all floating point
733 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
734 // the cost is 1 for both f32 and f64.
735 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000736 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000737 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000738 // When NEON is used for SP, only half of the register file is available
739 // because operations that define both SP and DP results will be constrained
740 // to the VFP2 class (D0-D15). We currently model this constraint prior to
741 // coalescing by double-counting the SP regs. See the FIXME above.
742 if (Subtarget->useNEONForSinglePrecisionFP())
743 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000744 break;
745 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
746 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000747 RRC = ARM::DPRRegisterClass;
748 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000749 break;
750 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000751 RRC = ARM::DPRRegisterClass;
752 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000753 break;
754 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000755 RRC = ARM::DPRRegisterClass;
756 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000757 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000758 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000759 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000760}
761
Evan Chenga8e29892007-01-19 07:51:42 +0000762const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
763 switch (Opcode) {
764 default: return 0;
765 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000766 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000767 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000768 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
769 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000770 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000771 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
772 case ARMISD::tCALL: return "ARMISD::tCALL";
773 case ARMISD::BRCOND: return "ARMISD::BRCOND";
774 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000775 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000776 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
777 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
778 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000779 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000780 case ARMISD::CMPFP: return "ARMISD::CMPFP";
781 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000782 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000783 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
784 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000785
Jim Grosbach3482c802010-01-18 19:58:49 +0000786 case ARMISD::RBIT: return "ARMISD::RBIT";
787
Bob Wilson76a312b2010-03-19 22:51:32 +0000788 case ARMISD::FTOSI: return "ARMISD::FTOSI";
789 case ARMISD::FTOUI: return "ARMISD::FTOUI";
790 case ARMISD::SITOF: return "ARMISD::SITOF";
791 case ARMISD::UITOF: return "ARMISD::UITOF";
792
Evan Chenga8e29892007-01-19 07:51:42 +0000793 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
794 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
795 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000796
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000797 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
798 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000799
Evan Chengc5942082009-10-28 06:55:03 +0000800 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
801 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000802 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000803
Dale Johannesen51e28e62010-06-03 21:09:53 +0000804 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000805
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000806 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000807
Evan Cheng86198642009-08-07 00:34:42 +0000808 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
809
Jim Grosbach3728e962009-12-10 00:11:09 +0000810 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000811 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000812
Evan Chengdfed19f2010-11-03 06:34:55 +0000813 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
814
Bob Wilson5bafff32009-06-22 23:27:02 +0000815 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000816 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000817 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000818 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
819 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000820 case ARMISD::VCGEU: return "ARMISD::VCGEU";
821 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000822 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
823 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000824 case ARMISD::VCGTU: return "ARMISD::VCGTU";
825 case ARMISD::VTST: return "ARMISD::VTST";
826
827 case ARMISD::VSHL: return "ARMISD::VSHL";
828 case ARMISD::VSHRs: return "ARMISD::VSHRs";
829 case ARMISD::VSHRu: return "ARMISD::VSHRu";
830 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
831 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
832 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
833 case ARMISD::VSHRN: return "ARMISD::VSHRN";
834 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
835 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
836 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
837 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
838 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
839 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
840 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
841 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
842 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
843 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
844 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
845 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
846 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
847 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000848 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000849 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000850 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000851 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000852 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000853 case ARMISD::VREV64: return "ARMISD::VREV64";
854 case ARMISD::VREV32: return "ARMISD::VREV32";
855 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000856 case ARMISD::VZIP: return "ARMISD::VZIP";
857 case ARMISD::VUZP: return "ARMISD::VUZP";
858 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000859 case ARMISD::VTBL1: return "ARMISD::VTBL1";
860 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000861 case ARMISD::VMULLs: return "ARMISD::VMULLs";
862 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000863 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000864 case ARMISD::FMAX: return "ARMISD::FMAX";
865 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000866 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000867 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
868 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000869 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
870 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
871 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000872 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
873 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
874 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
875 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
876 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
877 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
878 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
879 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
880 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
881 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
882 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
883 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
884 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
885 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
886 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
887 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
888 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000889 }
890}
891
Evan Cheng06b666c2010-05-15 02:18:07 +0000892/// getRegClassFor - Return the register class that should be used for the
893/// specified value type.
894TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
895 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
896 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
897 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000898 if (Subtarget->hasNEON()) {
899 if (VT == MVT::v4i64)
900 return ARM::QQPRRegisterClass;
901 else if (VT == MVT::v8i64)
902 return ARM::QQQQPRRegisterClass;
903 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000904 return TargetLowering::getRegClassFor(VT);
905}
906
Eric Christopherab695882010-07-21 22:26:11 +0000907// Create a fast isel object.
908FastISel *
909ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
910 return ARM::createFastISel(funcInfo);
911}
912
Bill Wendlingb4202b82009-07-01 18:50:55 +0000913/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000914unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
Bob Wilsonb5b50572010-07-01 22:26:26 +0000915 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
Bill Wendling20c568f2009-06-30 22:38:32 +0000916}
917
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000918/// getMaximalGlobalOffset - Returns the maximal possible offset which can
919/// be used for loads / stores from the global.
920unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
921 return (Subtarget->isThumb1Only() ? 127 : 4095);
922}
923
Evan Cheng1cc39842010-05-20 23:26:43 +0000924Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000925 unsigned NumVals = N->getNumValues();
926 if (!NumVals)
927 return Sched::RegPressure;
928
929 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000930 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000931 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000932 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000933 if (VT.isFloatingPoint() || VT.isVector())
934 return Sched::Latency;
935 }
Evan Chengc10f5432010-05-28 23:25:23 +0000936
937 if (!N->isMachineOpcode())
938 return Sched::RegPressure;
939
940 // Load are scheduled for latency even if there instruction itinerary
941 // is not available.
942 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
943 const TargetInstrDesc &TID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000944
945 if (TID.getNumDefs() == 0)
946 return Sched::RegPressure;
947 if (!Itins->isEmpty() &&
948 Itins->getOperandCycle(TID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000949 return Sched::Latency;
950
Evan Cheng1cc39842010-05-20 23:26:43 +0000951 return Sched::RegPressure;
952}
953
Evan Chenga8e29892007-01-19 07:51:42 +0000954//===----------------------------------------------------------------------===//
955// Lowering Code
956//===----------------------------------------------------------------------===//
957
Evan Chenga8e29892007-01-19 07:51:42 +0000958/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
959static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
960 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000961 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +0000962 case ISD::SETNE: return ARMCC::NE;
963 case ISD::SETEQ: return ARMCC::EQ;
964 case ISD::SETGT: return ARMCC::GT;
965 case ISD::SETGE: return ARMCC::GE;
966 case ISD::SETLT: return ARMCC::LT;
967 case ISD::SETLE: return ARMCC::LE;
968 case ISD::SETUGT: return ARMCC::HI;
969 case ISD::SETUGE: return ARMCC::HS;
970 case ISD::SETULT: return ARMCC::LO;
971 case ISD::SETULE: return ARMCC::LS;
972 }
973}
974
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000975/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
976static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +0000977 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +0000978 CondCode2 = ARMCC::AL;
979 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000980 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +0000981 case ISD::SETEQ:
982 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
983 case ISD::SETGT:
984 case ISD::SETOGT: CondCode = ARMCC::GT; break;
985 case ISD::SETGE:
986 case ISD::SETOGE: CondCode = ARMCC::GE; break;
987 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +0000988 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +0000989 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
990 case ISD::SETO: CondCode = ARMCC::VC; break;
991 case ISD::SETUO: CondCode = ARMCC::VS; break;
992 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
993 case ISD::SETUGT: CondCode = ARMCC::HI; break;
994 case ISD::SETUGE: CondCode = ARMCC::PL; break;
995 case ISD::SETLT:
996 case ISD::SETULT: CondCode = ARMCC::LT; break;
997 case ISD::SETLE:
998 case ISD::SETULE: CondCode = ARMCC::LE; break;
999 case ISD::SETNE:
1000 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1001 }
Evan Chenga8e29892007-01-19 07:51:42 +00001002}
1003
Bob Wilson1f595bb2009-04-17 19:07:39 +00001004//===----------------------------------------------------------------------===//
1005// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001006//===----------------------------------------------------------------------===//
1007
1008#include "ARMGenCallingConv.inc"
1009
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001010/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1011/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001012CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001013 bool Return,
1014 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001015 switch (CC) {
1016 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001017 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001018 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001019 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001020 if (!Subtarget->isAAPCS_ABI())
1021 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1022 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1023 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1024 }
1025 // Fallthrough
1026 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001027 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001028 if (!Subtarget->isAAPCS_ABI())
1029 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1030 else if (Subtarget->hasVFP2() &&
1031 FloatABIType == FloatABI::Hard && !isVarArg)
1032 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1033 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1034 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001035 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001036 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001037 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001038 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001039 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001040 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001041 }
1042}
1043
Dan Gohman98ca4f22009-08-05 01:29:28 +00001044/// LowerCallResult - Lower the result values of a call into the
1045/// appropriate copies out of appropriate physical registers.
1046SDValue
1047ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001048 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001049 const SmallVectorImpl<ISD::InputArg> &Ins,
1050 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001051 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001052
Bob Wilson1f595bb2009-04-17 19:07:39 +00001053 // Assign locations to each value returned by this call.
1054 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001055 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001056 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001057 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001058 CCAssignFnForNode(CallConv, /* Return*/ true,
1059 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001060
1061 // Copy all of the result registers out of their specified physreg.
1062 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1063 CCValAssign VA = RVLocs[i];
1064
Bob Wilson80915242009-04-25 00:33:20 +00001065 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001066 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001067 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001068 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001069 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001070 Chain = Lo.getValue(1);
1071 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001073 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001074 InFlag);
1075 Chain = Hi.getValue(1);
1076 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001077 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001078
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 if (VA.getLocVT() == MVT::v2f64) {
1080 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1081 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1082 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001083
1084 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001086 Chain = Lo.getValue(1);
1087 InFlag = Lo.getValue(2);
1088 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001090 Chain = Hi.getValue(1);
1091 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001092 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001093 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1094 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001095 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001096 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001097 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1098 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001099 Chain = Val.getValue(1);
1100 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001101 }
Bob Wilson80915242009-04-25 00:33:20 +00001102
1103 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001104 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001105 case CCValAssign::Full: break;
1106 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001107 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001108 break;
1109 }
1110
Dan Gohman98ca4f22009-08-05 01:29:28 +00001111 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001112 }
1113
Dan Gohman98ca4f22009-08-05 01:29:28 +00001114 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001115}
1116
1117/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1118/// by "Src" to address "Dst" of size "Size". Alignment information is
Bob Wilsondee46d72009-04-17 20:35:10 +00001119/// specified by the specific parameter attribute. The copy will be passed as
Bob Wilson1f595bb2009-04-17 19:07:39 +00001120/// a byval function parameter.
1121/// Sometimes what we are copying is the end of a larger object, the part that
1122/// does not fit in registers.
1123static SDValue
1124CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
1125 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1126 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001128 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001129 /*isVolatile=*/false, /*AlwaysInline=*/false,
Chris Lattnere72f2022010-09-21 05:40:29 +00001130 MachinePointerInfo(0), MachinePointerInfo(0));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131}
1132
Bob Wilsondee46d72009-04-17 20:35:10 +00001133/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001135ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1136 SDValue StackPtr, SDValue Arg,
1137 DebugLoc dl, SelectionDAG &DAG,
1138 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001139 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001140 unsigned LocMemOffset = VA.getLocMemOffset();
1141 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1142 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001143 if (Flags.isByVal())
Bob Wilson1f595bb2009-04-17 19:07:39 +00001144 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001145
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001147 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001148 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001149}
1150
Dan Gohman98ca4f22009-08-05 01:29:28 +00001151void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001152 SDValue Chain, SDValue &Arg,
1153 RegsToPassVector &RegsToPass,
1154 CCValAssign &VA, CCValAssign &NextVA,
1155 SDValue &StackPtr,
1156 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001157 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001158
Jim Grosbache5165492009-11-09 00:11:35 +00001159 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001160 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001161 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1162
1163 if (NextVA.isRegLoc())
1164 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1165 else {
1166 assert(NextVA.isMemLoc());
1167 if (StackPtr.getNode() == 0)
1168 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1169
Dan Gohman98ca4f22009-08-05 01:29:28 +00001170 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1171 dl, DAG, NextVA,
1172 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001173 }
1174}
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001177/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1178/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001180ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001181 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001182 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001184 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 const SmallVectorImpl<ISD::InputArg> &Ins,
1186 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001187 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001188 MachineFunction &MF = DAG.getMachineFunction();
1189 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1190 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001191 // Temporarily disable tail calls so things don't break.
1192 if (!EnableARMTailCalls)
1193 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001194 if (isTailCall) {
1195 // Check if it's really possible to do a tail call.
1196 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1197 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001198 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001199 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1200 // detected sibcalls.
1201 if (isTailCall) {
1202 ++NumTailCalls;
1203 IsSibCall = true;
1204 }
1205 }
Evan Chenga8e29892007-01-19 07:51:42 +00001206
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 // Analyze operands of the call, assigning locations to each operand.
1208 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1210 *DAG.getContext());
1211 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001212 CCAssignFnForNode(CallConv, /* Return*/ false,
1213 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001214
Bob Wilson1f595bb2009-04-17 19:07:39 +00001215 // Get a count of how many bytes are to be pushed on the stack.
1216 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001217
Dale Johannesen51e28e62010-06-03 21:09:53 +00001218 // For tail calls, memory operands are available in our caller's stack.
1219 if (IsSibCall)
1220 NumBytes = 0;
1221
Evan Chenga8e29892007-01-19 07:51:42 +00001222 // Adjust the stack pointer for the new arguments...
1223 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001224 if (!IsSibCall)
1225 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001226
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001227 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001228
Bob Wilson5bafff32009-06-22 23:27:02 +00001229 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001231
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001233 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001234 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1235 i != e;
1236 ++i, ++realArgIdx) {
1237 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001238 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001239 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001240 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001241
Bob Wilson1f595bb2009-04-17 19:07:39 +00001242 // Promote the value if needed.
1243 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001244 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 case CCValAssign::Full: break;
1246 case CCValAssign::SExt:
1247 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1248 break;
1249 case CCValAssign::ZExt:
1250 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1251 break;
1252 case CCValAssign::AExt:
1253 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1254 break;
1255 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001256 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001258 }
1259
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001260 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 if (VA.getLocVT() == MVT::v2f64) {
1263 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1264 DAG.getConstant(0, MVT::i32));
1265 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1266 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001267
Dan Gohman98ca4f22009-08-05 01:29:28 +00001268 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001269 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1270
1271 VA = ArgLocs[++i]; // skip ahead to next loc
1272 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1275 } else {
1276 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001277
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1279 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001280 }
1281 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001282 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001283 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001284 }
1285 } else if (VA.isRegLoc()) {
1286 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsf222e592011-02-28 17:17:53 +00001287 } else if (!IsSibCall || isByVal) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001288 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001289
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1291 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292 }
Evan Chenga8e29892007-01-19 07:51:42 +00001293 }
1294
1295 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001296 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001297 &MemOpChains[0], MemOpChains.size());
1298
1299 // Build a sequence of copy-to-reg nodes chained together with token chain
1300 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001301 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001302 // Tail call byval lowering might overwrite argument registers so in case of
1303 // tail call optimization the copies to registers are lowered later.
1304 if (!isTailCall)
1305 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1306 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1307 RegsToPass[i].second, InFlag);
1308 InFlag = Chain.getValue(1);
1309 }
Evan Chenga8e29892007-01-19 07:51:42 +00001310
Dale Johannesen51e28e62010-06-03 21:09:53 +00001311 // For tail calls lower the arguments to the 'real' stack slot.
1312 if (isTailCall) {
1313 // Force all the incoming stack arguments to be loaded from the stack
1314 // before any new outgoing arguments are stored to the stack, because the
1315 // outgoing stack slots may alias the incoming argument stack slots, and
1316 // the alias isn't otherwise explicit. This is slightly more conservative
1317 // than necessary, because it means that each store effectively depends
1318 // on every argument instead of just those arguments it would clobber.
1319
1320 // Do not flag preceeding copytoreg stuff together with the following stuff.
1321 InFlag = SDValue();
1322 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1323 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1324 RegsToPass[i].second, InFlag);
1325 InFlag = Chain.getValue(1);
1326 }
1327 InFlag =SDValue();
1328 }
1329
Bill Wendling056292f2008-09-16 21:48:12 +00001330 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1331 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1332 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001333 bool isDirect = false;
1334 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001335 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001336 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001337
1338 if (EnableARMLongCalls) {
1339 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1340 && "long-calls with non-static relocation model!");
1341 // Handle a global address or an external symbol. If it's not one of
1342 // those, the target's already in a register, so we don't need to do
1343 // anything extra.
1344 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001345 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001346 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001347 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001348 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1349 ARMPCLabelIndex,
1350 ARMCP::CPValue, 0);
1351 // Get the address of the callee into a register
1352 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1353 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1354 Callee = DAG.getLoad(getPointerTy(), dl,
1355 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001356 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001357 false, false, 0);
1358 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1359 const char *Sym = S->getSymbol();
1360
1361 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001362 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001363 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1364 Sym, ARMPCLabelIndex, 0);
1365 // Get the address of the callee into a register
1366 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1367 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1368 Callee = DAG.getLoad(getPointerTy(), dl,
1369 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001370 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001371 false, false, 0);
1372 }
1373 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001374 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001375 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001376 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001377 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001378 getTargetMachine().getRelocationModel() != Reloc::Static;
1379 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001380 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001381 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001382 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001383 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001384 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001385 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001386 ARMPCLabelIndex,
1387 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001388 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001389 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001390 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001391 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001392 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001393 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001394 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001395 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001396 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001397 } else {
1398 // On ELF targets for PIC code, direct calls should go through the PLT
1399 unsigned OpFlags = 0;
1400 if (Subtarget->isTargetELF() &&
1401 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1402 OpFlags = ARMII::MO_PLT;
1403 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1404 }
Bill Wendling056292f2008-09-16 21:48:12 +00001405 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001406 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001407 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001408 getTargetMachine().getRelocationModel() != Reloc::Static;
1409 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001410 // tBX takes a register source operand.
1411 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001412 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001413 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001414 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001415 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001416 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001417 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001418 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001419 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001420 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001421 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001422 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001423 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001424 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001425 } else {
1426 unsigned OpFlags = 0;
1427 // On ELF targets for PIC code, direct calls should go through the PLT
1428 if (Subtarget->isTargetELF() &&
1429 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1430 OpFlags = ARMII::MO_PLT;
1431 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1432 }
Evan Chenga8e29892007-01-19 07:51:42 +00001433 }
1434
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001435 // FIXME: handle tail calls differently.
1436 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001437 if (Subtarget->isThumb()) {
1438 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001439 CallOpc = ARMISD::CALL_NOLINK;
1440 else
1441 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1442 } else {
1443 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001444 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1445 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001446 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001447
Dan Gohman475871a2008-07-27 21:46:04 +00001448 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001449 Ops.push_back(Chain);
1450 Ops.push_back(Callee);
1451
1452 // Add argument registers to the end of the list so that they are known live
1453 // into the call.
1454 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1455 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1456 RegsToPass[i].second.getValueType()));
1457
Gabor Greifba36cb52008-08-28 21:40:38 +00001458 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001459 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001460
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001461 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001462 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001463 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001464
Duncan Sands4bdcb612008-07-02 17:40:58 +00001465 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001466 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001467 InFlag = Chain.getValue(1);
1468
Chris Lattnere563bbc2008-10-11 22:08:30 +00001469 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1470 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001471 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001472 InFlag = Chain.getValue(1);
1473
Bob Wilson1f595bb2009-04-17 19:07:39 +00001474 // Handle result values, copying them out of physregs into vregs that we
1475 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001476 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1477 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001478}
1479
Stuart Hastingsf222e592011-02-28 17:17:53 +00001480/// HandleByVal - Every parameter *after* a byval parameter is passed
1481/// on the stack. Confiscate all the parameter registers to insure
1482/// this.
1483void
1484llvm::ARMTargetLowering::HandleByVal(CCState *State) const {
1485 static const unsigned RegList1[] = {
1486 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1487 };
1488 do {} while (State->AllocateReg(RegList1, 4));
1489}
1490
Dale Johannesen51e28e62010-06-03 21:09:53 +00001491/// MatchingStackOffset - Return true if the given stack call argument is
1492/// already available in the same position (relatively) of the caller's
1493/// incoming argument stack.
1494static
1495bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1496 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1497 const ARMInstrInfo *TII) {
1498 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1499 int FI = INT_MAX;
1500 if (Arg.getOpcode() == ISD::CopyFromReg) {
1501 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001502 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001503 return false;
1504 MachineInstr *Def = MRI->getVRegDef(VR);
1505 if (!Def)
1506 return false;
1507 if (!Flags.isByVal()) {
1508 if (!TII->isLoadFromStackSlot(Def, FI))
1509 return false;
1510 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001511 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001512 }
1513 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1514 if (Flags.isByVal())
1515 // ByVal argument is passed in as a pointer but it's now being
1516 // dereferenced. e.g.
1517 // define @foo(%struct.X* %A) {
1518 // tail call @bar(%struct.X* byval %A)
1519 // }
1520 return false;
1521 SDValue Ptr = Ld->getBasePtr();
1522 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1523 if (!FINode)
1524 return false;
1525 FI = FINode->getIndex();
1526 } else
1527 return false;
1528
1529 assert(FI != INT_MAX);
1530 if (!MFI->isFixedObjectIndex(FI))
1531 return false;
1532 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1533}
1534
1535/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1536/// for tail call optimization. Targets which want to do tail call
1537/// optimization should implement this function.
1538bool
1539ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1540 CallingConv::ID CalleeCC,
1541 bool isVarArg,
1542 bool isCalleeStructRet,
1543 bool isCallerStructRet,
1544 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001545 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001546 const SmallVectorImpl<ISD::InputArg> &Ins,
1547 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001548 const Function *CallerF = DAG.getMachineFunction().getFunction();
1549 CallingConv::ID CallerCC = CallerF->getCallingConv();
1550 bool CCMatch = CallerCC == CalleeCC;
1551
1552 // Look for obvious safe cases to perform tail call optimization that do not
1553 // require ABI changes. This is what gcc calls sibcall.
1554
Jim Grosbach7616b642010-06-16 23:45:49 +00001555 // Do not sibcall optimize vararg calls unless the call site is not passing
1556 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001557 if (isVarArg && !Outs.empty())
1558 return false;
1559
1560 // Also avoid sibcall optimization if either caller or callee uses struct
1561 // return semantics.
1562 if (isCalleeStructRet || isCallerStructRet)
1563 return false;
1564
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001565 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Evan Cheng0110ac62010-06-19 01:01:32 +00001566 // emitEpilogue is not ready for them.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001567 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1568 // LR. This means if we need to reload LR, it takes an extra instructions,
1569 // which outweighs the value of the tail call; but here we don't know yet
1570 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001571 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001572 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001573
1574 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1575 // but we need to make sure there are enough registers; the only valid
1576 // registers are the 4 used for parameters. We don't currently do this
1577 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001578 if (Subtarget->isThumb1Only())
1579 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001580
Dale Johannesen51e28e62010-06-03 21:09:53 +00001581 // If the calling conventions do not match, then we'd better make sure the
1582 // results are returned in the same way as what the caller expects.
1583 if (!CCMatch) {
1584 SmallVector<CCValAssign, 16> RVLocs1;
1585 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
1586 RVLocs1, *DAG.getContext());
1587 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1588
1589 SmallVector<CCValAssign, 16> RVLocs2;
1590 CCState CCInfo2(CallerCC, false, getTargetMachine(),
1591 RVLocs2, *DAG.getContext());
1592 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1593
1594 if (RVLocs1.size() != RVLocs2.size())
1595 return false;
1596 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1597 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1598 return false;
1599 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1600 return false;
1601 if (RVLocs1[i].isRegLoc()) {
1602 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1603 return false;
1604 } else {
1605 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1606 return false;
1607 }
1608 }
1609 }
1610
1611 // If the callee takes no arguments then go on to check the results of the
1612 // call.
1613 if (!Outs.empty()) {
1614 // Check if stack adjustment is needed. For now, do not do this if any
1615 // argument is passed on the stack.
1616 SmallVector<CCValAssign, 16> ArgLocs;
1617 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
1618 ArgLocs, *DAG.getContext());
1619 CCInfo.AnalyzeCallOperands(Outs,
1620 CCAssignFnForNode(CalleeCC, false, isVarArg));
1621 if (CCInfo.getNextStackOffset()) {
1622 MachineFunction &MF = DAG.getMachineFunction();
1623
1624 // Check if the arguments are already laid out in the right way as
1625 // the caller's fixed stack objects.
1626 MachineFrameInfo *MFI = MF.getFrameInfo();
1627 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1628 const ARMInstrInfo *TII =
1629 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001630 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1631 i != e;
1632 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001633 CCValAssign &VA = ArgLocs[i];
1634 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001635 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001636 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001637 if (VA.getLocInfo() == CCValAssign::Indirect)
1638 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001639 if (VA.needsCustom()) {
1640 // f64 and vector types are split into multiple registers or
1641 // register/stack-slot combinations. The types will not match
1642 // the registers; give up on memory f64 refs until we figure
1643 // out what to do about this.
1644 if (!VA.isRegLoc())
1645 return false;
1646 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001647 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001648 if (RegVT == MVT::v2f64) {
1649 if (!ArgLocs[++i].isRegLoc())
1650 return false;
1651 if (!ArgLocs[++i].isRegLoc())
1652 return false;
1653 }
1654 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001655 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1656 MFI, MRI, TII))
1657 return false;
1658 }
1659 }
1660 }
1661 }
1662
1663 return true;
1664}
1665
Dan Gohman98ca4f22009-08-05 01:29:28 +00001666SDValue
1667ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001668 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001670 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001671 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001672
Bob Wilsondee46d72009-04-17 20:35:10 +00001673 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001674 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001675
Bob Wilsondee46d72009-04-17 20:35:10 +00001676 // CCState - Info about the registers and stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001677 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1678 *DAG.getContext());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001679
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001681 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1682 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001683
1684 // If this is the first return lowered for this function, add
1685 // the regs to the liveout set for the function.
1686 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1687 for (unsigned i = 0; i != RVLocs.size(); ++i)
1688 if (RVLocs[i].isRegLoc())
1689 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001690 }
1691
Bob Wilson1f595bb2009-04-17 19:07:39 +00001692 SDValue Flag;
1693
1694 // Copy the result values into the output registers.
1695 for (unsigned i = 0, realRVLocIdx = 0;
1696 i != RVLocs.size();
1697 ++i, ++realRVLocIdx) {
1698 CCValAssign &VA = RVLocs[i];
1699 assert(VA.isRegLoc() && "Can only return in registers!");
1700
Dan Gohmanc9403652010-07-07 15:54:55 +00001701 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001702
1703 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001704 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001705 case CCValAssign::Full: break;
1706 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001707 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001708 break;
1709 }
1710
Bob Wilson1f595bb2009-04-17 19:07:39 +00001711 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001713 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001714 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1715 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001716 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001717 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001718
1719 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1720 Flag = Chain.getValue(1);
1721 VA = RVLocs[++i]; // skip ahead to next loc
1722 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1723 HalfGPRs.getValue(1), Flag);
1724 Flag = Chain.getValue(1);
1725 VA = RVLocs[++i]; // skip ahead to next loc
1726
1727 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1729 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001730 }
1731 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1732 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001733 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001734 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001735 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001736 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001737 VA = RVLocs[++i]; // skip ahead to next loc
1738 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1739 Flag);
1740 } else
1741 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1742
Bob Wilsondee46d72009-04-17 20:35:10 +00001743 // Guarantee that all emitted copies are
1744 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001745 Flag = Chain.getValue(1);
1746 }
1747
1748 SDValue result;
1749 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001750 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001751 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001753
1754 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001755}
1756
Evan Cheng3d2125c2010-11-30 23:55:39 +00001757bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1758 if (N->getNumValues() != 1)
1759 return false;
1760 if (!N->hasNUsesOfValue(1, 0))
1761 return false;
1762
1763 unsigned NumCopies = 0;
1764 SDNode* Copies[2];
1765 SDNode *Use = *N->use_begin();
1766 if (Use->getOpcode() == ISD::CopyToReg) {
1767 Copies[NumCopies++] = Use;
1768 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1769 // f64 returned in a pair of GPRs.
1770 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1771 UI != UE; ++UI) {
1772 if (UI->getOpcode() != ISD::CopyToReg)
1773 return false;
1774 Copies[UI.getUse().getResNo()] = *UI;
1775 ++NumCopies;
1776 }
1777 } else if (Use->getOpcode() == ISD::BITCAST) {
1778 // f32 returned in a single GPR.
1779 if (!Use->hasNUsesOfValue(1, 0))
1780 return false;
1781 Use = *Use->use_begin();
1782 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1783 return false;
1784 Copies[NumCopies++] = Use;
1785 } else {
1786 return false;
1787 }
1788
1789 if (NumCopies != 1 && NumCopies != 2)
1790 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001791
1792 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001793 for (unsigned i = 0; i < NumCopies; ++i) {
1794 SDNode *Copy = Copies[i];
1795 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1796 UI != UE; ++UI) {
1797 if (UI->getOpcode() == ISD::CopyToReg) {
1798 SDNode *Use = *UI;
1799 if (Use == Copies[0] || Use == Copies[1])
1800 continue;
1801 return false;
1802 }
1803 if (UI->getOpcode() != ARMISD::RET_FLAG)
1804 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001805 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001806 }
1807 }
1808
Evan Cheng1bf891a2010-12-01 22:59:46 +00001809 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001810}
1811
Evan Cheng485fafc2011-03-21 01:19:09 +00001812bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1813 if (!EnableARMTailCalls)
1814 return false;
1815
1816 if (!CI->isTailCall())
1817 return false;
1818
1819 return !Subtarget->isThumb1Only();
1820}
1821
Bob Wilsonb62d2572009-11-03 00:02:05 +00001822// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1823// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1824// one of the above mentioned nodes. It has to be wrapped because otherwise
1825// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1826// be used to form addressing mode. These wrapped nodes will be selected
1827// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001828static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001829 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001830 // FIXME there is no actual debug info here
1831 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001832 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001833 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001834 if (CP->isMachineConstantPoolEntry())
1835 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1836 CP->getAlignment());
1837 else
1838 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1839 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001841}
1842
Jim Grosbache1102ca2010-07-19 17:20:38 +00001843unsigned ARMTargetLowering::getJumpTableEncoding() const {
1844 return MachineJumpTableInfo::EK_Inline;
1845}
1846
Dan Gohmand858e902010-04-17 15:26:15 +00001847SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1848 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001849 MachineFunction &MF = DAG.getMachineFunction();
1850 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1851 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001852 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001853 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001854 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001855 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1856 SDValue CPAddr;
1857 if (RelocM == Reloc::Static) {
1858 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1859 } else {
1860 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001861 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001862 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1863 ARMCP::CPBlockAddress,
1864 PCAdj);
1865 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1866 }
1867 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1868 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001869 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001870 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001871 if (RelocM == Reloc::Static)
1872 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001873 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001874 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001875}
1876
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001877// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001878SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001879ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001880 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001881 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001882 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001883 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001884 MachineFunction &MF = DAG.getMachineFunction();
1885 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001886 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001887 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001888 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001889 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001890 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001891 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001892 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001893 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001894 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001895 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001896
Evan Chenge7e0d622009-11-06 22:24:13 +00001897 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001898 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001899
1900 // call __tls_get_addr.
1901 ArgListTy Args;
1902 ArgListEntry Entry;
1903 Entry.Node = Argument;
Owen Anderson1d0be152009-08-13 21:58:54 +00001904 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001905 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001906 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001907 std::pair<SDValue, SDValue> CallResult =
Evan Cheng59bc0602009-08-14 19:11:20 +00001908 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1909 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001910 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001911 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001912 return CallResult.first;
1913}
1914
1915// Lower ISD::GlobalTLSAddress using the "initial exec" or
1916// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00001917SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001918ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001919 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00001920 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001921 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00001922 SDValue Offset;
1923 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001924 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001925 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001926 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001927
Chris Lattner4fb63d02009-07-15 04:12:33 +00001928 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00001929 MachineFunction &MF = DAG.getMachineFunction();
1930 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001931 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00001932 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001933 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1934 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001935 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001936 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001937 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001939 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001940 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001941 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001942 Chain = Offset.getValue(1);
1943
Evan Chenge7e0d622009-11-06 22:24:13 +00001944 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001945 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001946
Evan Cheng9eda6892009-10-31 03:39:36 +00001947 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001948 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001949 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001950 } else {
1951 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001952 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001953 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00001955 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001956 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001957 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001958 }
1959
1960 // The address of the thread local variable is the add of the thread
1961 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001962 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001963}
1964
Dan Gohman475871a2008-07-27 21:46:04 +00001965SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00001966ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001967 // TODO: implement the "local dynamic" model
1968 assert(Subtarget->isTargetELF() &&
1969 "TLS not implemented for non-ELF targets");
1970 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1971 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1972 // otherwise use the "Local Exec" TLS Model
1973 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1974 return LowerToTLSGeneralDynamicModel(GA, DAG);
1975 else
1976 return LowerToTLSExecModels(GA, DAG);
1977}
1978
Dan Gohman475871a2008-07-27 21:46:04 +00001979SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001980 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001981 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001982 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001983 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001984 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1985 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00001986 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001987 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001988 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001989 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001990 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001991 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001992 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001993 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001994 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001995 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001996 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001997 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00001998 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00001999 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002000 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002001 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002002 }
2003
2004 // If we have T2 ops, we can materialize the address directly via movt/movw
2005 // pair. This is always cheaper.
2006 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002007 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002008 // FIXME: Once remat is capable of dealing with instructions with register
2009 // operands, expand this into two nodes.
2010 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2011 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002012 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002013 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2014 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2015 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2016 MachinePointerInfo::getConstantPool(),
2017 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002018 }
2019}
2020
Dan Gohman475871a2008-07-27 21:46:04 +00002021SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002022 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002023 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002024 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002025 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002026 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002027 MachineFunction &MF = DAG.getMachineFunction();
2028 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2029
2030 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002031 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002032 // FIXME: Once remat is capable of dealing with instructions with register
2033 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002034 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002035 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2036 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2037
Evan Cheng53519f02011-01-21 18:55:51 +00002038 unsigned Wrapper = (RelocM == Reloc::PIC_)
2039 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2040 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002041 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002042 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2043 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2044 MachinePointerInfo::getGOT(), false, false, 0);
2045 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002046 }
2047
2048 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002050 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002051 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002052 } else {
2053 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002054 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2055 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002056 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002057 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002058 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002060
Evan Cheng9eda6892009-10-31 03:39:36 +00002061 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002062 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002063 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002064 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002065
2066 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002067 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002068 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002069 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002070
Evan Cheng63476a82009-09-03 07:04:02 +00002071 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002072 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002073 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002074
2075 return Result;
2076}
2077
Dan Gohman475871a2008-07-27 21:46:04 +00002078SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002079 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002080 assert(Subtarget->isTargetELF() &&
2081 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002082 MachineFunction &MF = DAG.getMachineFunction();
2083 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002084 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002085 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002086 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002087 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002088 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2089 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002090 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002091 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002092 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002093 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002094 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002095 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002096 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002097 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002098}
2099
Jim Grosbach0e0da732009-05-12 23:59:14 +00002100SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002101ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2102 const {
2103 DebugLoc dl = Op.getDebugLoc();
2104 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
2105 Op.getOperand(0), Op.getOperand(1));
2106}
2107
2108SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002109ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2110 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002111 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002112 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2113 Op.getOperand(1), Val);
2114}
2115
2116SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002117ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2118 DebugLoc dl = Op.getDebugLoc();
2119 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2120 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2121}
2122
2123SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002124ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002125 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002126 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002127 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002128 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002129 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002130 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002131 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002132 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2133 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002134 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002135 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002136 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002137 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002138 EVT PtrVT = getPointerTy();
2139 DebugLoc dl = Op.getDebugLoc();
2140 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2141 SDValue CPAddr;
2142 unsigned PCAdj = (RelocM != Reloc::PIC_)
2143 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002144 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002145 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2146 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002147 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002149 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002150 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002151 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002152 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002153
2154 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002155 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002156 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2157 }
2158 return Result;
2159 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002160 }
2161}
2162
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002163static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002164 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002165 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002166 if (!Subtarget->hasDataBarrier()) {
2167 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2168 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2169 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002170 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002171 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002172 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002173 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002174 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002175
2176 SDValue Op5 = Op.getOperand(5);
2177 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2178 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2179 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2180 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2181
2182 ARM_MB::MemBOpt DMBOpt;
2183 if (isDeviceBarrier)
2184 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2185 else
2186 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2187 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2188 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002189}
2190
Evan Chengdfed19f2010-11-03 06:34:55 +00002191static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2192 const ARMSubtarget *Subtarget) {
2193 // ARM pre v5TE and Thumb1 does not have preload instructions.
2194 if (!(Subtarget->isThumb2() ||
2195 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2196 // Just preserve the chain.
2197 return Op.getOperand(0);
2198
2199 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002200 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2201 if (!isRead &&
2202 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2203 // ARMv7 with MP extension has PLDW.
2204 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002205
2206 if (Subtarget->isThumb())
2207 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002208 isRead = ~isRead & 1;
2209 unsigned isData = Subtarget->isThumb() ? 0 : 1;
Evan Chengdfed19f2010-11-03 06:34:55 +00002210
Evan Cheng416941d2010-11-04 05:19:35 +00002211 // Currently there is no intrinsic that matches pli.
Evan Chengdfed19f2010-11-03 06:34:55 +00002212 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002213 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2214 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002215}
2216
Dan Gohman1e93df62010-04-17 14:41:14 +00002217static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2218 MachineFunction &MF = DAG.getMachineFunction();
2219 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2220
Evan Chenga8e29892007-01-19 07:51:42 +00002221 // vastart just stores the address of the VarArgsFrameIndex slot into the
2222 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002223 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002224 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002225 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002226 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002227 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2228 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002229}
2230
Dan Gohman475871a2008-07-27 21:46:04 +00002231SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002232ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2233 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002234 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002235 MachineFunction &MF = DAG.getMachineFunction();
2236 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2237
2238 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002239 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002240 RC = ARM::tGPRRegisterClass;
2241 else
2242 RC = ARM::GPRRegisterClass;
2243
2244 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002245 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002246 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002247
2248 SDValue ArgValue2;
2249 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002250 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002251 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002252
2253 // Create load node to retrieve arguments from the stack.
2254 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002255 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002256 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002257 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002258 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002259 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002260 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002261 }
2262
Jim Grosbache5165492009-11-09 00:11:35 +00002263 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002264}
2265
2266SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002267ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002268 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002269 const SmallVectorImpl<ISD::InputArg>
2270 &Ins,
2271 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002272 SmallVectorImpl<SDValue> &InVals)
2273 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002274
Bob Wilson1f595bb2009-04-17 19:07:39 +00002275 MachineFunction &MF = DAG.getMachineFunction();
2276 MachineFrameInfo *MFI = MF.getFrameInfo();
2277
Bob Wilson1f595bb2009-04-17 19:07:39 +00002278 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2279
2280 // Assign locations to all of the incoming arguments.
2281 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002282 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
2283 *DAG.getContext());
2284 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002285 CCAssignFnForNode(CallConv, /* Return*/ false,
2286 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002287
2288 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002289 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002290
Stuart Hastingsf222e592011-02-28 17:17:53 +00002291 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002292 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2293 CCValAssign &VA = ArgLocs[i];
2294
Bob Wilsondee46d72009-04-17 20:35:10 +00002295 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002296 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002297 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002298
Bob Wilson1f595bb2009-04-17 19:07:39 +00002299 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002300 // f64 and vector types are split up into multiple registers or
2301 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002302 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002303 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002304 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002305 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002306 SDValue ArgValue2;
2307 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002308 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002309 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2310 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002311 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002312 false, false, 0);
2313 } else {
2314 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2315 Chain, DAG, dl);
2316 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002317 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2318 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002319 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002320 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002321 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2322 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002323 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002324
Bob Wilson5bafff32009-06-22 23:27:02 +00002325 } else {
2326 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002327
Owen Anderson825b72b2009-08-11 20:47:22 +00002328 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002329 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002330 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002331 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002332 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002333 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002334 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002335 RC = (AFI->isThumb1OnlyFunction() ?
2336 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002337 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002338 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002339
2340 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002341 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002342 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002343 }
2344
2345 // If this is an 8 or 16-bit value, it is really passed promoted
2346 // to 32 bits. Insert an assert[sz]ext to capture this, then
2347 // truncate to the right size.
2348 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002349 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002350 case CCValAssign::Full: break;
2351 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002352 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002353 break;
2354 case CCValAssign::SExt:
2355 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2356 DAG.getValueType(VA.getValVT()));
2357 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2358 break;
2359 case CCValAssign::ZExt:
2360 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2361 DAG.getValueType(VA.getValVT()));
2362 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2363 break;
2364 }
2365
Dan Gohman98ca4f22009-08-05 01:29:28 +00002366 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002367
2368 } else { // VA.isRegLoc()
2369
2370 // sanity check
2371 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002372 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002373
Stuart Hastingsf222e592011-02-28 17:17:53 +00002374 int index = ArgLocs[i].getValNo();
2375
2376 // Some Ins[] entries become multiple ArgLoc[] entries.
2377 // Process them only once.
2378 if (index != lastInsIndex)
2379 {
2380 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2381 // FIXME: For now, all byval parameter objects are marked mutable. This can be
2382 // changed with more analysis.
2383 // In case of tail call optimization mark all arguments mutable. Since they
2384 // could be overwritten by lowering of arguments in case of a tail call.
2385 if (Flags.isByVal()) {
2386 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2387 VA.getLocMemOffset(), false);
2388 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2389 } else {
2390 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2391 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002392
Stuart Hastingsf222e592011-02-28 17:17:53 +00002393 // Create load nodes to retrieve arguments from the stack.
2394 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2395 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2396 MachinePointerInfo::getFixedStack(FI),
2397 false, false, 0));
2398 }
2399 lastInsIndex = index;
2400 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002401 }
2402 }
2403
2404 // varargs
Evan Chenga8e29892007-01-19 07:51:42 +00002405 if (isVarArg) {
2406 static const unsigned GPRArgRegs[] = {
2407 ARM::R0, ARM::R1, ARM::R2, ARM::R3
2408 };
2409
Bob Wilsondee46d72009-04-17 20:35:10 +00002410 unsigned NumGPRs = CCInfo.getFirstUnallocated
2411 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002412
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002413 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Lauro Ramos Venancio600c3832007-02-23 20:32:57 +00002414 unsigned VARegSize = (4 - NumGPRs) * 4;
2415 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
Rafael Espindolac1382b72009-10-30 14:33:14 +00002416 unsigned ArgOffset = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00002417 if (VARegSaveSize) {
2418 // If this function is vararg, store any remaining integer argument regs
2419 // to their spots on the stack so that they may be loaded by deferencing
2420 // the result of va_next.
2421 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Dan Gohman1e93df62010-04-17 14:41:14 +00002422 AFI->setVarArgsFrameIndex(
2423 MFI->CreateFixedObject(VARegSaveSize,
2424 ArgOffset + VARegSaveSize - VARegSize,
Jim Grosbachfd529062010-10-15 18:34:47 +00002425 false));
Dan Gohman1e93df62010-04-17 14:41:14 +00002426 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2427 getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00002428
Dan Gohman475871a2008-07-27 21:46:04 +00002429 SmallVector<SDValue, 4> MemOps;
Evan Chenga8e29892007-01-19 07:51:42 +00002430 for (; NumGPRs < 4; ++NumGPRs) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002431 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002432 if (AFI->isThumb1OnlyFunction())
Bob Wilson1f595bb2009-04-17 19:07:39 +00002433 RC = ARM::tGPRRegisterClass;
Jim Grosbach30eae3c2009-04-07 20:34:09 +00002434 else
Bob Wilson1f595bb2009-04-17 19:07:39 +00002435 RC = ARM::GPRRegisterClass;
2436
Devang Patel68e6bee2011-02-21 23:21:26 +00002437 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002438 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Dan Gohman1e93df62010-04-17 14:41:14 +00002439 SDValue Store =
2440 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002441 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
2442 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002443 MemOps.push_back(Store);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002444 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Evan Chenga8e29892007-01-19 07:51:42 +00002445 DAG.getConstant(4, getPointerTy()));
2446 }
2447 if (!MemOps.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002448 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002449 &MemOps[0], MemOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002450 } else
2451 // This will point to the next argument passed via stack.
Evan Chenged2ae132010-07-03 00:40:23 +00002452 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
Evan Chenga8e29892007-01-19 07:51:42 +00002453 }
2454
Dan Gohman98ca4f22009-08-05 01:29:28 +00002455 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002456}
2457
2458/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002459static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002460 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002461 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002462 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002463 // Maybe this has already been legalized into the constant pool?
2464 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002465 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002466 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002467 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002468 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002469 }
2470 }
2471 return false;
2472}
2473
Evan Chenga8e29892007-01-19 07:51:42 +00002474/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2475/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002476SDValue
2477ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002478 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002479 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002480 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002481 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002482 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002483 // Constant does not fit, try adjusting it by one?
2484 switch (CC) {
2485 default: break;
2486 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002487 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002488 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002489 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002490 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002491 }
2492 break;
2493 case ISD::SETULT:
2494 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002495 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002496 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002497 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002498 }
2499 break;
2500 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002501 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002502 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002503 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
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::SETULE:
2508 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002509 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002510 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
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 }
2515 }
2516 }
2517
2518 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002519 ARMISD::NodeType CompareType;
2520 switch (CondCode) {
2521 default:
2522 CompareType = ARMISD::CMP;
2523 break;
2524 case ARMCC::EQ:
2525 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002526 // Uses only Z Flag
2527 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002528 break;
2529 }
Evan Cheng218977b2010-07-13 19:27:42 +00002530 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002531 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002532}
2533
2534/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002535SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002536ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002537 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002538 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002539 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002540 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002541 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002542 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2543 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002544}
2545
Bob Wilson79f56c92011-03-08 01:17:20 +00002546/// duplicateCmp - Glue values can have only one use, so this function
2547/// duplicates a comparison node.
2548SDValue
2549ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2550 unsigned Opc = Cmp.getOpcode();
2551 DebugLoc DL = Cmp.getDebugLoc();
2552 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2553 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2554
2555 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2556 Cmp = Cmp.getOperand(0);
2557 Opc = Cmp.getOpcode();
2558 if (Opc == ARMISD::CMPFP)
2559 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2560 else {
2561 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2562 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2563 }
2564 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2565}
2566
Bill Wendlingde2b1512010-08-11 08:43:16 +00002567SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2568 SDValue Cond = Op.getOperand(0);
2569 SDValue SelectTrue = Op.getOperand(1);
2570 SDValue SelectFalse = Op.getOperand(2);
2571 DebugLoc dl = Op.getDebugLoc();
2572
2573 // Convert:
2574 //
2575 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2576 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2577 //
2578 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2579 const ConstantSDNode *CMOVTrue =
2580 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2581 const ConstantSDNode *CMOVFalse =
2582 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2583
2584 if (CMOVTrue && CMOVFalse) {
2585 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2586 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2587
2588 SDValue True;
2589 SDValue False;
2590 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2591 True = SelectTrue;
2592 False = SelectFalse;
2593 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2594 True = SelectFalse;
2595 False = SelectTrue;
2596 }
2597
2598 if (True.getNode() && False.getNode()) {
2599 EVT VT = Cond.getValueType();
2600 SDValue ARMcc = Cond.getOperand(2);
2601 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002602 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002603 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
2604 }
2605 }
2606 }
2607
2608 return DAG.getSelectCC(dl, Cond,
2609 DAG.getConstant(0, Cond.getValueType()),
2610 SelectTrue, SelectFalse, ISD::SETNE);
2611}
2612
Dan Gohmand858e902010-04-17 15:26:15 +00002613SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002614 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002615 SDValue LHS = Op.getOperand(0);
2616 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002617 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002618 SDValue TrueVal = Op.getOperand(2);
2619 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002620 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002621
Owen Anderson825b72b2009-08-11 20:47:22 +00002622 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002623 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002624 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002625 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2626 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002627 }
2628
2629 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002630 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002631
Evan Cheng218977b2010-07-13 19:27:42 +00002632 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2633 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002635 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002636 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002637 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002638 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002639 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002640 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002641 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002642 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002643 }
2644 return Result;
2645}
2646
Evan Cheng218977b2010-07-13 19:27:42 +00002647/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2648/// to morph to an integer compare sequence.
2649static bool canChangeToInt(SDValue Op, bool &SeenZero,
2650 const ARMSubtarget *Subtarget) {
2651 SDNode *N = Op.getNode();
2652 if (!N->hasOneUse())
2653 // Otherwise it requires moving the value from fp to integer registers.
2654 return false;
2655 if (!N->getNumValues())
2656 return false;
2657 EVT VT = Op.getValueType();
2658 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2659 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2660 // vmrs are very slow, e.g. cortex-a8.
2661 return false;
2662
2663 if (isFloatingPointZero(Op)) {
2664 SeenZero = true;
2665 return true;
2666 }
2667 return ISD::isNormalLoad(N);
2668}
2669
2670static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2671 if (isFloatingPointZero(Op))
2672 return DAG.getConstant(0, MVT::i32);
2673
2674 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2675 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002676 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002677 Ld->isVolatile(), Ld->isNonTemporal(),
2678 Ld->getAlignment());
2679
2680 llvm_unreachable("Unknown VFP cmp argument!");
2681}
2682
2683static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2684 SDValue &RetVal1, SDValue &RetVal2) {
2685 if (isFloatingPointZero(Op)) {
2686 RetVal1 = DAG.getConstant(0, MVT::i32);
2687 RetVal2 = DAG.getConstant(0, MVT::i32);
2688 return;
2689 }
2690
2691 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2692 SDValue Ptr = Ld->getBasePtr();
2693 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2694 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002695 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002696 Ld->isVolatile(), Ld->isNonTemporal(),
2697 Ld->getAlignment());
2698
2699 EVT PtrType = Ptr.getValueType();
2700 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2701 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2702 PtrType, Ptr, DAG.getConstant(4, PtrType));
2703 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2704 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002705 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002706 Ld->isVolatile(), Ld->isNonTemporal(),
2707 NewAlign);
2708 return;
2709 }
2710
2711 llvm_unreachable("Unknown VFP cmp argument!");
2712}
2713
2714/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2715/// f32 and even f64 comparisons to integer ones.
2716SDValue
2717ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2718 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002719 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002720 SDValue LHS = Op.getOperand(2);
2721 SDValue RHS = Op.getOperand(3);
2722 SDValue Dest = Op.getOperand(4);
2723 DebugLoc dl = Op.getDebugLoc();
2724
2725 bool SeenZero = false;
2726 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2727 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002728 // If one of the operand is zero, it's safe to ignore the NaN case since
2729 // we only care about equality comparisons.
2730 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002731 // If unsafe fp math optimization is enabled and there are no other uses of
2732 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002733 // to an integer comparison.
2734 if (CC == ISD::SETOEQ)
2735 CC = ISD::SETEQ;
2736 else if (CC == ISD::SETUNE)
2737 CC = ISD::SETNE;
2738
2739 SDValue ARMcc;
2740 if (LHS.getValueType() == MVT::f32) {
2741 LHS = bitcastf32Toi32(LHS, DAG);
2742 RHS = bitcastf32Toi32(RHS, DAG);
2743 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2744 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2745 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2746 Chain, Dest, ARMcc, CCR, Cmp);
2747 }
2748
2749 SDValue LHS1, LHS2;
2750 SDValue RHS1, RHS2;
2751 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2752 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2753 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2754 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002755 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002756 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2757 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2758 }
2759
2760 return SDValue();
2761}
2762
2763SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2764 SDValue Chain = Op.getOperand(0);
2765 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2766 SDValue LHS = Op.getOperand(2);
2767 SDValue RHS = Op.getOperand(3);
2768 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002769 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002770
Owen Anderson825b72b2009-08-11 20:47:22 +00002771 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002772 SDValue ARMcc;
2773 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002774 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002775 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002776 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002777 }
2778
Owen Anderson825b72b2009-08-11 20:47:22 +00002779 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002780
2781 if (UnsafeFPMath &&
2782 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2783 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2784 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2785 if (Result.getNode())
2786 return Result;
2787 }
2788
Evan Chenga8e29892007-01-19 07:51:42 +00002789 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002790 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002791
Evan Cheng218977b2010-07-13 19:27:42 +00002792 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2793 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002794 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002795 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002796 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002797 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002798 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002799 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2800 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002801 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002802 }
2803 return Res;
2804}
2805
Dan Gohmand858e902010-04-17 15:26:15 +00002806SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002807 SDValue Chain = Op.getOperand(0);
2808 SDValue Table = Op.getOperand(1);
2809 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002810 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002811
Owen Andersone50ed302009-08-10 22:56:29 +00002812 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002813 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2814 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002815 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002816 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002817 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002818 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2819 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002820 if (Subtarget->isThumb2()) {
2821 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2822 // which does another jump to the destination. This also makes it easier
2823 // to translate it to TBB / TBH later.
2824 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002825 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002826 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002827 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002828 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002829 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002830 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002831 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002832 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002833 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002834 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002835 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002836 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002837 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002838 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002839 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002840 }
Evan Chenga8e29892007-01-19 07:51:42 +00002841}
2842
Bob Wilson76a312b2010-03-19 22:51:32 +00002843static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2844 DebugLoc dl = Op.getDebugLoc();
2845 unsigned Opc;
2846
2847 switch (Op.getOpcode()) {
2848 default:
2849 assert(0 && "Invalid opcode!");
2850 case ISD::FP_TO_SINT:
2851 Opc = ARMISD::FTOSI;
2852 break;
2853 case ISD::FP_TO_UINT:
2854 Opc = ARMISD::FTOUI;
2855 break;
2856 }
2857 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002858 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00002859}
2860
Cameron Zwarich3007d332011-03-29 21:41:55 +00002861static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2862 EVT VT = Op.getValueType();
2863 DebugLoc dl = Op.getDebugLoc();
2864
2865 EVT OperandVT = Op.getOperand(0).getValueType();
2866 assert(OperandVT == MVT::v4i16 && "Invalid type for custom lowering!");
2867 if (VT != MVT::v4f32)
2868 return DAG.UnrollVectorOp(Op.getNode());
2869
2870 unsigned CastOpc;
2871 unsigned Opc;
2872 switch (Op.getOpcode()) {
2873 default:
2874 assert(0 && "Invalid opcode!");
2875 case ISD::SINT_TO_FP:
2876 CastOpc = ISD::SIGN_EXTEND;
2877 Opc = ISD::SINT_TO_FP;
2878 break;
2879 case ISD::UINT_TO_FP:
2880 CastOpc = ISD::ZERO_EXTEND;
2881 Opc = ISD::UINT_TO_FP;
2882 break;
2883 }
2884
2885 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
2886 return DAG.getNode(Opc, dl, VT, Op);
2887}
2888
Bob Wilson76a312b2010-03-19 22:51:32 +00002889static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2890 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00002891 if (VT.isVector())
2892 return LowerVectorINT_TO_FP(Op, DAG);
2893
Bob Wilson76a312b2010-03-19 22:51:32 +00002894 DebugLoc dl = Op.getDebugLoc();
2895 unsigned Opc;
2896
2897 switch (Op.getOpcode()) {
2898 default:
2899 assert(0 && "Invalid opcode!");
2900 case ISD::SINT_TO_FP:
2901 Opc = ARMISD::SITOF;
2902 break;
2903 case ISD::UINT_TO_FP:
2904 Opc = ARMISD::UITOF;
2905 break;
2906 }
2907
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002908 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00002909 return DAG.getNode(Opc, dl, VT, Op);
2910}
2911
Evan Cheng515fe3a2010-07-08 02:08:50 +00002912SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00002913 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00002914 SDValue Tmp0 = Op.getOperand(0);
2915 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00002916 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002917 EVT VT = Op.getValueType();
2918 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00002919 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
2920 Tmp0.getOpcode() == ARMISD::VMOVDRR;
2921 bool UseNEON = !InGPR && Subtarget->hasNEON();
2922
2923 if (UseNEON) {
2924 // Use VBSL to copy the sign bit.
2925 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
2926 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
2927 DAG.getTargetConstant(EncodedVal, MVT::i32));
2928 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
2929 if (VT == MVT::f64)
2930 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
2931 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
2932 DAG.getConstant(32, MVT::i32));
2933 else /*if (VT == MVT::f32)*/
2934 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
2935 if (SrcVT == MVT::f32) {
2936 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
2937 if (VT == MVT::f64)
2938 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
2939 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
2940 DAG.getConstant(32, MVT::i32));
2941 }
2942 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
2943 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
2944
2945 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
2946 MVT::i32);
2947 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
2948 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
2949 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
2950
2951 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
2952 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
2953 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00002954 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00002955 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
2956 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
2957 DAG.getConstant(0, MVT::i32));
2958 } else {
2959 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
2960 }
2961
2962 return Res;
2963 }
Evan Chengc143dd42011-02-11 02:28:55 +00002964
2965 // Bitcast operand 1 to i32.
2966 if (SrcVT == MVT::f64)
2967 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2968 &Tmp1, 1).getValue(1);
2969 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
2970
Evan Chenge573fb32011-02-23 02:24:55 +00002971 // Or in the signbit with integer operations.
2972 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
2973 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
2974 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
2975 if (VT == MVT::f32) {
2976 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
2977 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
2978 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
2979 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00002980 }
2981
Evan Chenge573fb32011-02-23 02:24:55 +00002982 // f64: Or the high part with signbit and then combine two parts.
2983 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
2984 &Tmp0, 1);
2985 SDValue Lo = Tmp0.getValue(0);
2986 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
2987 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
2988 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00002989}
2990
Evan Cheng2457f2c2010-05-22 01:47:14 +00002991SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2992 MachineFunction &MF = DAG.getMachineFunction();
2993 MachineFrameInfo *MFI = MF.getFrameInfo();
2994 MFI->setReturnAddressIsTaken(true);
2995
2996 EVT VT = Op.getValueType();
2997 DebugLoc dl = Op.getDebugLoc();
2998 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2999 if (Depth) {
3000 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3001 SDValue Offset = DAG.getConstant(4, MVT::i32);
3002 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3003 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003004 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003005 }
3006
3007 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003008 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003009 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3010}
3011
Dan Gohmand858e902010-04-17 15:26:15 +00003012SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003013 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3014 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003015
Owen Andersone50ed302009-08-10 22:56:29 +00003016 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003017 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3018 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003019 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003020 ? ARM::R7 : ARM::R11;
3021 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3022 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003023 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3024 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003025 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003026 return FrameAddr;
3027}
3028
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003029/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003030/// expand a bit convert where either the source or destination type is i64 to
3031/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3032/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3033/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003034static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003035 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3036 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003037 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003038
Bob Wilson9f3f0612010-04-17 05:30:19 +00003039 // This function is only supposed to be called for i64 types, either as the
3040 // source or destination of the bit convert.
3041 EVT SrcVT = Op.getValueType();
3042 EVT DstVT = N->getValueType(0);
3043 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003044 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003045
Bob Wilson9f3f0612010-04-17 05:30:19 +00003046 // Turn i64->f64 into VMOVDRR.
3047 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003048 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3049 DAG.getConstant(0, MVT::i32));
3050 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3051 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003052 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003053 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003054 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003055
Jim Grosbache5165492009-11-09 00:11:35 +00003056 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003057 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3058 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3059 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3060 // Merge the pieces into a single i64 value.
3061 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3062 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003063
Bob Wilson9f3f0612010-04-17 05:30:19 +00003064 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003065}
3066
Bob Wilson5bafff32009-06-22 23:27:02 +00003067/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003068/// Zero vectors are used to represent vector negation and in those cases
3069/// will be implemented with the NEON VNEG instruction. However, VNEG does
3070/// not support i64 elements, so sometimes the zero vectors will need to be
3071/// explicitly constructed. Regardless, use a canonical VMOV to create the
3072/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003073static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003074 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003075 // The canonical modified immediate encoding of a zero vector is....0!
3076 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3077 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3078 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003079 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003080}
3081
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003082/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3083/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003084SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3085 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003086 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3087 EVT VT = Op.getValueType();
3088 unsigned VTBits = VT.getSizeInBits();
3089 DebugLoc dl = Op.getDebugLoc();
3090 SDValue ShOpLo = Op.getOperand(0);
3091 SDValue ShOpHi = Op.getOperand(1);
3092 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003093 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003094 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003095
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003096 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3097
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003098 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3099 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3100 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3101 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3102 DAG.getConstant(VTBits, MVT::i32));
3103 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3104 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003105 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003106
3107 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3108 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003109 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003110 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003111 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003112 CCR, Cmp);
3113
3114 SDValue Ops[2] = { Lo, Hi };
3115 return DAG.getMergeValues(Ops, 2, dl);
3116}
3117
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003118/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3119/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003120SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3121 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003122 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3123 EVT VT = Op.getValueType();
3124 unsigned VTBits = VT.getSizeInBits();
3125 DebugLoc dl = Op.getDebugLoc();
3126 SDValue ShOpLo = Op.getOperand(0);
3127 SDValue ShOpHi = Op.getOperand(1);
3128 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003129 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003130
3131 assert(Op.getOpcode() == ISD::SHL_PARTS);
3132 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3133 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3134 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3135 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3136 DAG.getConstant(VTBits, MVT::i32));
3137 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3138 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3139
3140 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3141 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3142 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003143 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003144 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003145 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003146 CCR, Cmp);
3147
3148 SDValue Ops[2] = { Lo, Hi };
3149 return DAG.getMergeValues(Ops, 2, dl);
3150}
3151
Jim Grosbach4725ca72010-09-08 03:54:02 +00003152SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003153 SelectionDAG &DAG) const {
3154 // The rounding mode is in bits 23:22 of the FPSCR.
3155 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3156 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3157 // so that the shift + and get folded into a bitfield extract.
3158 DebugLoc dl = Op.getDebugLoc();
3159 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3160 DAG.getConstant(Intrinsic::arm_get_fpscr,
3161 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003162 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003163 DAG.getConstant(1U << 22, MVT::i32));
3164 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3165 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003166 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003167 DAG.getConstant(3, MVT::i32));
3168}
3169
Jim Grosbach3482c802010-01-18 19:58:49 +00003170static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3171 const ARMSubtarget *ST) {
3172 EVT VT = N->getValueType(0);
3173 DebugLoc dl = N->getDebugLoc();
3174
3175 if (!ST->hasV6T2Ops())
3176 return SDValue();
3177
3178 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3179 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3180}
3181
Bob Wilson5bafff32009-06-22 23:27:02 +00003182static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3183 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003184 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003185 DebugLoc dl = N->getDebugLoc();
3186
Bob Wilsond5448bb2010-11-18 21:16:28 +00003187 if (!VT.isVector())
3188 return SDValue();
3189
Bob Wilson5bafff32009-06-22 23:27:02 +00003190 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003191 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003192
Bob Wilsond5448bb2010-11-18 21:16:28 +00003193 // Left shifts translate directly to the vshiftu intrinsic.
3194 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003195 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003196 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3197 N->getOperand(0), N->getOperand(1));
3198
3199 assert((N->getOpcode() == ISD::SRA ||
3200 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3201
3202 // NEON uses the same intrinsics for both left and right shifts. For
3203 // right shifts, the shift amounts are negative, so negate the vector of
3204 // shift amounts.
3205 EVT ShiftVT = N->getOperand(1).getValueType();
3206 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3207 getZeroVector(ShiftVT, DAG, dl),
3208 N->getOperand(1));
3209 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3210 Intrinsic::arm_neon_vshifts :
3211 Intrinsic::arm_neon_vshiftu);
3212 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3213 DAG.getConstant(vshiftInt, MVT::i32),
3214 N->getOperand(0), NegatedCount);
3215}
3216
3217static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3218 const ARMSubtarget *ST) {
3219 EVT VT = N->getValueType(0);
3220 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003221
Eli Friedmance392eb2009-08-22 03:13:10 +00003222 // We can get here for a node like i32 = ISD::SHL i32, i64
3223 if (VT != MVT::i64)
3224 return SDValue();
3225
3226 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003227 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003228
Chris Lattner27a6c732007-11-24 07:07:01 +00003229 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3230 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003231 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003232 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003233
Chris Lattner27a6c732007-11-24 07:07:01 +00003234 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003235 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003236
Chris Lattner27a6c732007-11-24 07:07:01 +00003237 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003238 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003239 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003240 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003241 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003242
Chris Lattner27a6c732007-11-24 07:07:01 +00003243 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3244 // captures the result into a carry flag.
3245 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003246 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003247
Chris Lattner27a6c732007-11-24 07:07:01 +00003248 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003249 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003250
Chris Lattner27a6c732007-11-24 07:07:01 +00003251 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003252 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003253}
3254
Bob Wilson5bafff32009-06-22 23:27:02 +00003255static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3256 SDValue TmpOp0, TmpOp1;
3257 bool Invert = false;
3258 bool Swap = false;
3259 unsigned Opc = 0;
3260
3261 SDValue Op0 = Op.getOperand(0);
3262 SDValue Op1 = Op.getOperand(1);
3263 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003264 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003265 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3266 DebugLoc dl = Op.getDebugLoc();
3267
3268 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3269 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003270 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003271 case ISD::SETUNE:
3272 case ISD::SETNE: Invert = true; // Fallthrough
3273 case ISD::SETOEQ:
3274 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3275 case ISD::SETOLT:
3276 case ISD::SETLT: Swap = true; // Fallthrough
3277 case ISD::SETOGT:
3278 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3279 case ISD::SETOLE:
3280 case ISD::SETLE: Swap = true; // Fallthrough
3281 case ISD::SETOGE:
3282 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3283 case ISD::SETUGE: Swap = true; // Fallthrough
3284 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3285 case ISD::SETUGT: Swap = true; // Fallthrough
3286 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3287 case ISD::SETUEQ: Invert = true; // Fallthrough
3288 case ISD::SETONE:
3289 // Expand this to (OLT | OGT).
3290 TmpOp0 = Op0;
3291 TmpOp1 = Op1;
3292 Opc = ISD::OR;
3293 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3294 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3295 break;
3296 case ISD::SETUO: Invert = true; // Fallthrough
3297 case ISD::SETO:
3298 // Expand this to (OLT | OGE).
3299 TmpOp0 = Op0;
3300 TmpOp1 = Op1;
3301 Opc = ISD::OR;
3302 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3303 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3304 break;
3305 }
3306 } else {
3307 // Integer comparisons.
3308 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003309 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003310 case ISD::SETNE: Invert = true;
3311 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3312 case ISD::SETLT: Swap = true;
3313 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3314 case ISD::SETLE: Swap = true;
3315 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3316 case ISD::SETULT: Swap = true;
3317 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3318 case ISD::SETULE: Swap = true;
3319 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3320 }
3321
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003322 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003323 if (Opc == ARMISD::VCEQ) {
3324
3325 SDValue AndOp;
3326 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3327 AndOp = Op0;
3328 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3329 AndOp = Op1;
3330
3331 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003332 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003333 AndOp = AndOp.getOperand(0);
3334
3335 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3336 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003337 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3338 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003339 Invert = !Invert;
3340 }
3341 }
3342 }
3343
3344 if (Swap)
3345 std::swap(Op0, Op1);
3346
Owen Andersonc24cb352010-11-08 23:21:22 +00003347 // If one of the operands is a constant vector zero, attempt to fold the
3348 // comparison to a specialized compare-against-zero form.
3349 SDValue SingleOp;
3350 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3351 SingleOp = Op0;
3352 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3353 if (Opc == ARMISD::VCGE)
3354 Opc = ARMISD::VCLEZ;
3355 else if (Opc == ARMISD::VCGT)
3356 Opc = ARMISD::VCLTZ;
3357 SingleOp = Op1;
3358 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003359
Owen Andersonc24cb352010-11-08 23:21:22 +00003360 SDValue Result;
3361 if (SingleOp.getNode()) {
3362 switch (Opc) {
3363 case ARMISD::VCEQ:
3364 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3365 case ARMISD::VCGE:
3366 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3367 case ARMISD::VCLEZ:
3368 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3369 case ARMISD::VCGT:
3370 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3371 case ARMISD::VCLTZ:
3372 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3373 default:
3374 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3375 }
3376 } else {
3377 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3378 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003379
3380 if (Invert)
3381 Result = DAG.getNOT(dl, Result, VT);
3382
3383 return Result;
3384}
3385
Bob Wilsond3c42842010-06-14 22:19:57 +00003386/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3387/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003388/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003389static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3390 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003391 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003392 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003393
Bob Wilson827b2102010-06-15 19:05:35 +00003394 // SplatBitSize is set to the smallest size that splats the vector, so a
3395 // zero vector will always have SplatBitSize == 8. However, NEON modified
3396 // immediate instructions others than VMOV do not support the 8-bit encoding
3397 // of a zero vector, and the default encoding of zero is supposed to be the
3398 // 32-bit version.
3399 if (SplatBits == 0)
3400 SplatBitSize = 32;
3401
Bob Wilson5bafff32009-06-22 23:27:02 +00003402 switch (SplatBitSize) {
3403 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003404 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003405 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003406 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003407 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003408 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003409 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003410 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003411 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003412
3413 case 16:
3414 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003415 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003416 if ((SplatBits & ~0xff) == 0) {
3417 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003418 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003419 Imm = SplatBits;
3420 break;
3421 }
3422 if ((SplatBits & ~0xff00) == 0) {
3423 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003424 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003425 Imm = SplatBits >> 8;
3426 break;
3427 }
3428 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003429
3430 case 32:
3431 // NEON's 32-bit VMOV supports splat values where:
3432 // * only one byte is nonzero, or
3433 // * the least significant byte is 0xff and the second byte is nonzero, or
3434 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003435 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003436 if ((SplatBits & ~0xff) == 0) {
3437 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003438 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003439 Imm = SplatBits;
3440 break;
3441 }
3442 if ((SplatBits & ~0xff00) == 0) {
3443 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003444 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003445 Imm = SplatBits >> 8;
3446 break;
3447 }
3448 if ((SplatBits & ~0xff0000) == 0) {
3449 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003450 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003451 Imm = SplatBits >> 16;
3452 break;
3453 }
3454 if ((SplatBits & ~0xff000000) == 0) {
3455 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003456 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003457 Imm = SplatBits >> 24;
3458 break;
3459 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003460
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003461 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3462 if (type == OtherModImm) return SDValue();
3463
Bob Wilson5bafff32009-06-22 23:27:02 +00003464 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003465 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3466 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003467 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003468 Imm = SplatBits >> 8;
3469 SplatBits |= 0xff;
3470 break;
3471 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003472
3473 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003474 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3475 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003476 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003477 Imm = SplatBits >> 16;
3478 SplatBits |= 0xffff;
3479 break;
3480 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003481
3482 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3483 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3484 // VMOV.I32. A (very) minor optimization would be to replicate the value
3485 // and fall through here to test for a valid 64-bit splat. But, then the
3486 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003487 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003488
3489 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003490 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003491 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003492 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003493 uint64_t BitMask = 0xff;
3494 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003495 unsigned ImmMask = 1;
3496 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003497 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003498 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003499 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003500 Imm |= ImmMask;
3501 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003502 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003503 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003504 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003505 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003506 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003507 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003508 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003509 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003510 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003511 break;
3512 }
3513
Bob Wilson1a913ed2010-06-11 21:34:50 +00003514 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003515 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003516 return SDValue();
3517 }
3518
Bob Wilsoncba270d2010-07-13 21:16:48 +00003519 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3520 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003521}
3522
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003523static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3524 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003525 unsigned NumElts = VT.getVectorNumElements();
3526 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003527
3528 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3529 if (M[0] < 0)
3530 return false;
3531
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003532 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003533
3534 // If this is a VEXT shuffle, the immediate value is the index of the first
3535 // element. The other shuffle indices must be the successive elements after
3536 // the first one.
3537 unsigned ExpectedElt = Imm;
3538 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003539 // Increment the expected index. If it wraps around, it may still be
3540 // a VEXT but the source vectors must be swapped.
3541 ExpectedElt += 1;
3542 if (ExpectedElt == NumElts * 2) {
3543 ExpectedElt = 0;
3544 ReverseVEXT = true;
3545 }
3546
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003547 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003548 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003549 return false;
3550 }
3551
3552 // Adjust the index value if the source operands will be swapped.
3553 if (ReverseVEXT)
3554 Imm -= NumElts;
3555
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003556 return true;
3557}
3558
Bob Wilson8bb9e482009-07-26 00:39:34 +00003559/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3560/// instruction with the specified blocksize. (The order of the elements
3561/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003562static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3563 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003564 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3565 "Only possible block sizes for VREV are: 16, 32, 64");
3566
Bob Wilson8bb9e482009-07-26 00:39:34 +00003567 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003568 if (EltSz == 64)
3569 return false;
3570
3571 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003572 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003573 // If the first shuffle index is UNDEF, be optimistic.
3574 if (M[0] < 0)
3575 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003576
3577 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3578 return false;
3579
3580 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003581 if (M[i] < 0) continue; // ignore UNDEF indices
3582 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003583 return false;
3584 }
3585
3586 return true;
3587}
3588
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003589static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3590 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3591 // range, then 0 is placed into the resulting vector. So pretty much any mask
3592 // of 8 elements can work here.
3593 return VT == MVT::v8i8 && M.size() == 8;
3594}
3595
Bob Wilsonc692cb72009-08-21 20:54:19 +00003596static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3597 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003598 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3599 if (EltSz == 64)
3600 return false;
3601
Bob Wilsonc692cb72009-08-21 20:54:19 +00003602 unsigned NumElts = VT.getVectorNumElements();
3603 WhichResult = (M[0] == 0 ? 0 : 1);
3604 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003605 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3606 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003607 return false;
3608 }
3609 return true;
3610}
3611
Bob Wilson324f4f12009-12-03 06:40:55 +00003612/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3613/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3614/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3615static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3616 unsigned &WhichResult) {
3617 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3618 if (EltSz == 64)
3619 return false;
3620
3621 unsigned NumElts = VT.getVectorNumElements();
3622 WhichResult = (M[0] == 0 ? 0 : 1);
3623 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003624 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3625 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003626 return false;
3627 }
3628 return true;
3629}
3630
Bob Wilsonc692cb72009-08-21 20:54:19 +00003631static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3632 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003633 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3634 if (EltSz == 64)
3635 return false;
3636
Bob Wilsonc692cb72009-08-21 20:54:19 +00003637 unsigned NumElts = VT.getVectorNumElements();
3638 WhichResult = (M[0] == 0 ? 0 : 1);
3639 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003640 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003641 if ((unsigned) M[i] != 2 * i + WhichResult)
3642 return false;
3643 }
3644
3645 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003646 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003647 return false;
3648
3649 return true;
3650}
3651
Bob Wilson324f4f12009-12-03 06:40:55 +00003652/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3653/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3654/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3655static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3656 unsigned &WhichResult) {
3657 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3658 if (EltSz == 64)
3659 return false;
3660
3661 unsigned Half = VT.getVectorNumElements() / 2;
3662 WhichResult = (M[0] == 0 ? 0 : 1);
3663 for (unsigned j = 0; j != 2; ++j) {
3664 unsigned Idx = WhichResult;
3665 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003666 int MIdx = M[i + j * Half];
3667 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003668 return false;
3669 Idx += 2;
3670 }
3671 }
3672
3673 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3674 if (VT.is64BitVector() && EltSz == 32)
3675 return false;
3676
3677 return true;
3678}
3679
Bob Wilsonc692cb72009-08-21 20:54:19 +00003680static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3681 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003682 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3683 if (EltSz == 64)
3684 return false;
3685
Bob Wilsonc692cb72009-08-21 20:54:19 +00003686 unsigned NumElts = VT.getVectorNumElements();
3687 WhichResult = (M[0] == 0 ? 0 : 1);
3688 unsigned Idx = WhichResult * NumElts / 2;
3689 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003690 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3691 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003692 return false;
3693 Idx += 1;
3694 }
3695
3696 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003697 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003698 return false;
3699
3700 return true;
3701}
3702
Bob Wilson324f4f12009-12-03 06:40:55 +00003703/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3704/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3705/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3706static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3707 unsigned &WhichResult) {
3708 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3709 if (EltSz == 64)
3710 return false;
3711
3712 unsigned NumElts = VT.getVectorNumElements();
3713 WhichResult = (M[0] == 0 ? 0 : 1);
3714 unsigned Idx = WhichResult * NumElts / 2;
3715 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003716 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3717 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003718 return false;
3719 Idx += 1;
3720 }
3721
3722 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3723 if (VT.is64BitVector() && EltSz == 32)
3724 return false;
3725
3726 return true;
3727}
3728
Dale Johannesenf630c712010-07-29 20:10:08 +00003729// If N is an integer constant that can be moved into a register in one
3730// instruction, return an SDValue of such a constant (will become a MOV
3731// instruction). Otherwise return null.
3732static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3733 const ARMSubtarget *ST, DebugLoc dl) {
3734 uint64_t Val;
3735 if (!isa<ConstantSDNode>(N))
3736 return SDValue();
3737 Val = cast<ConstantSDNode>(N)->getZExtValue();
3738
3739 if (ST->isThumb1Only()) {
3740 if (Val <= 255 || ~Val <= 255)
3741 return DAG.getConstant(Val, MVT::i32);
3742 } else {
3743 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3744 return DAG.getConstant(Val, MVT::i32);
3745 }
3746 return SDValue();
3747}
3748
Bob Wilson5bafff32009-06-22 23:27:02 +00003749// If this is a case we can't handle, return null and let the default
3750// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003751SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3752 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003753 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003754 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003755 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003756
3757 APInt SplatBits, SplatUndef;
3758 unsigned SplatBitSize;
3759 bool HasAnyUndefs;
3760 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003761 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003762 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003763 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003764 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003765 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003766 DAG, VmovVT, VT.is128BitVector(),
3767 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003768 if (Val.getNode()) {
3769 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003770 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003771 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003772
3773 // Try an immediate VMVN.
3774 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3775 ((1LL << SplatBitSize) - 1));
3776 Val = isNEONModifiedImm(NegatedImm,
3777 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003778 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003779 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003780 if (Val.getNode()) {
3781 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003782 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003783 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003784 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003785 }
3786
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003787 // Scan through the operands to see if only one value is used.
3788 unsigned NumElts = VT.getVectorNumElements();
3789 bool isOnlyLowElement = true;
3790 bool usesOnlyOneValue = true;
3791 bool isConstant = true;
3792 SDValue Value;
3793 for (unsigned i = 0; i < NumElts; ++i) {
3794 SDValue V = Op.getOperand(i);
3795 if (V.getOpcode() == ISD::UNDEF)
3796 continue;
3797 if (i > 0)
3798 isOnlyLowElement = false;
3799 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3800 isConstant = false;
3801
3802 if (!Value.getNode())
3803 Value = V;
3804 else if (V != Value)
3805 usesOnlyOneValue = false;
3806 }
3807
3808 if (!Value.getNode())
3809 return DAG.getUNDEF(VT);
3810
3811 if (isOnlyLowElement)
3812 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3813
Dale Johannesenf630c712010-07-29 20:10:08 +00003814 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3815
Dale Johannesen575cd142010-10-19 20:00:17 +00003816 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3817 // i32 and try again.
3818 if (usesOnlyOneValue && EltSize <= 32) {
3819 if (!isConstant)
3820 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3821 if (VT.getVectorElementType().isFloatingPoint()) {
3822 SmallVector<SDValue, 8> Ops;
3823 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003824 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003825 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003826 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3827 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003828 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3829 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003830 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003831 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003832 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3833 if (Val.getNode())
3834 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003835 }
3836
3837 // If all elements are constants and the case above didn't get hit, fall back
3838 // to the default expansion, which will generate a load from the constant
3839 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003840 if (isConstant)
3841 return SDValue();
3842
Bob Wilson11a1dff2011-01-07 21:37:30 +00003843 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
3844 if (NumElts >= 4) {
3845 SDValue shuffle = ReconstructShuffle(Op, DAG);
3846 if (shuffle != SDValue())
3847 return shuffle;
3848 }
3849
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003850 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003851 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
3852 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003853 if (EltSize >= 32) {
3854 // Do the expansion with floating-point types, since that is what the VFP
3855 // registers are defined to use, and since i64 is not legal.
3856 EVT EltVT = EVT::getFloatingPointVT(EltSize);
3857 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003858 SmallVector<SDValue, 8> Ops;
3859 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003860 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00003861 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003862 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00003863 }
3864
3865 return SDValue();
3866}
3867
Bob Wilson11a1dff2011-01-07 21:37:30 +00003868// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003869// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00003870SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
3871 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00003872 DebugLoc dl = Op.getDebugLoc();
3873 EVT VT = Op.getValueType();
3874 unsigned NumElts = VT.getVectorNumElements();
3875
3876 SmallVector<SDValue, 2> SourceVecs;
3877 SmallVector<unsigned, 2> MinElts;
3878 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003879
Bob Wilson11a1dff2011-01-07 21:37:30 +00003880 for (unsigned i = 0; i < NumElts; ++i) {
3881 SDValue V = Op.getOperand(i);
3882 if (V.getOpcode() == ISD::UNDEF)
3883 continue;
3884 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
3885 // A shuffle can only come from building a vector from various
3886 // elements of other vectors.
3887 return SDValue();
3888 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003889
Bob Wilson11a1dff2011-01-07 21:37:30 +00003890 // Record this extraction against the appropriate vector if possible...
3891 SDValue SourceVec = V.getOperand(0);
3892 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
3893 bool FoundSource = false;
3894 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
3895 if (SourceVecs[j] == SourceVec) {
3896 if (MinElts[j] > EltNo)
3897 MinElts[j] = EltNo;
3898 if (MaxElts[j] < EltNo)
3899 MaxElts[j] = EltNo;
3900 FoundSource = true;
3901 break;
3902 }
3903 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003904
Bob Wilson11a1dff2011-01-07 21:37:30 +00003905 // Or record a new source if not...
3906 if (!FoundSource) {
3907 SourceVecs.push_back(SourceVec);
3908 MinElts.push_back(EltNo);
3909 MaxElts.push_back(EltNo);
3910 }
3911 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003912
Bob Wilson11a1dff2011-01-07 21:37:30 +00003913 // Currently only do something sane when at most two source vectors
3914 // involved.
3915 if (SourceVecs.size() > 2)
3916 return SDValue();
3917
3918 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
3919 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003920
Bob Wilson11a1dff2011-01-07 21:37:30 +00003921 // This loop extracts the usage patterns of the source vectors
3922 // and prepares appropriate SDValues for a shuffle if possible.
3923 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
3924 if (SourceVecs[i].getValueType() == VT) {
3925 // No VEXT necessary
3926 ShuffleSrcs[i] = SourceVecs[i];
3927 VEXTOffsets[i] = 0;
3928 continue;
3929 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
3930 // It probably isn't worth padding out a smaller vector just to
3931 // break it down again in a shuffle.
3932 return SDValue();
3933 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003934
Bob Wilson11a1dff2011-01-07 21:37:30 +00003935 // Since only 64-bit and 128-bit vectors are legal on ARM and
3936 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00003937 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
3938 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003939
Bob Wilson11a1dff2011-01-07 21:37:30 +00003940 if (MaxElts[i] - MinElts[i] >= NumElts) {
3941 // Span too large for a VEXT to cope
3942 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003943 }
3944
Bob Wilson11a1dff2011-01-07 21:37:30 +00003945 if (MinElts[i] >= NumElts) {
3946 // The extraction can just take the second half
3947 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00003948 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3949 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003950 DAG.getIntPtrConstant(NumElts));
3951 } else if (MaxElts[i] < NumElts) {
3952 // The extraction can just take the first half
3953 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00003954 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3955 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003956 DAG.getIntPtrConstant(0));
3957 } else {
3958 // An actual VEXT is needed
3959 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00003960 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3961 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003962 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00003963 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
3964 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00003965 DAG.getIntPtrConstant(NumElts));
3966 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
3967 DAG.getConstant(VEXTOffsets[i], MVT::i32));
3968 }
3969 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003970
Bob Wilson11a1dff2011-01-07 21:37:30 +00003971 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003972
Bob Wilson11a1dff2011-01-07 21:37:30 +00003973 for (unsigned i = 0; i < NumElts; ++i) {
3974 SDValue Entry = Op.getOperand(i);
3975 if (Entry.getOpcode() == ISD::UNDEF) {
3976 Mask.push_back(-1);
3977 continue;
3978 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003979
Bob Wilson11a1dff2011-01-07 21:37:30 +00003980 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00003981 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
3982 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00003983 if (ExtractVec == SourceVecs[0]) {
3984 Mask.push_back(ExtractElt - VEXTOffsets[0]);
3985 } else {
3986 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
3987 }
3988 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003989
Bob Wilson11a1dff2011-01-07 21:37:30 +00003990 // Final check before we try to produce nonsense...
3991 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00003992 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
3993 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00003994
Bob Wilson11a1dff2011-01-07 21:37:30 +00003995 return SDValue();
3996}
3997
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003998/// isShuffleMaskLegal - Targets can use this to indicate that they only
3999/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4000/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4001/// are assumed to be legal.
4002bool
4003ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4004 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004005 if (VT.getVectorNumElements() == 4 &&
4006 (VT.is128BitVector() || VT.is64BitVector())) {
4007 unsigned PFIndexes[4];
4008 for (unsigned i = 0; i != 4; ++i) {
4009 if (M[i] < 0)
4010 PFIndexes[i] = 8;
4011 else
4012 PFIndexes[i] = M[i];
4013 }
4014
4015 // Compute the index in the perfect shuffle table.
4016 unsigned PFTableIndex =
4017 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4018 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4019 unsigned Cost = (PFEntry >> 30);
4020
4021 if (Cost <= 4)
4022 return true;
4023 }
4024
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004025 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004026 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004027
Bob Wilson53dd2452010-06-07 23:53:38 +00004028 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4029 return (EltSize >= 32 ||
4030 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004031 isVREVMask(M, VT, 64) ||
4032 isVREVMask(M, VT, 32) ||
4033 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004034 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004035 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004036 isVTRNMask(M, VT, WhichResult) ||
4037 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004038 isVZIPMask(M, VT, WhichResult) ||
4039 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4040 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4041 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004042}
4043
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004044/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4045/// the specified operations to build the shuffle.
4046static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4047 SDValue RHS, SelectionDAG &DAG,
4048 DebugLoc dl) {
4049 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4050 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4051 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4052
4053 enum {
4054 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4055 OP_VREV,
4056 OP_VDUP0,
4057 OP_VDUP1,
4058 OP_VDUP2,
4059 OP_VDUP3,
4060 OP_VEXT1,
4061 OP_VEXT2,
4062 OP_VEXT3,
4063 OP_VUZPL, // VUZP, left result
4064 OP_VUZPR, // VUZP, right result
4065 OP_VZIPL, // VZIP, left result
4066 OP_VZIPR, // VZIP, right result
4067 OP_VTRNL, // VTRN, left result
4068 OP_VTRNR // VTRN, right result
4069 };
4070
4071 if (OpNum == OP_COPY) {
4072 if (LHSID == (1*9+2)*9+3) return LHS;
4073 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4074 return RHS;
4075 }
4076
4077 SDValue OpLHS, OpRHS;
4078 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4079 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4080 EVT VT = OpLHS.getValueType();
4081
4082 switch (OpNum) {
4083 default: llvm_unreachable("Unknown shuffle opcode!");
4084 case OP_VREV:
4085 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4086 case OP_VDUP0:
4087 case OP_VDUP1:
4088 case OP_VDUP2:
4089 case OP_VDUP3:
4090 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004091 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004092 case OP_VEXT1:
4093 case OP_VEXT2:
4094 case OP_VEXT3:
4095 return DAG.getNode(ARMISD::VEXT, dl, VT,
4096 OpLHS, OpRHS,
4097 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4098 case OP_VUZPL:
4099 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004100 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004101 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4102 case OP_VZIPL:
4103 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004104 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004105 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4106 case OP_VTRNL:
4107 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004108 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4109 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004110 }
4111}
4112
Bill Wendling69a05a72011-03-14 23:02:38 +00004113static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4114 SmallVectorImpl<int> &ShuffleMask,
4115 SelectionDAG &DAG) {
4116 // Check to see if we can use the VTBL instruction.
4117 SDValue V1 = Op.getOperand(0);
4118 SDValue V2 = Op.getOperand(1);
4119 DebugLoc DL = Op.getDebugLoc();
4120
4121 SmallVector<SDValue, 8> VTBLMask;
4122 for (SmallVectorImpl<int>::iterator
4123 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4124 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4125
4126 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4127 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4128 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4129 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004130
4131 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
4132 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4133 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004134}
4135
Bob Wilson5bafff32009-06-22 23:27:02 +00004136static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004137 SDValue V1 = Op.getOperand(0);
4138 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004139 DebugLoc dl = Op.getDebugLoc();
4140 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004141 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004142 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004143
Bob Wilson28865062009-08-13 02:13:04 +00004144 // Convert shuffles that are directly supported on NEON to target-specific
4145 // DAG nodes, instead of keeping them as shuffles and matching them again
4146 // during code selection. This is more efficient and avoids the possibility
4147 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004148 // FIXME: floating-point vectors should be canonicalized to integer vectors
4149 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004150 SVN->getMask(ShuffleMask);
4151
Bob Wilson53dd2452010-06-07 23:53:38 +00004152 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4153 if (EltSize <= 32) {
4154 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4155 int Lane = SVN->getSplatIndex();
4156 // If this is undef splat, generate it via "just" vdup, if possible.
4157 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004158
Bob Wilson53dd2452010-06-07 23:53:38 +00004159 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4160 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4161 }
4162 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4163 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004164 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004165
4166 bool ReverseVEXT;
4167 unsigned Imm;
4168 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4169 if (ReverseVEXT)
4170 std::swap(V1, V2);
4171 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4172 DAG.getConstant(Imm, MVT::i32));
4173 }
4174
4175 if (isVREVMask(ShuffleMask, VT, 64))
4176 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4177 if (isVREVMask(ShuffleMask, VT, 32))
4178 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4179 if (isVREVMask(ShuffleMask, VT, 16))
4180 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4181
4182 // Check for Neon shuffles that modify both input vectors in place.
4183 // If both results are used, i.e., if there are two shuffles with the same
4184 // source operands and with masks corresponding to both results of one of
4185 // these operations, DAG memoization will ensure that a single node is
4186 // used for both shuffles.
4187 unsigned WhichResult;
4188 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4189 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4190 V1, V2).getValue(WhichResult);
4191 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4192 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4193 V1, V2).getValue(WhichResult);
4194 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4195 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4196 V1, V2).getValue(WhichResult);
4197
4198 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4199 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4200 V1, V1).getValue(WhichResult);
4201 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4202 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4203 V1, V1).getValue(WhichResult);
4204 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4205 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4206 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004207 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004208
Bob Wilsonc692cb72009-08-21 20:54:19 +00004209 // If the shuffle is not directly supported and it has 4 elements, use
4210 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004211 unsigned NumElts = VT.getVectorNumElements();
4212 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004213 unsigned PFIndexes[4];
4214 for (unsigned i = 0; i != 4; ++i) {
4215 if (ShuffleMask[i] < 0)
4216 PFIndexes[i] = 8;
4217 else
4218 PFIndexes[i] = ShuffleMask[i];
4219 }
4220
4221 // Compute the index in the perfect shuffle table.
4222 unsigned PFTableIndex =
4223 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004224 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4225 unsigned Cost = (PFEntry >> 30);
4226
4227 if (Cost <= 4)
4228 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4229 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004230
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004231 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004232 if (EltSize >= 32) {
4233 // Do the expansion with floating-point types, since that is what the VFP
4234 // registers are defined to use, and since i64 is not legal.
4235 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4236 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004237 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4238 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004239 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004240 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004241 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004242 Ops.push_back(DAG.getUNDEF(EltVT));
4243 else
4244 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4245 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4246 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4247 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004248 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004249 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004250 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004251 }
4252
Bill Wendling69a05a72011-03-14 23:02:38 +00004253 if (VT == MVT::v8i8) {
4254 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4255 if (NewOp.getNode())
4256 return NewOp;
4257 }
4258
Bob Wilson22cac0d2009-08-14 05:16:33 +00004259 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004260}
4261
Bob Wilson5bafff32009-06-22 23:27:02 +00004262static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004263 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004264 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004265 if (!isa<ConstantSDNode>(Lane))
4266 return SDValue();
4267
4268 SDValue Vec = Op.getOperand(0);
4269 if (Op.getValueType() == MVT::i32 &&
4270 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4271 DebugLoc dl = Op.getDebugLoc();
4272 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4273 }
4274
4275 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004276}
4277
Bob Wilsona6d65862009-08-03 20:36:38 +00004278static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4279 // The only time a CONCAT_VECTORS operation can have legal types is when
4280 // two 64-bit vectors are concatenated to a 128-bit vector.
4281 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4282 "unexpected CONCAT_VECTORS");
4283 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004284 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004285 SDValue Op0 = Op.getOperand(0);
4286 SDValue Op1 = Op.getOperand(1);
4287 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004288 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004289 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004290 DAG.getIntPtrConstant(0));
4291 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004292 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004293 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004294 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004295 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004296}
4297
Bob Wilson626613d2010-11-23 19:38:38 +00004298/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4299/// element has been zero/sign-extended, depending on the isSigned parameter,
4300/// from an integer type half its size.
4301static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4302 bool isSigned) {
4303 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4304 EVT VT = N->getValueType(0);
4305 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4306 SDNode *BVN = N->getOperand(0).getNode();
4307 if (BVN->getValueType(0) != MVT::v4i32 ||
4308 BVN->getOpcode() != ISD::BUILD_VECTOR)
4309 return false;
4310 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4311 unsigned HiElt = 1 - LoElt;
4312 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4313 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4314 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4315 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4316 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4317 return false;
4318 if (isSigned) {
4319 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4320 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4321 return true;
4322 } else {
4323 if (Hi0->isNullValue() && Hi1->isNullValue())
4324 return true;
4325 }
4326 return false;
4327 }
4328
4329 if (N->getOpcode() != ISD::BUILD_VECTOR)
4330 return false;
4331
4332 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4333 SDNode *Elt = N->getOperand(i).getNode();
4334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4335 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4336 unsigned HalfSize = EltSize / 2;
4337 if (isSigned) {
4338 int64_t SExtVal = C->getSExtValue();
4339 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4340 return false;
4341 } else {
4342 if ((C->getZExtValue() >> HalfSize) != 0)
4343 return false;
4344 }
4345 continue;
4346 }
4347 return false;
4348 }
4349
4350 return true;
4351}
4352
4353/// isSignExtended - Check if a node is a vector value that is sign-extended
4354/// or a constant BUILD_VECTOR with sign-extended elements.
4355static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4356 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4357 return true;
4358 if (isExtendedBUILD_VECTOR(N, DAG, true))
4359 return true;
4360 return false;
4361}
4362
4363/// isZeroExtended - Check if a node is a vector value that is zero-extended
4364/// or a constant BUILD_VECTOR with zero-extended elements.
4365static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4366 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4367 return true;
4368 if (isExtendedBUILD_VECTOR(N, DAG, false))
4369 return true;
4370 return false;
4371}
4372
4373/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4374/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004375static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4376 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4377 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004378 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4379 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4380 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4381 LD->isNonTemporal(), LD->getAlignment());
4382 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4383 // have been legalized as a BITCAST from v4i32.
4384 if (N->getOpcode() == ISD::BITCAST) {
4385 SDNode *BVN = N->getOperand(0).getNode();
4386 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4387 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4388 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4389 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4390 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4391 }
4392 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4393 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4394 EVT VT = N->getValueType(0);
4395 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4396 unsigned NumElts = VT.getVectorNumElements();
4397 MVT TruncVT = MVT::getIntegerVT(EltSize);
4398 SmallVector<SDValue, 8> Ops;
4399 for (unsigned i = 0; i != NumElts; ++i) {
4400 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4401 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004402 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004403 }
4404 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4405 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004406}
4407
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004408static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4409 unsigned Opcode = N->getOpcode();
4410 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4411 SDNode *N0 = N->getOperand(0).getNode();
4412 SDNode *N1 = N->getOperand(1).getNode();
4413 return N0->hasOneUse() && N1->hasOneUse() &&
4414 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4415 }
4416 return false;
4417}
4418
4419static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4420 unsigned Opcode = N->getOpcode();
4421 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4422 SDNode *N0 = N->getOperand(0).getNode();
4423 SDNode *N1 = N->getOperand(1).getNode();
4424 return N0->hasOneUse() && N1->hasOneUse() &&
4425 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4426 }
4427 return false;
4428}
4429
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004430static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4431 // Multiplications are only custom-lowered for 128-bit vectors so that
4432 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4433 EVT VT = Op.getValueType();
4434 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4435 SDNode *N0 = Op.getOperand(0).getNode();
4436 SDNode *N1 = Op.getOperand(1).getNode();
4437 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004438 bool isMLA = false;
4439 bool isN0SExt = isSignExtended(N0, DAG);
4440 bool isN1SExt = isSignExtended(N1, DAG);
4441 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004442 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004443 else {
4444 bool isN0ZExt = isZeroExtended(N0, DAG);
4445 bool isN1ZExt = isZeroExtended(N1, DAG);
4446 if (isN0ZExt && isN1ZExt)
4447 NewOpc = ARMISD::VMULLu;
4448 else if (isN1SExt || isN1ZExt) {
4449 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4450 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4451 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4452 NewOpc = ARMISD::VMULLs;
4453 isMLA = true;
4454 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4455 NewOpc = ARMISD::VMULLu;
4456 isMLA = true;
4457 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4458 std::swap(N0, N1);
4459 NewOpc = ARMISD::VMULLu;
4460 isMLA = true;
4461 }
4462 }
4463
4464 if (!NewOpc) {
4465 if (VT == MVT::v2i64)
4466 // Fall through to expand this. It is not legal.
4467 return SDValue();
4468 else
4469 // Other vector multiplications are legal.
4470 return Op;
4471 }
4472 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004473
4474 // Legalize to a VMULL instruction.
4475 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004476 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004477 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004478 if (!isMLA) {
4479 Op0 = SkipExtension(N0, DAG);
4480 assert(Op0.getValueType().is64BitVector() &&
4481 Op1.getValueType().is64BitVector() &&
4482 "unexpected types for extended operands to VMULL");
4483 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4484 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004485
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004486 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4487 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4488 // vmull q0, d4, d6
4489 // vmlal q0, d5, d6
4490 // is faster than
4491 // vaddl q0, d4, d5
4492 // vmovl q1, d6
4493 // vmul q0, q0, q1
4494 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4495 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4496 EVT Op1VT = Op1.getValueType();
4497 return DAG.getNode(N0->getOpcode(), DL, VT,
4498 DAG.getNode(NewOpc, DL, VT,
4499 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4500 DAG.getNode(NewOpc, DL, VT,
4501 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004502}
4503
Nate Begeman7973f352011-02-11 20:53:29 +00004504static SDValue
4505LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4506 // Convert to float
4507 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4508 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4509 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4510 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4511 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4512 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4513 // Get reciprocal estimate.
4514 // float4 recip = vrecpeq_f32(yf);
4515 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4516 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4517 // Because char has a smaller range than uchar, we can actually get away
4518 // without any newton steps. This requires that we use a weird bias
4519 // of 0xb000, however (again, this has been exhaustively tested).
4520 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4521 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4522 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4523 Y = DAG.getConstant(0xb000, MVT::i32);
4524 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4525 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4526 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4527 // Convert back to short.
4528 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4529 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4530 return X;
4531}
4532
4533static SDValue
4534LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4535 SDValue N2;
4536 // Convert to float.
4537 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4538 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4539 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4540 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4541 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4542 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4543
4544 // Use reciprocal estimate and one refinement step.
4545 // float4 recip = vrecpeq_f32(yf);
4546 // recip *= vrecpsq_f32(yf, recip);
4547 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4548 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
4549 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4550 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4551 N1, N2);
4552 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4553 // Because short has a smaller range than ushort, we can actually get away
4554 // with only a single newton step. This requires that we use a weird bias
4555 // of 89, however (again, this has been exhaustively tested).
4556 // float4 result = as_float4(as_int4(xf*recip) + 89);
4557 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4558 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4559 N1 = DAG.getConstant(89, MVT::i32);
4560 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4561 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4562 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4563 // Convert back to integer and return.
4564 // return vmovn_s32(vcvt_s32_f32(result));
4565 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4566 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4567 return N0;
4568}
4569
4570static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4571 EVT VT = Op.getValueType();
4572 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4573 "unexpected type for custom-lowering ISD::SDIV");
4574
4575 DebugLoc dl = Op.getDebugLoc();
4576 SDValue N0 = Op.getOperand(0);
4577 SDValue N1 = Op.getOperand(1);
4578 SDValue N2, N3;
4579
4580 if (VT == MVT::v8i8) {
4581 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4582 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
4583
4584 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4585 DAG.getIntPtrConstant(4));
4586 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4587 DAG.getIntPtrConstant(4));
4588 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4589 DAG.getIntPtrConstant(0));
4590 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4591 DAG.getIntPtrConstant(0));
4592
4593 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4594 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4595
4596 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4597 N0 = LowerCONCAT_VECTORS(N0, DAG);
4598
4599 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4600 return N0;
4601 }
4602 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4603}
4604
4605static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4606 EVT VT = Op.getValueType();
4607 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4608 "unexpected type for custom-lowering ISD::UDIV");
4609
4610 DebugLoc dl = Op.getDebugLoc();
4611 SDValue N0 = Op.getOperand(0);
4612 SDValue N1 = Op.getOperand(1);
4613 SDValue N2, N3;
4614
4615 if (VT == MVT::v8i8) {
4616 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4617 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
4618
4619 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4620 DAG.getIntPtrConstant(4));
4621 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4622 DAG.getIntPtrConstant(4));
4623 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4624 DAG.getIntPtrConstant(0));
4625 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4626 DAG.getIntPtrConstant(0));
4627
4628 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4629 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
4630
4631 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4632 N0 = LowerCONCAT_VECTORS(N0, DAG);
4633
4634 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
4635 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4636 N0);
4637 return N0;
4638 }
4639
4640 // v4i16 sdiv ... Convert to float.
4641 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4642 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4643 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4644 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4645 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4646 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
4647
4648 // Use reciprocal estimate and two refinement steps.
4649 // float4 recip = vrecpeq_f32(yf);
4650 // recip *= vrecpsq_f32(yf, recip);
4651 // recip *= vrecpsq_f32(yf, recip);
4652 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4653 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
4654 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4655 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4656 N1, N2);
4657 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4658 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
4659 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4660 N1, N2);
4661 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4662 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4663 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4664 // and that it will never cause us to return an answer too large).
4665 // float4 result = as_float4(as_int4(xf*recip) + 89);
4666 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4667 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4668 N1 = DAG.getConstant(2, MVT::i32);
4669 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4670 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4671 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4672 // Convert back to integer and return.
4673 // return vmovn_u32(vcvt_s32_f32(result));
4674 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4675 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4676 return N0;
4677}
4678
Dan Gohmand858e902010-04-17 15:26:15 +00004679SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004680 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004681 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004682 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004683 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004684 case ISD::GlobalAddress:
4685 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4686 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004687 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004688 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004689 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4690 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004691 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004692 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004693 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004694 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004695 case ISD::SINT_TO_FP:
4696 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4697 case ISD::FP_TO_SINT:
4698 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004699 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004700 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004701 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004702 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004703 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004704 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004705 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004706 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4707 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004708 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004709 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004710 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004711 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004712 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004713 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004714 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004715 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004716 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004717 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004718 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004719 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004720 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004721 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004722 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004723 case ISD::SDIV: return LowerSDIV(Op, DAG);
4724 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004725 }
Dan Gohman475871a2008-07-27 21:46:04 +00004726 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004727}
4728
Duncan Sands1607f052008-12-01 11:39:25 +00004729/// ReplaceNodeResults - Replace the results of node with an illegal result
4730/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004731void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4732 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004733 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004734 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004735 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004736 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004737 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004738 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004739 case ISD::BITCAST:
4740 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004741 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004742 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004743 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004744 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004745 break;
Duncan Sands1607f052008-12-01 11:39:25 +00004746 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00004747 if (Res.getNode())
4748 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00004749}
Chris Lattner27a6c732007-11-24 07:07:01 +00004750
Evan Chenga8e29892007-01-19 07:51:42 +00004751//===----------------------------------------------------------------------===//
4752// ARM Scheduler Hooks
4753//===----------------------------------------------------------------------===//
4754
4755MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004756ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
4757 MachineBasicBlock *BB,
4758 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00004759 unsigned dest = MI->getOperand(0).getReg();
4760 unsigned ptr = MI->getOperand(1).getReg();
4761 unsigned oldval = MI->getOperand(2).getReg();
4762 unsigned newval = MI->getOperand(3).getReg();
4763 unsigned scratch = BB->getParent()->getRegInfo()
4764 .createVirtualRegister(ARM::GPRRegisterClass);
4765 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4766 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004767 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00004768
4769 unsigned ldrOpc, strOpc;
4770 switch (Size) {
4771 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004772 case 1:
4773 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00004774 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004775 break;
4776 case 2:
4777 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4778 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4779 break;
4780 case 4:
4781 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4782 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4783 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00004784 }
4785
4786 MachineFunction *MF = BB->getParent();
4787 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4788 MachineFunction::iterator It = BB;
4789 ++It; // insert the new blocks after the current block
4790
4791 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4792 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
4793 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4794 MF->insert(It, loop1MBB);
4795 MF->insert(It, loop2MBB);
4796 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004797
4798 // Transfer the remainder of BB and its successor edges to exitMBB.
4799 exitMBB->splice(exitMBB->begin(), BB,
4800 llvm::next(MachineBasicBlock::iterator(MI)),
4801 BB->end());
4802 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004803
4804 // thisMBB:
4805 // ...
4806 // fallthrough --> loop1MBB
4807 BB->addSuccessor(loop1MBB);
4808
4809 // loop1MBB:
4810 // ldrex dest, [ptr]
4811 // cmp dest, oldval
4812 // bne exitMBB
4813 BB = loop1MBB;
4814 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004815 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004816 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004817 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4818 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004819 BB->addSuccessor(loop2MBB);
4820 BB->addSuccessor(exitMBB);
4821
4822 // loop2MBB:
4823 // strex scratch, newval, [ptr]
4824 // cmp scratch, #0
4825 // bne loop1MBB
4826 BB = loop2MBB;
4827 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
4828 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004829 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00004830 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004831 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4832 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004833 BB->addSuccessor(loop1MBB);
4834 BB->addSuccessor(exitMBB);
4835
4836 // exitMBB:
4837 // ...
4838 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00004839
Dan Gohman14152b42010-07-06 20:24:04 +00004840 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00004841
Jim Grosbach5278eb82009-12-11 01:42:04 +00004842 return BB;
4843}
4844
4845MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00004846ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
4847 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00004848 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
4849 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4850
4851 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004852 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004853 MachineFunction::iterator It = BB;
4854 ++It;
4855
4856 unsigned dest = MI->getOperand(0).getReg();
4857 unsigned ptr = MI->getOperand(1).getReg();
4858 unsigned incr = MI->getOperand(2).getReg();
4859 DebugLoc dl = MI->getDebugLoc();
Rafael Espindolafda60d32009-12-18 16:59:39 +00004860
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004861 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004862 unsigned ldrOpc, strOpc;
4863 switch (Size) {
4864 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004865 case 1:
4866 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00004867 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004868 break;
4869 case 2:
4870 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
4871 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
4872 break;
4873 case 4:
4874 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
4875 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
4876 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00004877 }
4878
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004879 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4880 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
4881 MF->insert(It, loopMBB);
4882 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004883
4884 // Transfer the remainder of BB and its successor edges to exitMBB.
4885 exitMBB->splice(exitMBB->begin(), BB,
4886 llvm::next(MachineBasicBlock::iterator(MI)),
4887 BB->end());
4888 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004889
Jim Grosbach867bbbf2010-01-15 00:22:18 +00004890 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Jim Grosbachc3c23542009-12-14 04:22:04 +00004891 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4892 unsigned scratch2 = (!BinOpcode) ? incr :
4893 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
4894
4895 // thisMBB:
4896 // ...
4897 // fallthrough --> loopMBB
4898 BB->addSuccessor(loopMBB);
4899
4900 // loopMBB:
4901 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004902 // <binop> scratch2, dest, incr
4903 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00004904 // cmp scratch, #0
4905 // bne- loopMBB
4906 // fallthrough --> exitMBB
4907 BB = loopMBB;
4908 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
Jim Grosbachc67b5562009-12-15 00:12:35 +00004909 if (BinOpcode) {
4910 // operand order needs to go the other way for NAND
4911 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
4912 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4913 addReg(incr).addReg(dest)).addReg(0);
4914 else
4915 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
4916 addReg(dest).addReg(incr)).addReg(0);
4917 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00004918
4919 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
4920 .addReg(ptr));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004921 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00004922 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004923 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
4924 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00004925
4926 BB->addSuccessor(loopMBB);
4927 BB->addSuccessor(exitMBB);
4928
4929 // exitMBB:
4930 // ...
4931 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00004932
Dan Gohman14152b42010-07-06 20:24:04 +00004933 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00004934
Jim Grosbachc3c23542009-12-14 04:22:04 +00004935 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00004936}
4937
Evan Cheng218977b2010-07-13 19:27:42 +00004938static
4939MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
4940 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
4941 E = MBB->succ_end(); I != E; ++I)
4942 if (*I != Succ)
4943 return *I;
4944 llvm_unreachable("Expecting a BB with two successors!");
4945}
4946
Jim Grosbache801dc42009-12-12 01:40:06 +00004947MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004948ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004949 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004950 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00004951 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004952 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00004953 switch (MI->getOpcode()) {
Evan Cheng86198642009-08-07 00:34:42 +00004954 default:
Jim Grosbach5278eb82009-12-11 01:42:04 +00004955 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00004956 llvm_unreachable("Unexpected instr type to insert");
Jim Grosbach5278eb82009-12-11 01:42:04 +00004957
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004958 case ARM::ATOMIC_LOAD_ADD_I8:
4959 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4960 case ARM::ATOMIC_LOAD_ADD_I16:
4961 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
4962 case ARM::ATOMIC_LOAD_ADD_I32:
4963 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004964
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004965 case ARM::ATOMIC_LOAD_AND_I8:
4966 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4967 case ARM::ATOMIC_LOAD_AND_I16:
4968 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
4969 case ARM::ATOMIC_LOAD_AND_I32:
4970 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004971
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004972 case ARM::ATOMIC_LOAD_OR_I8:
4973 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4974 case ARM::ATOMIC_LOAD_OR_I16:
4975 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
4976 case ARM::ATOMIC_LOAD_OR_I32:
4977 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00004978
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004979 case ARM::ATOMIC_LOAD_XOR_I8:
4980 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4981 case ARM::ATOMIC_LOAD_XOR_I16:
4982 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
4983 case ARM::ATOMIC_LOAD_XOR_I32:
4984 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004985
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004986 case ARM::ATOMIC_LOAD_NAND_I8:
4987 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4988 case ARM::ATOMIC_LOAD_NAND_I16:
4989 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
4990 case ARM::ATOMIC_LOAD_NAND_I32:
4991 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004992
Jim Grosbacha36c8f22009-12-14 20:14:59 +00004993 case ARM::ATOMIC_LOAD_SUB_I8:
4994 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4995 case ARM::ATOMIC_LOAD_SUB_I16:
4996 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
4997 case ARM::ATOMIC_LOAD_SUB_I32:
4998 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00004999
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005000 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5001 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5002 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005003
5004 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5005 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5006 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005007
Evan Cheng007ea272009-08-12 05:17:19 +00005008 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005009 // To "insert" a SELECT_CC instruction, we actually have to insert the
5010 // diamond control-flow pattern. The incoming instruction knows the
5011 // destination vreg to set, the condition code register to branch on, the
5012 // true/false values to select between, and a branch opcode to use.
5013 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005014 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005015 ++It;
5016
5017 // thisMBB:
5018 // ...
5019 // TrueVal = ...
5020 // cmpTY ccX, r1, r2
5021 // bCC copy1MBB
5022 // fallthrough --> copy0MBB
5023 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005024 MachineFunction *F = BB->getParent();
5025 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5026 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005027 F->insert(It, copy0MBB);
5028 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005029
5030 // Transfer the remainder of BB and its successor edges to sinkMBB.
5031 sinkMBB->splice(sinkMBB->begin(), BB,
5032 llvm::next(MachineBasicBlock::iterator(MI)),
5033 BB->end());
5034 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5035
Dan Gohman258c58c2010-07-06 15:49:48 +00005036 BB->addSuccessor(copy0MBB);
5037 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005038
Dan Gohman14152b42010-07-06 20:24:04 +00005039 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5040 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5041
Evan Chenga8e29892007-01-19 07:51:42 +00005042 // copy0MBB:
5043 // %FalseValue = ...
5044 // # fallthrough to sinkMBB
5045 BB = copy0MBB;
5046
5047 // Update machine-CFG edges
5048 BB->addSuccessor(sinkMBB);
5049
5050 // sinkMBB:
5051 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5052 // ...
5053 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005054 BuildMI(*BB, BB->begin(), dl,
5055 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005056 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5057 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5058
Dan Gohman14152b42010-07-06 20:24:04 +00005059 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005060 return BB;
5061 }
Evan Cheng86198642009-08-07 00:34:42 +00005062
Evan Cheng218977b2010-07-13 19:27:42 +00005063 case ARM::BCCi64:
5064 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005065 // If there is an unconditional branch to the other successor, remove it.
5066 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005067
Evan Cheng218977b2010-07-13 19:27:42 +00005068 // Compare both parts that make up the double comparison separately for
5069 // equality.
5070 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5071
5072 unsigned LHS1 = MI->getOperand(1).getReg();
5073 unsigned LHS2 = MI->getOperand(2).getReg();
5074 if (RHSisZero) {
5075 AddDefaultPred(BuildMI(BB, dl,
5076 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5077 .addReg(LHS1).addImm(0));
5078 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5079 .addReg(LHS2).addImm(0)
5080 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5081 } else {
5082 unsigned RHS1 = MI->getOperand(3).getReg();
5083 unsigned RHS2 = MI->getOperand(4).getReg();
5084 AddDefaultPred(BuildMI(BB, dl,
5085 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5086 .addReg(LHS1).addReg(RHS1));
5087 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5088 .addReg(LHS2).addReg(RHS2)
5089 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5090 }
5091
5092 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5093 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5094 if (MI->getOperand(0).getImm() == ARMCC::NE)
5095 std::swap(destMBB, exitMBB);
5096
5097 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5098 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
5099 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2B : ARM::B))
5100 .addMBB(exitMBB);
5101
5102 MI->eraseFromParent(); // The pseudo instruction is gone now.
5103 return BB;
5104 }
Evan Chenga8e29892007-01-19 07:51:42 +00005105 }
5106}
5107
5108//===----------------------------------------------------------------------===//
5109// ARM Optimization Hooks
5110//===----------------------------------------------------------------------===//
5111
Chris Lattnerd1980a52009-03-12 06:52:53 +00005112static
5113SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5114 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005115 SelectionDAG &DAG = DCI.DAG;
5116 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005117 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005118 unsigned Opc = N->getOpcode();
5119 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5120 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5121 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5122 ISD::CondCode CC = ISD::SETCC_INVALID;
5123
5124 if (isSlctCC) {
5125 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5126 } else {
5127 SDValue CCOp = Slct.getOperand(0);
5128 if (CCOp.getOpcode() == ISD::SETCC)
5129 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5130 }
5131
5132 bool DoXform = false;
5133 bool InvCC = false;
5134 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5135 "Bad input!");
5136
5137 if (LHS.getOpcode() == ISD::Constant &&
5138 cast<ConstantSDNode>(LHS)->isNullValue()) {
5139 DoXform = true;
5140 } else if (CC != ISD::SETCC_INVALID &&
5141 RHS.getOpcode() == ISD::Constant &&
5142 cast<ConstantSDNode>(RHS)->isNullValue()) {
5143 std::swap(LHS, RHS);
5144 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005145 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005146 Op0.getOperand(0).getValueType();
5147 bool isInt = OpVT.isInteger();
5148 CC = ISD::getSetCCInverse(CC, isInt);
5149
5150 if (!TLI.isCondCodeLegal(CC, OpVT))
5151 return SDValue(); // Inverse operator isn't legal.
5152
5153 DoXform = true;
5154 InvCC = true;
5155 }
5156
5157 if (DoXform) {
5158 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5159 if (isSlctCC)
5160 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5161 Slct.getOperand(0), Slct.getOperand(1), CC);
5162 SDValue CCOp = Slct.getOperand(0);
5163 if (InvCC)
5164 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5165 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5166 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5167 CCOp, OtherOp, Result);
5168 }
5169 return SDValue();
5170}
5171
Bob Wilson3d5792a2010-07-29 20:34:14 +00005172/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5173/// operands N0 and N1. This is a helper for PerformADDCombine that is
5174/// called with the default operands, and if that fails, with commuted
5175/// operands.
5176static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5177 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005178 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5179 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5180 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5181 if (Result.getNode()) return Result;
5182 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005183 return SDValue();
5184}
5185
Bob Wilson3d5792a2010-07-29 20:34:14 +00005186/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5187///
5188static SDValue PerformADDCombine(SDNode *N,
5189 TargetLowering::DAGCombinerInfo &DCI) {
5190 SDValue N0 = N->getOperand(0);
5191 SDValue N1 = N->getOperand(1);
5192
5193 // First try with the default operand order.
5194 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI);
5195 if (Result.getNode())
5196 return Result;
5197
5198 // If that didn't work, try again with the operands commuted.
5199 return PerformADDCombineWithOperands(N, N1, N0, DCI);
5200}
5201
Chris Lattnerd1980a52009-03-12 06:52:53 +00005202/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005203///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005204static SDValue PerformSUBCombine(SDNode *N,
5205 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005206 SDValue N0 = N->getOperand(0);
5207 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005208
Chris Lattnerd1980a52009-03-12 06:52:53 +00005209 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5210 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5211 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5212 if (Result.getNode()) return Result;
5213 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005214
Chris Lattnerd1980a52009-03-12 06:52:53 +00005215 return SDValue();
5216}
5217
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005218static SDValue PerformMULCombine(SDNode *N,
5219 TargetLowering::DAGCombinerInfo &DCI,
5220 const ARMSubtarget *Subtarget) {
5221 SelectionDAG &DAG = DCI.DAG;
5222
5223 if (Subtarget->isThumb1Only())
5224 return SDValue();
5225
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005226 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5227 return SDValue();
5228
5229 EVT VT = N->getValueType(0);
5230 if (VT != MVT::i32)
5231 return SDValue();
5232
5233 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
5234 if (!C)
5235 return SDValue();
5236
5237 uint64_t MulAmt = C->getZExtValue();
5238 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
5239 ShiftAmt = ShiftAmt & (32 - 1);
5240 SDValue V = N->getOperand(0);
5241 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005242
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005243 SDValue Res;
5244 MulAmt >>= ShiftAmt;
5245 if (isPowerOf2_32(MulAmt - 1)) {
5246 // (mul x, 2^N + 1) => (add (shl x, N), x)
5247 Res = DAG.getNode(ISD::ADD, DL, VT,
5248 V, DAG.getNode(ISD::SHL, DL, VT,
5249 V, DAG.getConstant(Log2_32(MulAmt-1),
5250 MVT::i32)));
5251 } else if (isPowerOf2_32(MulAmt + 1)) {
5252 // (mul x, 2^N - 1) => (sub (shl x, N), x)
5253 Res = DAG.getNode(ISD::SUB, DL, VT,
5254 DAG.getNode(ISD::SHL, DL, VT,
5255 V, DAG.getConstant(Log2_32(MulAmt+1),
5256 MVT::i32)),
5257 V);
5258 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005259 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005260
5261 if (ShiftAmt != 0)
5262 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
5263 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005264
5265 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00005266 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00005267 return SDValue();
5268}
5269
Owen Anderson080c0922010-11-05 19:27:46 +00005270static SDValue PerformANDCombine(SDNode *N,
5271 TargetLowering::DAGCombinerInfo &DCI) {
Eric Christopher29aeed12011-03-26 01:21:03 +00005272
Owen Anderson080c0922010-11-05 19:27:46 +00005273 // Attempt to use immediate-form VBIC
5274 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5275 DebugLoc dl = N->getDebugLoc();
5276 EVT VT = N->getValueType(0);
5277 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005278
Owen Anderson080c0922010-11-05 19:27:46 +00005279 APInt SplatBits, SplatUndef;
5280 unsigned SplatBitSize;
5281 bool HasAnyUndefs;
5282 if (BVN &&
5283 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5284 if (SplatBitSize <= 64) {
5285 EVT VbicVT;
5286 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
5287 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005288 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005289 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00005290 if (Val.getNode()) {
5291 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005292 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00005293 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005294 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00005295 }
5296 }
5297 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005298
Owen Anderson080c0922010-11-05 19:27:46 +00005299 return SDValue();
5300}
5301
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005302/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
5303static SDValue PerformORCombine(SDNode *N,
5304 TargetLowering::DAGCombinerInfo &DCI,
5305 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00005306 // Attempt to use immediate-form VORR
5307 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
5308 DebugLoc dl = N->getDebugLoc();
5309 EVT VT = N->getValueType(0);
5310 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005311
Owen Anderson60f48702010-11-03 23:15:26 +00005312 APInt SplatBits, SplatUndef;
5313 unsigned SplatBitSize;
5314 bool HasAnyUndefs;
5315 if (BVN && Subtarget->hasNEON() &&
5316 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
5317 if (SplatBitSize <= 64) {
5318 EVT VorrVT;
5319 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
5320 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00005321 DAG, VorrVT, VT.is128BitVector(),
5322 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00005323 if (Val.getNode()) {
5324 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005325 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00005326 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005327 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00005328 }
5329 }
5330 }
5331
Jim Grosbach54238562010-07-17 03:30:54 +00005332 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
5333 // reasonable.
5334
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005335 // BFI is only available on V6T2+
5336 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
5337 return SDValue();
5338
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005339 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Jim Grosbach54238562010-07-17 03:30:54 +00005340 DebugLoc DL = N->getDebugLoc();
5341 // 1) or (and A, mask), val => ARMbfi A, val, mask
5342 // iff (val & mask) == val
5343 //
5344 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
5345 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005346 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005347 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00005348 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00005349 // (i.e., copy a bitfield value into another bitfield of the same width)
5350 if (N0.getOpcode() != ISD::AND)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005351 return SDValue();
5352
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005353 if (VT != MVT::i32)
5354 return SDValue();
5355
Evan Cheng30fb13f2010-12-13 20:32:54 +00005356 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00005357
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005358 // The value and the mask need to be constants so we can verify this is
5359 // actually a bitfield set. If the mask is 0xffff, we can do better
5360 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00005361 SDValue MaskOp = N0.getOperand(1);
5362 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
5363 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005364 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005365 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005366 if (Mask == 0xffff)
5367 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005368 SDValue Res;
5369 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005370 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
5371 if (N1C) {
5372 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005373 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00005374 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005375
Evan Chenga9688c42010-12-11 04:11:38 +00005376 if (ARM::isBitFieldInvertedMask(Mask)) {
5377 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005378
Evan Cheng30fb13f2010-12-13 20:32:54 +00005379 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00005380 DAG.getConstant(Val, MVT::i32),
5381 DAG.getConstant(Mask, MVT::i32));
5382
5383 // Do not add new nodes to DAG combiner worklist.
5384 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005385 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00005386 }
Jim Grosbach54238562010-07-17 03:30:54 +00005387 } else if (N1.getOpcode() == ISD::AND) {
5388 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00005389 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5390 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00005391 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00005392 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005393
Eric Christopher29aeed12011-03-26 01:21:03 +00005394 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
5395 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00005396 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005397 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005398 // The pack halfword instruction works better for masks that fit it,
5399 // so use that when it's available.
5400 if (Subtarget->hasT2ExtractPack() &&
5401 (Mask == 0xffff || Mask == 0xffff0000))
5402 return SDValue();
5403 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00005404 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00005405 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00005406 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00005407 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00005408 DAG.getConstant(Mask, MVT::i32));
5409 // Do not add new nodes to DAG combiner worklist.
5410 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005411 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005412 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00005413 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00005414 // The pack halfword instruction works better for masks that fit it,
5415 // so use that when it's available.
5416 if (Subtarget->hasT2ExtractPack() &&
5417 (Mask2 == 0xffff || Mask2 == 0xffff0000))
5418 return SDValue();
5419 // 2b
5420 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005421 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00005422 DAG.getConstant(lsb, MVT::i32));
5423 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00005424 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00005425 // Do not add new nodes to DAG combiner worklist.
5426 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00005427 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00005428 }
5429 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005430
Evan Cheng30fb13f2010-12-13 20:32:54 +00005431 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
5432 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
5433 ARM::isBitFieldInvertedMask(~Mask)) {
5434 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
5435 // where lsb(mask) == #shamt and masked bits of B are known zero.
5436 SDValue ShAmt = N00.getOperand(1);
5437 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
5438 unsigned LSB = CountTrailingZeros_32(Mask);
5439 if (ShAmtC != LSB)
5440 return SDValue();
5441
5442 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
5443 DAG.getConstant(~Mask, MVT::i32));
5444
5445 // Do not add new nodes to DAG combiner worklist.
5446 DCI.CombineTo(N, Res, false);
5447 }
5448
Jim Grosbach469bbdb2010-07-16 23:05:05 +00005449 return SDValue();
5450}
5451
Evan Cheng0c1aec12010-12-14 03:22:07 +00005452/// PerformBFICombine - (bfi A, (and B, C1), C2) -> (bfi A, B, C2) iff
5453/// C1 & C2 == C1.
5454static SDValue PerformBFICombine(SDNode *N,
5455 TargetLowering::DAGCombinerInfo &DCI) {
5456 SDValue N1 = N->getOperand(1);
5457 if (N1.getOpcode() == ISD::AND) {
5458 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
5459 if (!N11C)
5460 return SDValue();
5461 unsigned Mask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
5462 unsigned Mask2 = N11C->getZExtValue();
5463 if ((Mask & Mask2) == Mask2)
5464 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
5465 N->getOperand(0), N1.getOperand(0),
5466 N->getOperand(2));
5467 }
5468 return SDValue();
5469}
5470
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005471/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
5472/// ARMISD::VMOVRRD.
5473static SDValue PerformVMOVRRDCombine(SDNode *N,
5474 TargetLowering::DAGCombinerInfo &DCI) {
5475 // vmovrrd(vmovdrr x, y) -> x,y
5476 SDValue InDouble = N->getOperand(0);
5477 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
5478 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
5479 return SDValue();
5480}
5481
5482/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
5483/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
5484static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
5485 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
5486 SDValue Op0 = N->getOperand(0);
5487 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005488 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005489 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005490 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005491 Op1 = Op1.getOperand(0);
5492 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
5493 Op0.getNode() == Op1.getNode() &&
5494 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005495 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00005496 N->getValueType(0), Op0.getOperand(0));
5497 return SDValue();
5498}
5499
Bob Wilson31600902010-12-21 06:43:19 +00005500/// PerformSTORECombine - Target-specific dag combine xforms for
5501/// ISD::STORE.
5502static SDValue PerformSTORECombine(SDNode *N,
5503 TargetLowering::DAGCombinerInfo &DCI) {
5504 // Bitcast an i64 store extracted from a vector to f64.
5505 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5506 StoreSDNode *St = cast<StoreSDNode>(N);
5507 SDValue StVal = St->getValue();
5508 if (!ISD::isNormalStore(St) || St->isVolatile() ||
5509 StVal.getValueType() != MVT::i64 ||
5510 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5511 return SDValue();
5512
5513 SelectionDAG &DAG = DCI.DAG;
5514 DebugLoc dl = StVal.getDebugLoc();
5515 SDValue IntVec = StVal.getOperand(0);
5516 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5517 IntVec.getValueType().getVectorNumElements());
5518 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
5519 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5520 Vec, StVal.getOperand(1));
5521 dl = N->getDebugLoc();
5522 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
5523 // Make the DAGCombiner fold the bitcasts.
5524 DCI.AddToWorklist(Vec.getNode());
5525 DCI.AddToWorklist(ExtElt.getNode());
5526 DCI.AddToWorklist(V.getNode());
5527 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
5528 St->getPointerInfo(), St->isVolatile(),
5529 St->isNonTemporal(), St->getAlignment(),
5530 St->getTBAAInfo());
5531}
5532
5533/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
5534/// are normal, non-volatile loads. If so, it is profitable to bitcast an
5535/// i64 vector to have f64 elements, since the value can then be loaded
5536/// directly into a VFP register.
5537static bool hasNormalLoadOperand(SDNode *N) {
5538 unsigned NumElts = N->getValueType(0).getVectorNumElements();
5539 for (unsigned i = 0; i < NumElts; ++i) {
5540 SDNode *Elt = N->getOperand(i).getNode();
5541 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
5542 return true;
5543 }
5544 return false;
5545}
5546
Bob Wilson75f02882010-09-17 22:59:05 +00005547/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
5548/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00005549static SDValue PerformBUILD_VECTORCombine(SDNode *N,
5550 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00005551 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
5552 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
5553 // into a pair of GPRs, which is fine when the value is used as a scalar,
5554 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00005555 SelectionDAG &DAG = DCI.DAG;
5556 if (N->getNumOperands() == 2) {
5557 SDValue RV = PerformVMOVDRRCombine(N, DAG);
5558 if (RV.getNode())
5559 return RV;
5560 }
Bob Wilson75f02882010-09-17 22:59:05 +00005561
Bob Wilson31600902010-12-21 06:43:19 +00005562 // Load i64 elements as f64 values so that type legalization does not split
5563 // them up into i32 values.
5564 EVT VT = N->getValueType(0);
5565 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
5566 return SDValue();
5567 DebugLoc dl = N->getDebugLoc();
5568 SmallVector<SDValue, 8> Ops;
5569 unsigned NumElts = VT.getVectorNumElements();
5570 for (unsigned i = 0; i < NumElts; ++i) {
5571 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
5572 Ops.push_back(V);
5573 // Make the DAGCombiner fold the bitcast.
5574 DCI.AddToWorklist(V.getNode());
5575 }
5576 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
5577 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
5578 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
5579}
5580
5581/// PerformInsertEltCombine - Target-specific dag combine xforms for
5582/// ISD::INSERT_VECTOR_ELT.
5583static SDValue PerformInsertEltCombine(SDNode *N,
5584 TargetLowering::DAGCombinerInfo &DCI) {
5585 // Bitcast an i64 load inserted into a vector to f64.
5586 // Otherwise, the i64 value will be legalized to a pair of i32 values.
5587 EVT VT = N->getValueType(0);
5588 SDNode *Elt = N->getOperand(1).getNode();
5589 if (VT.getVectorElementType() != MVT::i64 ||
5590 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
5591 return SDValue();
5592
5593 SelectionDAG &DAG = DCI.DAG;
5594 DebugLoc dl = N->getDebugLoc();
5595 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
5596 VT.getVectorNumElements());
5597 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
5598 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
5599 // Make the DAGCombiner fold the bitcasts.
5600 DCI.AddToWorklist(Vec.getNode());
5601 DCI.AddToWorklist(V.getNode());
5602 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
5603 Vec, V, N->getOperand(2));
5604 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00005605}
5606
Bob Wilsonf20700c2010-10-27 20:38:28 +00005607/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
5608/// ISD::VECTOR_SHUFFLE.
5609static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
5610 // The LLVM shufflevector instruction does not require the shuffle mask
5611 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
5612 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
5613 // operands do not match the mask length, they are extended by concatenating
5614 // them with undef vectors. That is probably the right thing for other
5615 // targets, but for NEON it is better to concatenate two double-register
5616 // size vector operands into a single quad-register size vector. Do that
5617 // transformation here:
5618 // shuffle(concat(v1, undef), concat(v2, undef)) ->
5619 // shuffle(concat(v1, v2), undef)
5620 SDValue Op0 = N->getOperand(0);
5621 SDValue Op1 = N->getOperand(1);
5622 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
5623 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
5624 Op0.getNumOperands() != 2 ||
5625 Op1.getNumOperands() != 2)
5626 return SDValue();
5627 SDValue Concat0Op1 = Op0.getOperand(1);
5628 SDValue Concat1Op1 = Op1.getOperand(1);
5629 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
5630 Concat1Op1.getOpcode() != ISD::UNDEF)
5631 return SDValue();
5632 // Skip the transformation if any of the types are illegal.
5633 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5634 EVT VT = N->getValueType(0);
5635 if (!TLI.isTypeLegal(VT) ||
5636 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
5637 !TLI.isTypeLegal(Concat1Op1.getValueType()))
5638 return SDValue();
5639
5640 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5641 Op0.getOperand(0), Op1.getOperand(0));
5642 // Translate the shuffle mask.
5643 SmallVector<int, 16> NewMask;
5644 unsigned NumElts = VT.getVectorNumElements();
5645 unsigned HalfElts = NumElts/2;
5646 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
5647 for (unsigned n = 0; n < NumElts; ++n) {
5648 int MaskElt = SVN->getMaskElt(n);
5649 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005650 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00005651 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00005652 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00005653 NewElt = HalfElts + MaskElt - NumElts;
5654 NewMask.push_back(NewElt);
5655 }
5656 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
5657 DAG.getUNDEF(VT), NewMask.data());
5658}
5659
Bob Wilson1c3ef902011-02-07 17:43:21 +00005660/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
5661/// NEON load/store intrinsics to merge base address updates.
5662static SDValue CombineBaseUpdate(SDNode *N,
5663 TargetLowering::DAGCombinerInfo &DCI) {
5664 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
5665 return SDValue();
5666
5667 SelectionDAG &DAG = DCI.DAG;
5668 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
5669 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
5670 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
5671 SDValue Addr = N->getOperand(AddrOpIdx);
5672
5673 // Search for a use of the address operand that is an increment.
5674 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
5675 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
5676 SDNode *User = *UI;
5677 if (User->getOpcode() != ISD::ADD ||
5678 UI.getUse().getResNo() != Addr.getResNo())
5679 continue;
5680
5681 // Check that the add is independent of the load/store. Otherwise, folding
5682 // it would create a cycle.
5683 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
5684 continue;
5685
5686 // Find the new opcode for the updating load/store.
5687 bool isLoad = true;
5688 bool isLaneOp = false;
5689 unsigned NewOpc = 0;
5690 unsigned NumVecs = 0;
5691 if (isIntrinsic) {
5692 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
5693 switch (IntNo) {
5694 default: assert(0 && "unexpected intrinsic for Neon base update");
5695 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
5696 NumVecs = 1; break;
5697 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
5698 NumVecs = 2; break;
5699 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
5700 NumVecs = 3; break;
5701 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
5702 NumVecs = 4; break;
5703 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
5704 NumVecs = 2; isLaneOp = true; break;
5705 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
5706 NumVecs = 3; isLaneOp = true; break;
5707 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
5708 NumVecs = 4; isLaneOp = true; break;
5709 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
5710 NumVecs = 1; isLoad = false; break;
5711 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
5712 NumVecs = 2; isLoad = false; break;
5713 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
5714 NumVecs = 3; isLoad = false; break;
5715 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
5716 NumVecs = 4; isLoad = false; break;
5717 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
5718 NumVecs = 2; isLoad = false; isLaneOp = true; break;
5719 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
5720 NumVecs = 3; isLoad = false; isLaneOp = true; break;
5721 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
5722 NumVecs = 4; isLoad = false; isLaneOp = true; break;
5723 }
5724 } else {
5725 isLaneOp = true;
5726 switch (N->getOpcode()) {
5727 default: assert(0 && "unexpected opcode for Neon base update");
5728 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
5729 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
5730 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
5731 }
5732 }
5733
5734 // Find the size of memory referenced by the load/store.
5735 EVT VecTy;
5736 if (isLoad)
5737 VecTy = N->getValueType(0);
5738 else
5739 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
5740 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
5741 if (isLaneOp)
5742 NumBytes /= VecTy.getVectorNumElements();
5743
5744 // If the increment is a constant, it must match the memory ref size.
5745 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
5746 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
5747 uint64_t IncVal = CInc->getZExtValue();
5748 if (IncVal != NumBytes)
5749 continue;
5750 } else if (NumBytes >= 3 * 16) {
5751 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
5752 // separate instructions that make it harder to use a non-constant update.
5753 continue;
5754 }
5755
5756 // Create the new updating load/store node.
5757 EVT Tys[6];
5758 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
5759 unsigned n;
5760 for (n = 0; n < NumResultVecs; ++n)
5761 Tys[n] = VecTy;
5762 Tys[n++] = MVT::i32;
5763 Tys[n] = MVT::Other;
5764 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
5765 SmallVector<SDValue, 8> Ops;
5766 Ops.push_back(N->getOperand(0)); // incoming chain
5767 Ops.push_back(N->getOperand(AddrOpIdx));
5768 Ops.push_back(Inc);
5769 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
5770 Ops.push_back(N->getOperand(i));
5771 }
5772 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
5773 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
5774 Ops.data(), Ops.size(),
5775 MemInt->getMemoryVT(),
5776 MemInt->getMemOperand());
5777
5778 // Update the uses.
5779 std::vector<SDValue> NewResults;
5780 for (unsigned i = 0; i < NumResultVecs; ++i) {
5781 NewResults.push_back(SDValue(UpdN.getNode(), i));
5782 }
5783 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
5784 DCI.CombineTo(N, NewResults);
5785 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
5786
5787 break;
5788 }
5789 return SDValue();
5790}
5791
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005792/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
5793/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
5794/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
5795/// return true.
5796static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5797 SelectionDAG &DAG = DCI.DAG;
5798 EVT VT = N->getValueType(0);
5799 // vldN-dup instructions only support 64-bit vectors for N > 1.
5800 if (!VT.is64BitVector())
5801 return false;
5802
5803 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
5804 SDNode *VLD = N->getOperand(0).getNode();
5805 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
5806 return false;
5807 unsigned NumVecs = 0;
5808 unsigned NewOpc = 0;
5809 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
5810 if (IntNo == Intrinsic::arm_neon_vld2lane) {
5811 NumVecs = 2;
5812 NewOpc = ARMISD::VLD2DUP;
5813 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
5814 NumVecs = 3;
5815 NewOpc = ARMISD::VLD3DUP;
5816 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
5817 NumVecs = 4;
5818 NewOpc = ARMISD::VLD4DUP;
5819 } else {
5820 return false;
5821 }
5822
5823 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
5824 // numbers match the load.
5825 unsigned VLDLaneNo =
5826 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
5827 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5828 UI != UE; ++UI) {
5829 // Ignore uses of the chain result.
5830 if (UI.getUse().getResNo() == NumVecs)
5831 continue;
5832 SDNode *User = *UI;
5833 if (User->getOpcode() != ARMISD::VDUPLANE ||
5834 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
5835 return false;
5836 }
5837
5838 // Create the vldN-dup node.
5839 EVT Tys[5];
5840 unsigned n;
5841 for (n = 0; n < NumVecs; ++n)
5842 Tys[n] = VT;
5843 Tys[n] = MVT::Other;
5844 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
5845 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
5846 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
5847 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
5848 Ops, 2, VLDMemInt->getMemoryVT(),
5849 VLDMemInt->getMemOperand());
5850
5851 // Update the uses.
5852 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
5853 UI != UE; ++UI) {
5854 unsigned ResNo = UI.getUse().getResNo();
5855 // Ignore uses of the chain result.
5856 if (ResNo == NumVecs)
5857 continue;
5858 SDNode *User = *UI;
5859 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
5860 }
5861
5862 // Now the vldN-lane intrinsic is dead except for its chain result.
5863 // Update uses of the chain.
5864 std::vector<SDValue> VLDDupResults;
5865 for (unsigned n = 0; n < NumVecs; ++n)
5866 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
5867 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
5868 DCI.CombineTo(VLD, VLDDupResults);
5869
5870 return true;
5871}
5872
Bob Wilson9e82bf12010-07-14 01:22:12 +00005873/// PerformVDUPLANECombine - Target-specific dag combine xforms for
5874/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005875static SDValue PerformVDUPLANECombine(SDNode *N,
5876 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00005877 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005878
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005879 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
5880 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
5881 if (CombineVLDDUP(N, DCI))
5882 return SDValue(N, 0);
5883
5884 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
5885 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005886 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005887 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00005888 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00005889 return SDValue();
5890
5891 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
5892 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
5893 // The canonical VMOV for a zero vector uses a 32-bit element size.
5894 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5895 unsigned EltBits;
5896 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
5897 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005898 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005899 if (EltSize > VT.getVectorElementType().getSizeInBits())
5900 return SDValue();
5901
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00005902 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00005903}
5904
Bob Wilson5bafff32009-06-22 23:27:02 +00005905/// getVShiftImm - Check if this is a valid build_vector for the immediate
5906/// operand of a vector shift operation, where all the elements of the
5907/// build_vector must have the same constant integer value.
5908static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
5909 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005910 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00005911 Op = Op.getOperand(0);
5912 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5913 APInt SplatBits, SplatUndef;
5914 unsigned SplatBitSize;
5915 bool HasAnyUndefs;
5916 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
5917 HasAnyUndefs, ElementBits) ||
5918 SplatBitSize > ElementBits)
5919 return false;
5920 Cnt = SplatBits.getSExtValue();
5921 return true;
5922}
5923
5924/// isVShiftLImm - Check if this is a valid build_vector for the immediate
5925/// operand of a vector shift left operation. That value must be in the range:
5926/// 0 <= Value < ElementBits for a left shift; or
5927/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005928static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00005929 assert(VT.isVector() && "vector shift count is not a vector type");
5930 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5931 if (! getVShiftImm(Op, ElementBits, Cnt))
5932 return false;
5933 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
5934}
5935
5936/// isVShiftRImm - Check if this is a valid build_vector for the immediate
5937/// operand of a vector shift right operation. For a shift opcode, the value
5938/// is positive, but for an intrinsic the value count must be negative. The
5939/// absolute value must be in the range:
5940/// 1 <= |Value| <= ElementBits for a right shift; or
5941/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00005942static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00005943 int64_t &Cnt) {
5944 assert(VT.isVector() && "vector shift count is not a vector type");
5945 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
5946 if (! getVShiftImm(Op, ElementBits, Cnt))
5947 return false;
5948 if (isIntrinsic)
5949 Cnt = -Cnt;
5950 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
5951}
5952
5953/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
5954static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
5955 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
5956 switch (IntNo) {
5957 default:
5958 // Don't do anything for most intrinsics.
5959 break;
5960
5961 // Vector shifts: check for immediate versions and lower them.
5962 // Note: This is done during DAG combining instead of DAG legalizing because
5963 // the build_vectors for 64-bit vector element shift counts are generally
5964 // not legal, and it is hard to see their values after they get legalized to
5965 // loads from a constant pool.
5966 case Intrinsic::arm_neon_vshifts:
5967 case Intrinsic::arm_neon_vshiftu:
5968 case Intrinsic::arm_neon_vshiftls:
5969 case Intrinsic::arm_neon_vshiftlu:
5970 case Intrinsic::arm_neon_vshiftn:
5971 case Intrinsic::arm_neon_vrshifts:
5972 case Intrinsic::arm_neon_vrshiftu:
5973 case Intrinsic::arm_neon_vrshiftn:
5974 case Intrinsic::arm_neon_vqshifts:
5975 case Intrinsic::arm_neon_vqshiftu:
5976 case Intrinsic::arm_neon_vqshiftsu:
5977 case Intrinsic::arm_neon_vqshiftns:
5978 case Intrinsic::arm_neon_vqshiftnu:
5979 case Intrinsic::arm_neon_vqshiftnsu:
5980 case Intrinsic::arm_neon_vqrshiftns:
5981 case Intrinsic::arm_neon_vqrshiftnu:
5982 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00005983 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00005984 int64_t Cnt;
5985 unsigned VShiftOpc = 0;
5986
5987 switch (IntNo) {
5988 case Intrinsic::arm_neon_vshifts:
5989 case Intrinsic::arm_neon_vshiftu:
5990 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
5991 VShiftOpc = ARMISD::VSHL;
5992 break;
5993 }
5994 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
5995 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
5996 ARMISD::VSHRs : ARMISD::VSHRu);
5997 break;
5998 }
5999 return SDValue();
6000
6001 case Intrinsic::arm_neon_vshiftls:
6002 case Intrinsic::arm_neon_vshiftlu:
6003 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6004 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006005 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006006
6007 case Intrinsic::arm_neon_vrshifts:
6008 case Intrinsic::arm_neon_vrshiftu:
6009 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
6010 break;
6011 return SDValue();
6012
6013 case Intrinsic::arm_neon_vqshifts:
6014 case Intrinsic::arm_neon_vqshiftu:
6015 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6016 break;
6017 return SDValue();
6018
6019 case Intrinsic::arm_neon_vqshiftsu:
6020 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
6021 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006022 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006023
6024 case Intrinsic::arm_neon_vshiftn:
6025 case Intrinsic::arm_neon_vrshiftn:
6026 case Intrinsic::arm_neon_vqshiftns:
6027 case Intrinsic::arm_neon_vqshiftnu:
6028 case Intrinsic::arm_neon_vqshiftnsu:
6029 case Intrinsic::arm_neon_vqrshiftns:
6030 case Intrinsic::arm_neon_vqrshiftnu:
6031 case Intrinsic::arm_neon_vqrshiftnsu:
6032 // Narrowing shifts require an immediate right shift.
6033 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
6034 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00006035 llvm_unreachable("invalid shift count for narrowing vector shift "
6036 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006037
6038 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006039 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00006040 }
6041
6042 switch (IntNo) {
6043 case Intrinsic::arm_neon_vshifts:
6044 case Intrinsic::arm_neon_vshiftu:
6045 // Opcode already set above.
6046 break;
6047 case Intrinsic::arm_neon_vshiftls:
6048 case Intrinsic::arm_neon_vshiftlu:
6049 if (Cnt == VT.getVectorElementType().getSizeInBits())
6050 VShiftOpc = ARMISD::VSHLLi;
6051 else
6052 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
6053 ARMISD::VSHLLs : ARMISD::VSHLLu);
6054 break;
6055 case Intrinsic::arm_neon_vshiftn:
6056 VShiftOpc = ARMISD::VSHRN; break;
6057 case Intrinsic::arm_neon_vrshifts:
6058 VShiftOpc = ARMISD::VRSHRs; break;
6059 case Intrinsic::arm_neon_vrshiftu:
6060 VShiftOpc = ARMISD::VRSHRu; break;
6061 case Intrinsic::arm_neon_vrshiftn:
6062 VShiftOpc = ARMISD::VRSHRN; break;
6063 case Intrinsic::arm_neon_vqshifts:
6064 VShiftOpc = ARMISD::VQSHLs; break;
6065 case Intrinsic::arm_neon_vqshiftu:
6066 VShiftOpc = ARMISD::VQSHLu; break;
6067 case Intrinsic::arm_neon_vqshiftsu:
6068 VShiftOpc = ARMISD::VQSHLsu; break;
6069 case Intrinsic::arm_neon_vqshiftns:
6070 VShiftOpc = ARMISD::VQSHRNs; break;
6071 case Intrinsic::arm_neon_vqshiftnu:
6072 VShiftOpc = ARMISD::VQSHRNu; break;
6073 case Intrinsic::arm_neon_vqshiftnsu:
6074 VShiftOpc = ARMISD::VQSHRNsu; break;
6075 case Intrinsic::arm_neon_vqrshiftns:
6076 VShiftOpc = ARMISD::VQRSHRNs; break;
6077 case Intrinsic::arm_neon_vqrshiftnu:
6078 VShiftOpc = ARMISD::VQRSHRNu; break;
6079 case Intrinsic::arm_neon_vqrshiftnsu:
6080 VShiftOpc = ARMISD::VQRSHRNsu; break;
6081 }
6082
6083 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006084 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006085 }
6086
6087 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00006088 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006089 int64_t Cnt;
6090 unsigned VShiftOpc = 0;
6091
6092 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
6093 VShiftOpc = ARMISD::VSLI;
6094 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
6095 VShiftOpc = ARMISD::VSRI;
6096 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006097 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006098 }
6099
6100 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
6101 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00006102 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006103 }
6104
6105 case Intrinsic::arm_neon_vqrshifts:
6106 case Intrinsic::arm_neon_vqrshiftu:
6107 // No immediate versions of these to check for.
6108 break;
6109 }
6110
6111 return SDValue();
6112}
6113
6114/// PerformShiftCombine - Checks for immediate versions of vector shifts and
6115/// lowers them. As with the vector shift intrinsics, this is done during DAG
6116/// combining instead of DAG legalizing because the build_vectors for 64-bit
6117/// vector element shift counts are generally not legal, and it is hard to see
6118/// their values after they get legalized to loads from a constant pool.
6119static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
6120 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00006121 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00006122
6123 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00006124 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6125 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00006126 return SDValue();
6127
6128 assert(ST->hasNEON() && "unexpected vector shift");
6129 int64_t Cnt;
6130
6131 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006132 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006133
6134 case ISD::SHL:
6135 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
6136 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006137 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006138 break;
6139
6140 case ISD::SRA:
6141 case ISD::SRL:
6142 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
6143 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
6144 ARMISD::VSHRs : ARMISD::VSHRu);
6145 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00006146 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00006147 }
6148 }
6149 return SDValue();
6150}
6151
6152/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
6153/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
6154static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
6155 const ARMSubtarget *ST) {
6156 SDValue N0 = N->getOperand(0);
6157
6158 // Check for sign- and zero-extensions of vector extract operations of 8-
6159 // and 16-bit vector elements. NEON supports these directly. They are
6160 // handled during DAG combining because type legalization will promote them
6161 // to 32-bit types and it is messy to recognize the operations after that.
6162 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
6163 SDValue Vec = N0.getOperand(0);
6164 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006165 EVT VT = N->getValueType(0);
6166 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006167 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6168
Owen Anderson825b72b2009-08-11 20:47:22 +00006169 if (VT == MVT::i32 &&
6170 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00006171 TLI.isTypeLegal(Vec.getValueType()) &&
6172 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006173
6174 unsigned Opc = 0;
6175 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006176 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00006177 case ISD::SIGN_EXTEND:
6178 Opc = ARMISD::VGETLANEs;
6179 break;
6180 case ISD::ZERO_EXTEND:
6181 case ISD::ANY_EXTEND:
6182 Opc = ARMISD::VGETLANEu;
6183 break;
6184 }
6185 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
6186 }
6187 }
6188
6189 return SDValue();
6190}
6191
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006192/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
6193/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
6194static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
6195 const ARMSubtarget *ST) {
6196 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00006197 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006198 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
6199 // a NaN; only do the transformation when it matches that behavior.
6200
6201 // For now only do this when using NEON for FP operations; if using VFP, it
6202 // is not obvious that the benefit outweighs the cost of switching to the
6203 // NEON pipeline.
6204 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
6205 N->getValueType(0) != MVT::f32)
6206 return SDValue();
6207
6208 SDValue CondLHS = N->getOperand(0);
6209 SDValue CondRHS = N->getOperand(1);
6210 SDValue LHS = N->getOperand(2);
6211 SDValue RHS = N->getOperand(3);
6212 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
6213
6214 unsigned Opcode = 0;
6215 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00006216 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006217 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00006218 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006219 IsReversed = true ; // x CC y ? y : x
6220 } else {
6221 return SDValue();
6222 }
6223
Bob Wilsone742bb52010-02-24 22:15:53 +00006224 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006225 switch (CC) {
6226 default: break;
6227 case ISD::SETOLT:
6228 case ISD::SETOLE:
6229 case ISD::SETLT:
6230 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006231 case ISD::SETULT:
6232 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006233 // If LHS is NaN, an ordered comparison will be false and the result will
6234 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
6235 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6236 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
6237 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6238 break;
6239 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
6240 // will return -0, so vmin can only be used for unsafe math or if one of
6241 // the operands is known to be nonzero.
6242 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
6243 !UnsafeFPMath &&
6244 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6245 break;
6246 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006247 break;
6248
6249 case ISD::SETOGT:
6250 case ISD::SETOGE:
6251 case ISD::SETGT:
6252 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006253 case ISD::SETUGT:
6254 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00006255 // If LHS is NaN, an ordered comparison will be false and the result will
6256 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
6257 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
6258 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
6259 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
6260 break;
6261 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
6262 // will return +0, so vmax can only be used for unsafe math or if one of
6263 // the operands is known to be nonzero.
6264 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
6265 !UnsafeFPMath &&
6266 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
6267 break;
6268 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006269 break;
6270 }
6271
6272 if (!Opcode)
6273 return SDValue();
6274 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
6275}
6276
Dan Gohman475871a2008-07-27 21:46:04 +00006277SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006278 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006279 switch (N->getOpcode()) {
6280 default: break;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006281 case ISD::ADD: return PerformADDCombine(N, DCI);
6282 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006283 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006284 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00006285 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00006286 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00006287 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006288 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00006289 case ISD::STORE: return PerformSTORECombine(N, DCI);
6290 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
6291 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00006292 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006293 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006294 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00006295 case ISD::SHL:
6296 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006297 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00006298 case ISD::SIGN_EXTEND:
6299 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00006300 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
6301 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Bob Wilson1c3ef902011-02-07 17:43:21 +00006302 case ARMISD::VLD2DUP:
6303 case ARMISD::VLD3DUP:
6304 case ARMISD::VLD4DUP:
6305 return CombineBaseUpdate(N, DCI);
6306 case ISD::INTRINSIC_VOID:
6307 case ISD::INTRINSIC_W_CHAIN:
6308 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
6309 case Intrinsic::arm_neon_vld1:
6310 case Intrinsic::arm_neon_vld2:
6311 case Intrinsic::arm_neon_vld3:
6312 case Intrinsic::arm_neon_vld4:
6313 case Intrinsic::arm_neon_vld2lane:
6314 case Intrinsic::arm_neon_vld3lane:
6315 case Intrinsic::arm_neon_vld4lane:
6316 case Intrinsic::arm_neon_vst1:
6317 case Intrinsic::arm_neon_vst2:
6318 case Intrinsic::arm_neon_vst3:
6319 case Intrinsic::arm_neon_vst4:
6320 case Intrinsic::arm_neon_vst2lane:
6321 case Intrinsic::arm_neon_vst3lane:
6322 case Intrinsic::arm_neon_vst4lane:
6323 return CombineBaseUpdate(N, DCI);
6324 default: break;
6325 }
6326 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006327 }
Dan Gohman475871a2008-07-27 21:46:04 +00006328 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00006329}
6330
Evan Cheng31959b12011-02-02 01:06:55 +00006331bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
6332 EVT VT) const {
6333 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
6334}
6335
Bill Wendlingaf566342009-08-15 21:21:19 +00006336bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00006337 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00006338 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00006339
6340 switch (VT.getSimpleVT().SimpleTy) {
6341 default:
6342 return false;
6343 case MVT::i8:
6344 case MVT::i16:
6345 case MVT::i32:
6346 return true;
6347 // FIXME: VLD1 etc with standard alignment is legal.
6348 }
6349}
6350
Evan Chenge6c835f2009-08-14 20:09:37 +00006351static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
6352 if (V < 0)
6353 return false;
6354
6355 unsigned Scale = 1;
6356 switch (VT.getSimpleVT().SimpleTy) {
6357 default: return false;
6358 case MVT::i1:
6359 case MVT::i8:
6360 // Scale == 1;
6361 break;
6362 case MVT::i16:
6363 // Scale == 2;
6364 Scale = 2;
6365 break;
6366 case MVT::i32:
6367 // Scale == 4;
6368 Scale = 4;
6369 break;
6370 }
6371
6372 if ((V & (Scale - 1)) != 0)
6373 return false;
6374 V /= Scale;
6375 return V == (V & ((1LL << 5) - 1));
6376}
6377
6378static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
6379 const ARMSubtarget *Subtarget) {
6380 bool isNeg = false;
6381 if (V < 0) {
6382 isNeg = true;
6383 V = - V;
6384 }
6385
6386 switch (VT.getSimpleVT().SimpleTy) {
6387 default: return false;
6388 case MVT::i1:
6389 case MVT::i8:
6390 case MVT::i16:
6391 case MVT::i32:
6392 // + imm12 or - imm8
6393 if (isNeg)
6394 return V == (V & ((1LL << 8) - 1));
6395 return V == (V & ((1LL << 12) - 1));
6396 case MVT::f32:
6397 case MVT::f64:
6398 // Same as ARM mode. FIXME: NEON?
6399 if (!Subtarget->hasVFP2())
6400 return false;
6401 if ((V & 3) != 0)
6402 return false;
6403 V >>= 2;
6404 return V == (V & ((1LL << 8) - 1));
6405 }
6406}
6407
Evan Chengb01fad62007-03-12 23:30:29 +00006408/// isLegalAddressImmediate - Return true if the integer value can be used
6409/// as the offset of the target addressing mode for load / store of the
6410/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00006411static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006412 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00006413 if (V == 0)
6414 return true;
6415
Evan Cheng65011532009-03-09 19:15:00 +00006416 if (!VT.isSimple())
6417 return false;
6418
Evan Chenge6c835f2009-08-14 20:09:37 +00006419 if (Subtarget->isThumb1Only())
6420 return isLegalT1AddressImmediate(V, VT);
6421 else if (Subtarget->isThumb2())
6422 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00006423
Evan Chenge6c835f2009-08-14 20:09:37 +00006424 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00006425 if (V < 0)
6426 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00006427 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00006428 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006429 case MVT::i1:
6430 case MVT::i8:
6431 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00006432 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006433 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006434 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00006435 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006436 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006437 case MVT::f32:
6438 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00006439 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00006440 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00006441 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00006442 return false;
6443 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00006444 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00006445 }
Evan Chenga8e29892007-01-19 07:51:42 +00006446}
6447
Evan Chenge6c835f2009-08-14 20:09:37 +00006448bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
6449 EVT VT) const {
6450 int Scale = AM.Scale;
6451 if (Scale < 0)
6452 return false;
6453
6454 switch (VT.getSimpleVT().SimpleTy) {
6455 default: return false;
6456 case MVT::i1:
6457 case MVT::i8:
6458 case MVT::i16:
6459 case MVT::i32:
6460 if (Scale == 1)
6461 return true;
6462 // r + r << imm
6463 Scale = Scale & ~1;
6464 return Scale == 2 || Scale == 4 || Scale == 8;
6465 case MVT::i64:
6466 // r + r
6467 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
6468 return true;
6469 return false;
6470 case MVT::isVoid:
6471 // Note, we allow "void" uses (basically, uses that aren't loads or
6472 // stores), because arm allows folding a scale into many arithmetic
6473 // operations. This should be made more precise and revisited later.
6474
6475 // Allow r << imm, but the imm has to be a multiple of two.
6476 if (Scale & 1) return false;
6477 return isPowerOf2_32(Scale);
6478 }
6479}
6480
Chris Lattner37caf8c2007-04-09 23:33:39 +00006481/// isLegalAddressingMode - Return true if the addressing mode represented
6482/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006483bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner37caf8c2007-04-09 23:33:39 +00006484 const Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006485 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00006486 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00006487 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006488
Chris Lattner37caf8c2007-04-09 23:33:39 +00006489 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006490 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006491 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006492
Chris Lattner37caf8c2007-04-09 23:33:39 +00006493 switch (AM.Scale) {
6494 case 0: // no scale reg, must be "r+i" or "r", or "i".
6495 break;
6496 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00006497 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00006498 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006499 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00006500 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00006501 // ARM doesn't support any R+R*scale+imm addr modes.
6502 if (AM.BaseOffs)
6503 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006504
Bob Wilson2c7dab12009-04-08 17:55:28 +00006505 if (!VT.isSimple())
6506 return false;
6507
Evan Chenge6c835f2009-08-14 20:09:37 +00006508 if (Subtarget->isThumb2())
6509 return isLegalT2ScaledAddressingMode(AM, VT);
6510
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006511 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00006512 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00006513 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00006514 case MVT::i1:
6515 case MVT::i8:
6516 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006517 if (Scale < 0) Scale = -Scale;
6518 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006519 return true;
6520 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00006521 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006522 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00006523 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006524 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00006525 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00006526 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00006527 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00006528
Owen Anderson825b72b2009-08-11 20:47:22 +00006529 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00006530 // Note, we allow "void" uses (basically, uses that aren't loads or
6531 // stores), because arm allows folding a scale into many arithmetic
6532 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00006533
Chris Lattner37caf8c2007-04-09 23:33:39 +00006534 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00006535 if (Scale & 1) return false;
6536 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00006537 }
6538 break;
Evan Chengb01fad62007-03-12 23:30:29 +00006539 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00006540 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00006541}
6542
Evan Cheng77e47512009-11-11 19:05:52 +00006543/// isLegalICmpImmediate - Return true if the specified immediate is legal
6544/// icmp immediate, that is the target has icmp instructions which can compare
6545/// a register against the immediate without having to materialize the
6546/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00006547bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00006548 if (!Subtarget->isThumb())
6549 return ARM_AM::getSOImmVal(Imm) != -1;
6550 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00006551 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00006552 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00006553}
6554
Owen Andersone50ed302009-08-10 22:56:29 +00006555static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006556 bool isSEXTLoad, SDValue &Base,
6557 SDValue &Offset, bool &isInc,
6558 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00006559 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6560 return false;
6561
Owen Anderson825b72b2009-08-11 20:47:22 +00006562 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00006563 // AddressingMode 3
6564 Base = Ptr->getOperand(0);
6565 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006566 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006567 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006568 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006569 isInc = false;
6570 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6571 return true;
6572 }
6573 }
6574 isInc = (Ptr->getOpcode() == ISD::ADD);
6575 Offset = Ptr->getOperand(1);
6576 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00006577 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00006578 // AddressingMode 2
6579 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006580 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00006581 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006582 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00006583 isInc = false;
6584 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6585 Base = Ptr->getOperand(0);
6586 return true;
6587 }
6588 }
6589
6590 if (Ptr->getOpcode() == ISD::ADD) {
6591 isInc = true;
6592 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
6593 if (ShOpcVal != ARM_AM::no_shift) {
6594 Base = Ptr->getOperand(1);
6595 Offset = Ptr->getOperand(0);
6596 } else {
6597 Base = Ptr->getOperand(0);
6598 Offset = Ptr->getOperand(1);
6599 }
6600 return true;
6601 }
6602
6603 isInc = (Ptr->getOpcode() == ISD::ADD);
6604 Base = Ptr->getOperand(0);
6605 Offset = Ptr->getOperand(1);
6606 return true;
6607 }
6608
Jim Grosbache5165492009-11-09 00:11:35 +00006609 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00006610 return false;
6611}
6612
Owen Andersone50ed302009-08-10 22:56:29 +00006613static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00006614 bool isSEXTLoad, SDValue &Base,
6615 SDValue &Offset, bool &isInc,
6616 SelectionDAG &DAG) {
6617 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
6618 return false;
6619
6620 Base = Ptr->getOperand(0);
6621 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
6622 int RHSC = (int)RHS->getZExtValue();
6623 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
6624 assert(Ptr->getOpcode() == ISD::ADD);
6625 isInc = false;
6626 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
6627 return true;
6628 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
6629 isInc = Ptr->getOpcode() == ISD::ADD;
6630 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
6631 return true;
6632 }
6633 }
6634
6635 return false;
6636}
6637
Evan Chenga8e29892007-01-19 07:51:42 +00006638/// getPreIndexedAddressParts - returns true by value, base pointer and
6639/// offset pointer and addressing mode by reference if the node's address
6640/// can be legally represented as pre-indexed load / store address.
6641bool
Dan Gohman475871a2008-07-27 21:46:04 +00006642ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
6643 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006644 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006645 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006646 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006647 return false;
6648
Owen Andersone50ed302009-08-10 22:56:29 +00006649 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006650 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006651 bool isSEXTLoad = false;
6652 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6653 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006654 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006655 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6656 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6657 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006658 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00006659 } else
6660 return false;
6661
6662 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006663 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006664 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006665 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
6666 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006667 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006668 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00006669 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00006670 if (!isLegal)
6671 return false;
6672
6673 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
6674 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006675}
6676
6677/// getPostIndexedAddressParts - returns true by value, base pointer and
6678/// offset pointer and addressing mode by reference if this node can be
6679/// combined with a load / store to form a post-indexed load / store.
6680bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00006681 SDValue &Base,
6682 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00006683 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00006684 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00006685 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00006686 return false;
6687
Owen Andersone50ed302009-08-10 22:56:29 +00006688 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00006689 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00006690 bool isSEXTLoad = false;
6691 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006692 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006693 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006694 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
6695 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006696 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00006697 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00006698 } else
6699 return false;
6700
6701 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00006702 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00006703 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00006704 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00006705 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00006706 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00006707 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
6708 isInc, DAG);
6709 if (!isLegal)
6710 return false;
6711
Evan Cheng28dad2a2010-05-18 21:31:17 +00006712 if (Ptr != Base) {
6713 // Swap base ptr and offset to catch more post-index load / store when
6714 // it's legal. In Thumb2 mode, offset must be an immediate.
6715 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
6716 !Subtarget->isThumb2())
6717 std::swap(Base, Offset);
6718
6719 // Post-indexed load / store update the base pointer.
6720 if (Ptr != Base)
6721 return false;
6722 }
6723
Evan Chenge88d5ce2009-07-02 07:28:31 +00006724 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
6725 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00006726}
6727
Dan Gohman475871a2008-07-27 21:46:04 +00006728void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006729 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00006730 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006731 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006732 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00006733 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006734 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006735 switch (Op.getOpcode()) {
6736 default: break;
6737 case ARMISD::CMOV: {
6738 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00006739 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006740 if (KnownZero == 0 && KnownOne == 0) return;
6741
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006742 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00006743 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
6744 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00006745 KnownZero &= KnownZeroRHS;
6746 KnownOne &= KnownOneRHS;
6747 return;
6748 }
6749 }
6750}
6751
6752//===----------------------------------------------------------------------===//
6753// ARM Inline Assembly Support
6754//===----------------------------------------------------------------------===//
6755
Evan Cheng55d42002011-01-08 01:24:27 +00006756bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
6757 // Looking for "rev" which is V6+.
6758 if (!Subtarget->hasV6Ops())
6759 return false;
6760
6761 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
6762 std::string AsmStr = IA->getAsmString();
6763 SmallVector<StringRef, 4> AsmPieces;
6764 SplitString(AsmStr, AsmPieces, ";\n");
6765
6766 switch (AsmPieces.size()) {
6767 default: return false;
6768 case 1:
6769 AsmStr = AsmPieces[0];
6770 AsmPieces.clear();
6771 SplitString(AsmStr, AsmPieces, " \t,");
6772
6773 // rev $0, $1
6774 if (AsmPieces.size() == 3 &&
6775 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
6776 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
6777 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
6778 if (Ty && Ty->getBitWidth() == 32)
6779 return IntrinsicLowering::LowerToByteSwap(CI);
6780 }
6781 break;
6782 }
6783
6784 return false;
6785}
6786
Evan Chenga8e29892007-01-19 07:51:42 +00006787/// getConstraintType - Given a constraint letter, return the type of
6788/// constraint it is for this target.
6789ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006790ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
6791 if (Constraint.size() == 1) {
6792 switch (Constraint[0]) {
6793 default: break;
6794 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006795 case 'w': return C_RegisterClass;
Chris Lattner4234f572007-03-25 02:14:49 +00006796 }
Evan Chenga8e29892007-01-19 07:51:42 +00006797 }
Chris Lattner4234f572007-03-25 02:14:49 +00006798 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00006799}
6800
John Thompson44ab89e2010-10-29 17:29:13 +00006801/// Examine constraint type and operand type and determine a weight value.
6802/// This object must already have been set up with the operand type
6803/// and the current alternative constraint selected.
6804TargetLowering::ConstraintWeight
6805ARMTargetLowering::getSingleConstraintMatchWeight(
6806 AsmOperandInfo &info, const char *constraint) const {
6807 ConstraintWeight weight = CW_Invalid;
6808 Value *CallOperandVal = info.CallOperandVal;
6809 // If we don't have a value, we can't do a match,
6810 // but allow it at the lowest weight.
6811 if (CallOperandVal == NULL)
6812 return CW_Default;
6813 const Type *type = CallOperandVal->getType();
6814 // Look at the constraint type.
6815 switch (*constraint) {
6816 default:
6817 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
6818 break;
6819 case 'l':
6820 if (type->isIntegerTy()) {
6821 if (Subtarget->isThumb())
6822 weight = CW_SpecificReg;
6823 else
6824 weight = CW_Register;
6825 }
6826 break;
6827 case 'w':
6828 if (type->isFloatingPointTy())
6829 weight = CW_Register;
6830 break;
6831 }
6832 return weight;
6833}
6834
Bob Wilson2dc4f542009-03-20 22:42:55 +00006835std::pair<unsigned, const TargetRegisterClass*>
Evan Chenga8e29892007-01-19 07:51:42 +00006836ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006837 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006838 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006839 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00006840 switch (Constraint[0]) {
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006841 case 'l':
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00006842 if (Subtarget->isThumb())
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006843 return std::make_pair(0U, ARM::tGPRRegisterClass);
6844 else
6845 return std::make_pair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006846 case 'r':
6847 return std::make_pair(0U, ARM::GPRRegisterClass);
6848 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006849 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006850 return std::make_pair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00006851 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006852 return std::make_pair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00006853 if (VT.getSizeInBits() == 128)
6854 return std::make_pair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006855 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006856 }
6857 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006858 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00006859 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00006860
Evan Chenga8e29892007-01-19 07:51:42 +00006861 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6862}
6863
6864std::vector<unsigned> ARMTargetLowering::
6865getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00006866 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006867 if (Constraint.size() != 1)
6868 return std::vector<unsigned>();
6869
6870 switch (Constraint[0]) { // GCC ARM Constraint Letters
6871 default: break;
6872 case 'l':
Jim Grosbach30eae3c2009-04-07 20:34:09 +00006873 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6874 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6875 0);
Evan Chenga8e29892007-01-19 07:51:42 +00006876 case 'r':
6877 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
6878 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
6879 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
6880 ARM::R12, ARM::LR, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006881 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00006882 if (VT == MVT::f32)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006883 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
6884 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
6885 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
6886 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
6887 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
6888 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
6889 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
6890 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
Bob Wilson5afffae2009-12-18 01:03:29 +00006891 if (VT.getSizeInBits() == 64)
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006892 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
6893 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
6894 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
6895 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
Evan Chengd831cda2009-12-08 23:06:22 +00006896 if (VT.getSizeInBits() == 128)
6897 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
6898 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00006899 break;
Evan Chenga8e29892007-01-19 07:51:42 +00006900 }
6901
6902 return std::vector<unsigned>();
6903}
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006904
6905/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6906/// vector. If it is invalid, don't add anything to Ops.
6907void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
6908 char Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006909 std::vector<SDValue>&Ops,
6910 SelectionDAG &DAG) const {
6911 SDValue Result(0, 0);
6912
6913 switch (Constraint) {
6914 default: break;
6915 case 'I': case 'J': case 'K': case 'L':
6916 case 'M': case 'N': case 'O':
6917 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
6918 if (!C)
6919 return;
6920
6921 int64_t CVal64 = C->getSExtValue();
6922 int CVal = (int) CVal64;
6923 // None of these constraints allow values larger than 32 bits. Check
6924 // that the value fits in an int.
6925 if (CVal != CVal64)
6926 return;
6927
6928 switch (Constraint) {
6929 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006930 if (Subtarget->isThumb1Only()) {
6931 // This must be a constant between 0 and 255, for ADD
6932 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006933 if (CVal >= 0 && CVal <= 255)
6934 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006935 } else if (Subtarget->isThumb2()) {
6936 // A constant that can be used as an immediate value in a
6937 // data-processing instruction.
6938 if (ARM_AM::getT2SOImmVal(CVal) != -1)
6939 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006940 } else {
6941 // A constant that can be used as an immediate value in a
6942 // data-processing instruction.
6943 if (ARM_AM::getSOImmVal(CVal) != -1)
6944 break;
6945 }
6946 return;
6947
6948 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006949 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006950 // This must be a constant between -255 and -1, for negated ADD
6951 // immediates. This can be used in GCC with an "n" modifier that
6952 // prints the negated value, for use with SUB instructions. It is
6953 // not useful otherwise but is implemented for compatibility.
6954 if (CVal >= -255 && CVal <= -1)
6955 break;
6956 } else {
6957 // This must be a constant between -4095 and 4095. It is not clear
6958 // what this constraint is intended for. Implemented for
6959 // compatibility with GCC.
6960 if (CVal >= -4095 && CVal <= 4095)
6961 break;
6962 }
6963 return;
6964
6965 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006966 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006967 // A 32-bit value where only one byte has a nonzero value. Exclude
6968 // zero to match GCC. This constraint is used by GCC internally for
6969 // constants that can be loaded with a move/shift combination.
6970 // It is not useful otherwise but is implemented for compatibility.
6971 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
6972 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006973 } else if (Subtarget->isThumb2()) {
6974 // A constant whose bitwise inverse can be used as an immediate
6975 // value in a data-processing instruction. This can be used in GCC
6976 // with a "B" modifier that prints the inverted value, for use with
6977 // BIC and MVN instructions. It is not useful otherwise but is
6978 // implemented for compatibility.
6979 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
6980 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006981 } else {
6982 // A constant whose bitwise inverse can be used as an immediate
6983 // value in a data-processing instruction. This can be used in GCC
6984 // with a "B" modifier that prints the inverted value, for use with
6985 // BIC and MVN instructions. It is not useful otherwise but is
6986 // implemented for compatibility.
6987 if (ARM_AM::getSOImmVal(~CVal) != -1)
6988 break;
6989 }
6990 return;
6991
6992 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00006993 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00006994 // This must be a constant between -7 and 7,
6995 // for 3-operand ADD/SUB immediate instructions.
6996 if (CVal >= -7 && CVal < 7)
6997 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00006998 } else if (Subtarget->isThumb2()) {
6999 // A constant whose negation can be used as an immediate value in a
7000 // data-processing instruction. This can be used in GCC with an "n"
7001 // modifier that prints the negated value, for use with SUB
7002 // instructions. It is not useful otherwise but is implemented for
7003 // compatibility.
7004 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
7005 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007006 } else {
7007 // A constant whose negation can be used as an immediate value in a
7008 // data-processing instruction. This can be used in GCC with an "n"
7009 // modifier that prints the negated value, for use with SUB
7010 // instructions. It is not useful otherwise but is implemented for
7011 // compatibility.
7012 if (ARM_AM::getSOImmVal(-CVal) != -1)
7013 break;
7014 }
7015 return;
7016
7017 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007018 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007019 // This must be a multiple of 4 between 0 and 1020, for
7020 // ADD sp + immediate.
7021 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
7022 break;
7023 } else {
7024 // A power of two or a constant between 0 and 32. This is used in
7025 // GCC for the shift amount on shifted register operands, but it is
7026 // useful in general for any shift amounts.
7027 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
7028 break;
7029 }
7030 return;
7031
7032 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007033 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007034 // This must be a constant between 0 and 31, for shift amounts.
7035 if (CVal >= 0 && CVal <= 31)
7036 break;
7037 }
7038 return;
7039
7040 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00007041 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007042 // This must be a multiple of 4 between -508 and 508, for
7043 // ADD/SUB sp = sp + immediate.
7044 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
7045 break;
7046 }
7047 return;
7048 }
7049 Result = DAG.getTargetConstant(CVal, Op.getValueType());
7050 break;
7051 }
7052
7053 if (Result.getNode()) {
7054 Ops.push_back(Result);
7055 return;
7056 }
Dale Johannesen1784d162010-06-25 21:55:36 +00007057 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007058}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00007059
7060bool
7061ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7062 // The ARM target isn't yet aware of offsets.
7063 return false;
7064}
Evan Cheng39382422009-10-28 01:44:26 +00007065
7066int ARM::getVFPf32Imm(const APFloat &FPImm) {
7067 APInt Imm = FPImm.bitcastToAPInt();
7068 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
7069 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
7070 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
7071
7072 // We can handle 4 bits of mantissa.
7073 // mantissa = (16+UInt(e:f:g:h))/16.
7074 if (Mantissa & 0x7ffff)
7075 return -1;
7076 Mantissa >>= 19;
7077 if ((Mantissa & 0xf) != Mantissa)
7078 return -1;
7079
7080 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7081 if (Exp < -3 || Exp > 4)
7082 return -1;
7083 Exp = ((Exp+3) & 0x7) ^ 4;
7084
7085 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7086}
7087
7088int ARM::getVFPf64Imm(const APFloat &FPImm) {
7089 APInt Imm = FPImm.bitcastToAPInt();
7090 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
7091 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
7092 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
7093
7094 // We can handle 4 bits of mantissa.
7095 // mantissa = (16+UInt(e:f:g:h))/16.
7096 if (Mantissa & 0xffffffffffffLL)
7097 return -1;
7098 Mantissa >>= 48;
7099 if ((Mantissa & 0xf) != Mantissa)
7100 return -1;
7101
7102 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
7103 if (Exp < -3 || Exp > 4)
7104 return -1;
7105 Exp = ((Exp+3) & 0x7) ^ 4;
7106
7107 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
7108}
7109
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007110bool ARM::isBitFieldInvertedMask(unsigned v) {
7111 if (v == 0xffffffff)
7112 return 0;
7113 // there can be 1's on either or both "outsides", all the "inside"
7114 // bits must be 0's
7115 unsigned int lsb = 0, msb = 31;
7116 while (v & (1 << msb)) --msb;
7117 while (v & (1 << lsb)) ++lsb;
7118 for (unsigned int i = lsb; i <= msb; ++i) {
7119 if (v & (1 << i))
7120 return 0;
7121 }
7122 return 1;
7123}
7124
Evan Cheng39382422009-10-28 01:44:26 +00007125/// isFPImmLegal - Returns true if the target can instruction select the
7126/// specified FP immediate natively. If false, the legalizer will
7127/// materialize the FP immediate as a load from a constant pool.
7128bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
7129 if (!Subtarget->hasVFP3())
7130 return false;
7131 if (VT == MVT::f32)
7132 return ARM::getVFPf32Imm(Imm) != -1;
7133 if (VT == MVT::f64)
7134 return ARM::getVFPf64Imm(Imm) != -1;
7135 return false;
7136}
Bob Wilson65ffec42010-09-21 17:56:22 +00007137
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007138/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00007139/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
7140/// specified in the intrinsic calls.
7141bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
7142 const CallInst &I,
7143 unsigned Intrinsic) const {
7144 switch (Intrinsic) {
7145 case Intrinsic::arm_neon_vld1:
7146 case Intrinsic::arm_neon_vld2:
7147 case Intrinsic::arm_neon_vld3:
7148 case Intrinsic::arm_neon_vld4:
7149 case Intrinsic::arm_neon_vld2lane:
7150 case Intrinsic::arm_neon_vld3lane:
7151 case Intrinsic::arm_neon_vld4lane: {
7152 Info.opc = ISD::INTRINSIC_W_CHAIN;
7153 // Conservatively set memVT to the entire set of vectors loaded.
7154 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
7155 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7156 Info.ptrVal = I.getArgOperand(0);
7157 Info.offset = 0;
7158 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7159 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7160 Info.vol = false; // volatile loads with NEON intrinsics not supported
7161 Info.readMem = true;
7162 Info.writeMem = false;
7163 return true;
7164 }
7165 case Intrinsic::arm_neon_vst1:
7166 case Intrinsic::arm_neon_vst2:
7167 case Intrinsic::arm_neon_vst3:
7168 case Intrinsic::arm_neon_vst4:
7169 case Intrinsic::arm_neon_vst2lane:
7170 case Intrinsic::arm_neon_vst3lane:
7171 case Intrinsic::arm_neon_vst4lane: {
7172 Info.opc = ISD::INTRINSIC_VOID;
7173 // Conservatively set memVT to the entire set of vectors stored.
7174 unsigned NumElts = 0;
7175 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
7176 const Type *ArgTy = I.getArgOperand(ArgI)->getType();
7177 if (!ArgTy->isVectorTy())
7178 break;
7179 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
7180 }
7181 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
7182 Info.ptrVal = I.getArgOperand(0);
7183 Info.offset = 0;
7184 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
7185 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
7186 Info.vol = false; // volatile stores with NEON intrinsics not supported
7187 Info.readMem = false;
7188 Info.writeMem = true;
7189 return true;
7190 }
7191 default:
7192 break;
7193 }
7194
7195 return false;
7196}