blob: 702e549e89adb50b897cef3ae0ef58b01c9db2ac [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Cheng10043e22007-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
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000026#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000027#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling202803e2011-10-05 00:02:33 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000034#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
36#include "llvm/IR/Constants.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000039#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Instructions.h"
42#include "llvm/IR/Intrinsics.h"
43#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000046#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000047#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000048#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000049#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000050#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000051using namespace llvm;
52
Chandler Carruth84e68b22014-04-22 02:41:26 +000053#define DEBUG_TYPE "arm-isel"
54
Dale Johannesend679ff72010-06-03 21:09:53 +000055STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000056STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000057STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000058
Eric Christopher347f4c32010-12-15 23:47:29 +000059cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000060EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000061 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000062 cl::init(false));
63
Evan Chengf128bdc2010-06-16 07:35:02 +000064static cl::opt<bool>
65ARMInterworking("arm-interworking", cl::Hidden,
66 cl::desc("Enable / disable ARM interworking (for debugging only)"),
67 cl::init(true));
68
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000069namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000070 class ARMCCState : public CCState {
71 public:
72 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Craig Topperb94011f2013-07-14 04:42:23 +000073 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
Cameron Zwarich89019782011-06-10 20:59:24 +000074 LLVMContext &C, ParmContext PC)
75 : CCState(CC, isVarArg, MF, TM, locs, C) {
76 assert(((PC == Call) || (PC == Prologue)) &&
77 "ARMCCState users must specify whether their context is call"
78 "or prologue generation.");
79 CallOrPrologue = PC;
80 }
81 };
82}
83
Stuart Hastings45fe3c32011-04-20 16:47:52 +000084// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000085static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000086 ARM::R0, ARM::R1, ARM::R2, ARM::R3
87};
88
Craig Topper4fa625f2012-08-12 03:16:37 +000089void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
90 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000091 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000092 setOperationAction(ISD::LOAD, VT, Promote);
93 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000094
Craig Topper4fa625f2012-08-12 03:16:37 +000095 setOperationAction(ISD::STORE, VT, Promote);
96 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000097 }
98
Craig Topper4fa625f2012-08-12 03:16:37 +000099 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000100 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000101 setOperationAction(ISD::SETCC, VT, Custom);
102 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
103 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000104 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
107 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
108 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000109 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000110 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
112 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
113 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000114 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000115 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
116 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
117 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
118 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
119 setOperationAction(ISD::SELECT, VT, Expand);
120 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000121 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000124 setOperationAction(ISD::SHL, VT, Custom);
125 setOperationAction(ISD::SRA, VT, Custom);
126 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000127 }
128
129 // Promote all bit-wise operations.
130 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000131 setOperationAction(ISD::AND, VT, Promote);
132 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
133 setOperationAction(ISD::OR, VT, Promote);
134 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
135 setOperationAction(ISD::XOR, VT, Promote);
136 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000137 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000138
139 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000140 setOperationAction(ISD::SDIV, VT, Expand);
141 setOperationAction(ISD::UDIV, VT, Expand);
142 setOperationAction(ISD::FDIV, VT, Expand);
143 setOperationAction(ISD::SREM, VT, Expand);
144 setOperationAction(ISD::UREM, VT, Expand);
145 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000146}
147
Craig Topper4fa625f2012-08-12 03:16:37 +0000148void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000149 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000150 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000151}
152
Craig Topper4fa625f2012-08-12 03:16:37 +0000153void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000154 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000155 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000156}
157
Chris Lattner5e693ed2009-07-28 03:13:23 +0000158static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
Tim Northoverd6a729b2014-01-06 14:28:05 +0000159 if (TM.getSubtarget<ARMSubtarget>().isTargetMachO())
Bill Wendlingbbcaa402010-03-15 21:09:38 +0000160 return new TargetLoweringObjectFileMachO();
Bill Wendling46ffefc2010-03-09 02:46:12 +0000161
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000162 return new ARMElfTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000163}
164
Evan Cheng10043e22007-01-19 07:51:42 +0000165ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Cheng408aa562009-11-06 22:24:13 +0000166 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000167 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Chengdf907f42010-07-23 22:39:59 +0000168 RegInfo = TM.getRegisterInfo();
Evan Chengbf407072010-09-10 01:29:16 +0000169 Itins = TM.getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000170
Duncan Sandsf2641e12011-09-06 19:07:46 +0000171 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
172
Tim Northoverd6a729b2014-01-06 14:28:05 +0000173 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000174 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000175 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Tim Northover978d25f2014-04-22 10:10:09 +0000176 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000177 // Single-precision floating-point arithmetic.
178 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
179 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
180 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
181 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000182
Evan Chengc9f22fd12007-04-27 08:15:43 +0000183 // Double-precision floating-point arithmetic.
184 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
185 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
186 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
187 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000188
Evan Chengc9f22fd12007-04-27 08:15:43 +0000189 // Single-precision comparisons.
190 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
191 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
192 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
193 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
194 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
195 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
196 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
197 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000198
Evan Chengc9f22fd12007-04-27 08:15:43 +0000199 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000207
Evan Chengc9f22fd12007-04-27 08:15:43 +0000208 // Double-precision comparisons.
209 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
210 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
211 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
212 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
213 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
214 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
215 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
216 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000217
Evan Chengc9f22fd12007-04-27 08:15:43 +0000218 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000226
Evan Chengc9f22fd12007-04-27 08:15:43 +0000227 // Floating-point to integer conversions.
228 // i64 conversions are done via library routines even when generating VFP
229 // instructions, so use the same ones.
230 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
231 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
232 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
233 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000234
Evan Chengc9f22fd12007-04-27 08:15:43 +0000235 // Conversions between floating types.
236 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
237 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
238
239 // Integer to floating-point conversions.
240 // i64 conversions are done via library routines even when generating VFP
241 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000242 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
243 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000244 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
245 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
246 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
247 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
248 }
Evan Cheng10043e22007-01-19 07:51:42 +0000249 }
250
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000251 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000252 setLibcallName(RTLIB::SHL_I128, nullptr);
253 setLibcallName(RTLIB::SRL_I128, nullptr);
254 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000255
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000256 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
257 !Subtarget->isTargetWindows()) {
Wesley Peck527da1b2010-11-23 03:31:01 +0000258 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000259 // RTABI chapter 4.1.2, Table 2
260 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
261 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
262 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
263 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
264 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
265 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
266 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
267 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
268
269 // Double-precision floating-point comparison helper functions
270 // RTABI chapter 4.1.2, Table 3
271 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
272 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
273 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
274 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
275 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
276 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
277 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
278 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
279 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
280 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
281 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
282 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
284 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
285 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
286 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
287 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
288 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
295
296 // Single-precision floating-point arithmetic helper functions
297 // RTABI chapter 4.1.2, Table 4
298 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
299 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
300 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
301 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
302 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
303 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
304 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
305 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
306
307 // Single-precision floating-point comparison helper functions
308 // RTABI chapter 4.1.2, Table 5
309 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
310 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
311 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
312 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
313 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
314 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
315 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
316 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
317 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
318 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
319 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
320 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
322 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
323 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
324 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
325 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
326 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
327 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
333
334 // Floating-point to integer conversions.
335 // RTABI chapter 4.1.2, Table 6
336 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
337 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
338 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
339 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
340 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
341 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
342 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
343 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
344 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
345 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
346 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
352
353 // Conversions between floating types.
354 // RTABI chapter 4.1.2, Table 7
355 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
356 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
357 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peck527da1b2010-11-23 03:31:01 +0000358 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000359
360 // Integer to floating-point conversions.
361 // RTABI chapter 4.1.2, Table 8
362 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
363 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
364 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
365 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
366 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
367 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
368 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
369 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
370 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
371 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
372 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
378
379 // Long long helper functions
380 // RTABI chapter 4.2, Table 9
381 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000382 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
383 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
384 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
385 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
386 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
391
392 // Integer division functions
393 // RTABI chapter 4.3.1
394 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
395 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
396 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000397 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000398 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
399 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
400 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000401 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000402 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
403 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
404 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000405 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000406 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
407 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peck527da1b2010-11-23 03:31:01 +0000408 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000409 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin4cd51872011-05-22 21:41:23 +0000410
411 // Memory operations
412 // RTABI chapter 4.3.4
413 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
414 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
415 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000416 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
417 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
418 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000419 }
420
Bob Wilsonbc158992011-10-07 16:59:21 +0000421 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000422 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000423 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
424 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
425 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
426 }
427
David Goodwin22c2fba2009-07-08 23:10:31 +0000428 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000429 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000430 else
Craig Topperc7242e02012-04-20 07:30:17 +0000431 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000432 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
433 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000434 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbach4d5dc3e2010-08-11 15:44:15 +0000435 if (!Subtarget->isFPOnlySP())
Craig Topperc7242e02012-04-20 07:30:17 +0000436 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson7117a912009-03-20 22:42:55 +0000437
Owen Anderson9f944592009-08-11 20:47:22 +0000438 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000439 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000440
Eli Friedman6f84fed2011-11-08 01:43:53 +0000441 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
442 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
443 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
444 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
445 setTruncStoreAction((MVT::SimpleValueType)VT,
446 (MVT::SimpleValueType)InnerVT, Expand);
447 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
448 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
449 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000450
451 setOperationAction(ISD::MULHS, (MVT::SimpleValueType)VT, Expand);
452 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
453 setOperationAction(ISD::MULHU, (MVT::SimpleValueType)VT, Expand);
454 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000455 }
456
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000457 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000458 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000459
Bob Wilson2e076c42009-06-22 23:27:02 +0000460 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000461 addDRTypeForNEON(MVT::v2f32);
462 addDRTypeForNEON(MVT::v8i8);
463 addDRTypeForNEON(MVT::v4i16);
464 addDRTypeForNEON(MVT::v2i32);
465 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000466
Owen Anderson9f944592009-08-11 20:47:22 +0000467 addQRTypeForNEON(MVT::v4f32);
468 addQRTypeForNEON(MVT::v2f64);
469 addQRTypeForNEON(MVT::v16i8);
470 addQRTypeForNEON(MVT::v8i16);
471 addQRTypeForNEON(MVT::v4i32);
472 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000473
Bob Wilson194a2512009-09-15 23:55:57 +0000474 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
475 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000476 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
477 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000478 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
479 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
480 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000481 // FIXME: Code duplication: FDIV and FREM are expanded always, see
482 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000483 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
484 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000485 // FIXME: Create unittest.
486 // In another words, find a way when "copysign" appears in DAG with vector
487 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000488 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000489 // FIXME: Code duplication: SETCC has custom operation action, see
490 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000491 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000492 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000493 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
494 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
495 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
496 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
497 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
498 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
499 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
500 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
501 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
502 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
503 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
504 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000505 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000506 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
507 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
508 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
509 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
510 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000511 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000512
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000513 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
514 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
515 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
516 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
517 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
518 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
519 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
520 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
521 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
522 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000523 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
524 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
525 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
526 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000527 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000528
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000529 // Mark v2f32 intrinsics.
530 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
531 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
532 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
533 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
534 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
535 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
536 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
537 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
538 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
539 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
540 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
541 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
542 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
543 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
544 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
545
Bob Wilson6cc46572009-09-16 00:32:15 +0000546 // Neon does not support some operations on v1i64 and v2i64 types.
547 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000548 // Custom handling for some quad-vector types to detect VMULL.
549 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
550 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
551 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000552 // Custom handling for some vector types to avoid expensive expansions
553 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
554 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
555 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
556 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000557 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
558 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000559 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000560 // a destination type that is wider than the source, and nor does
561 // it have a FP_TO_[SU]INT instruction with a narrower destination than
562 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000563 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
564 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000565 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
566 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000567
Eli Friedmane6385e62012-11-15 22:44:27 +0000568 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000569 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000570
Evan Chengb4eae132012-12-04 22:41:50 +0000571 // NEON does not have single instruction CTPOP for vectors with element
572 // types wider than 8-bits. However, custom lowering can leverage the
573 // v8i8/v16i8 vcnt instruction.
574 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
575 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
576 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
577 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
578
Jim Grosbach5f215872013-02-27 21:31:12 +0000579 // NEON only has FMA instructions as of VFP4.
580 if (!Subtarget->hasVFP4()) {
581 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
582 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
583 }
584
Bob Wilson06fce872011-02-07 17:43:21 +0000585 setTargetDAGCombine(ISD::INTRINSIC_VOID);
586 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000587 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
588 setTargetDAGCombine(ISD::SHL);
589 setTargetDAGCombine(ISD::SRL);
590 setTargetDAGCombine(ISD::SRA);
591 setTargetDAGCombine(ISD::SIGN_EXTEND);
592 setTargetDAGCombine(ISD::ZERO_EXTEND);
593 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000594 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000595 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000596 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000597 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
598 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000599 setTargetDAGCombine(ISD::FP_TO_SINT);
600 setTargetDAGCombine(ISD::FP_TO_UINT);
601 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000602
James Molloy547d4c02012-02-20 09:24:05 +0000603 // It is legal to extload from v4i8 to v4i16 or v4i32.
604 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
605 MVT::v4i16, MVT::v2i16,
606 MVT::v2i32};
607 for (unsigned i = 0; i < 6; ++i) {
608 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
609 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
610 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
611 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000612 }
613
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000614 // ARM and Thumb2 support UMLAL/SMLAL.
615 if (!Subtarget->isThumb1Only())
616 setTargetDAGCombine(ISD::ADDC);
617
618
Evan Cheng6addd652007-05-18 00:19:34 +0000619 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000620
621 // ARM does not have f32 extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000622 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000623
Duncan Sands95d46ef2008-01-23 20:39:46 +0000624 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000625 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000626
Evan Cheng10043e22007-01-19 07:51:42 +0000627 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000628 if (!Subtarget->isThumb1Only()) {
629 for (unsigned im = (unsigned)ISD::PRE_INC;
630 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000631 setIndexedLoadAction(im, MVT::i1, Legal);
632 setIndexedLoadAction(im, MVT::i8, Legal);
633 setIndexedLoadAction(im, MVT::i16, Legal);
634 setIndexedLoadAction(im, MVT::i32, Legal);
635 setIndexedStoreAction(im, MVT::i1, Legal);
636 setIndexedStoreAction(im, MVT::i8, Legal);
637 setIndexedStoreAction(im, MVT::i16, Legal);
638 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000639 }
Evan Cheng10043e22007-01-19 07:51:42 +0000640 }
641
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000642 setOperationAction(ISD::SADDO, MVT::i32, Custom);
643 setOperationAction(ISD::UADDO, MVT::i32, Custom);
644 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
645 setOperationAction(ISD::USUBO, MVT::i32, Custom);
646
Evan Cheng10043e22007-01-19 07:51:42 +0000647 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000648 setOperationAction(ISD::MUL, MVT::i64, Expand);
649 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000650 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000651 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
652 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000653 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000654 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
655 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000656 setOperationAction(ISD::MULHS, MVT::i32, Expand);
657
Jim Grosbach5d994042009-10-31 19:38:01 +0000658 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000659 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000660 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000661 setOperationAction(ISD::SRL, MVT::i64, Custom);
662 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000663
Evan Chenge8916542011-08-30 01:34:54 +0000664 if (!Subtarget->isThumb1Only()) {
665 // FIXME: We should do this for Thumb1 as well.
666 setOperationAction(ISD::ADDC, MVT::i32, Custom);
667 setOperationAction(ISD::ADDE, MVT::i32, Custom);
668 setOperationAction(ISD::SUBC, MVT::i32, Custom);
669 setOperationAction(ISD::SUBE, MVT::i32, Custom);
670 }
671
Evan Cheng10043e22007-01-19 07:51:42 +0000672 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000673 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000674 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000675 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000676 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000677 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000678
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000679 // These just redirect to CTTZ and CTLZ on ARM.
680 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
681 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
682
Tim Northoverbc933082013-05-23 19:11:20 +0000683 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
684
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000685 // Only ARMv6 has BSWAP.
686 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000687 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000688
Bob Wilsone8a549c2012-09-29 21:43:49 +0000689 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
690 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
691 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000692 setOperationAction(ISD::SDIV, MVT::i32, Expand);
693 setOperationAction(ISD::UDIV, MVT::i32, Expand);
694 }
Renato Golin87610692013-07-16 09:32:17 +0000695
696 // FIXME: Also set divmod for SREM on EABI
Owen Anderson9f944592009-08-11 20:47:22 +0000697 setOperationAction(ISD::SREM, MVT::i32, Expand);
698 setOperationAction(ISD::UREM, MVT::i32, Expand);
Renato Golin87610692013-07-16 09:32:17 +0000699 // Register based DivRem for AEABI (RTABI 4.2)
700 if (Subtarget->isTargetAEABI()) {
701 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
702 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
703 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
704 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
705 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
706 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
707 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
708 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
709
710 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
711 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
712 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
713 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
714 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
715 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
716 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
717 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
718
719 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
720 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
721 } else {
722 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
723 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
724 }
Bob Wilson7117a912009-03-20 22:42:55 +0000725
Owen Anderson9f944592009-08-11 20:47:22 +0000726 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
727 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
728 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
729 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000730 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000731
Evan Cheng74d92c12011-04-08 21:37:21 +0000732 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000733
Evan Cheng10043e22007-01-19 07:51:42 +0000734 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000735 setOperationAction(ISD::VASTART, MVT::Other, Custom);
736 setOperationAction(ISD::VAARG, MVT::Other, Expand);
737 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
738 setOperationAction(ISD::VAEND, MVT::Other, Expand);
739 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
740 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000741
Tim Northoverd6a729b2014-01-06 14:28:05 +0000742 if (!Subtarget->isTargetMachO()) {
743 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000744 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000745 setExceptionPointerRegister(ARM::R0);
746 setExceptionSelectorRegister(ARM::R1);
747 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000748
Evan Chengf7f97b42010-04-15 22:20:34 +0000749 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng6e809de2010-08-11 06:22:01 +0000750 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
751 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000752 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000753 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
754 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000755 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000756
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000757 // On v8, we have particularly efficient implementations of atomic fences
758 // if they can be combined with nearby atomic loads and stores.
759 if (!Subtarget->hasV8Ops()) {
760 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
761 setInsertFencesForAtomic(true);
762 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000763 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000764 // If there's anything we can use as a barrier, go through custom lowering
765 // for ATOMIC_FENCE.
766 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
767 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
768
Jim Grosbach6860bb72010-06-18 22:35:32 +0000769 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000770 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000771 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000772 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000773 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000774 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000775 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000776 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000777 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000778 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000779 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000780 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000781 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000782 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
783 // Unordered/Monotonic case.
784 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
785 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000786 }
Evan Cheng10043e22007-01-19 07:51:42 +0000787
Evan Cheng21acf9f2010-11-04 05:19:35 +0000788 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000789
Eli Friedman8cfa7712010-06-26 04:36:50 +0000790 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
791 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000792 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
793 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000794 }
Owen Anderson9f944592009-08-11 20:47:22 +0000795 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000796
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000797 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
798 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000799 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000800 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000801 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000802 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
803 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000804
805 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000806 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000807 if (Subtarget->isTargetDarwin()) {
808 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
809 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000810 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000811 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000812
Owen Anderson9f944592009-08-11 20:47:22 +0000813 setOperationAction(ISD::SETCC, MVT::i32, Expand);
814 setOperationAction(ISD::SETCC, MVT::f32, Expand);
815 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000816 setOperationAction(ISD::SELECT, MVT::i32, Custom);
817 setOperationAction(ISD::SELECT, MVT::f32, Custom);
818 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000819 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
820 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
821 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000822
Owen Anderson9f944592009-08-11 20:47:22 +0000823 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
824 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
825 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
826 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
827 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000828
Dan Gohman482732a2007-10-11 23:21:31 +0000829 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000830 setOperationAction(ISD::FSIN, MVT::f64, Expand);
831 setOperationAction(ISD::FSIN, MVT::f32, Expand);
832 setOperationAction(ISD::FCOS, MVT::f32, Expand);
833 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000834 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
835 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000836 setOperationAction(ISD::FREM, MVT::f64, Expand);
837 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000838 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
839 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000840 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
841 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000842 }
Owen Anderson9f944592009-08-11 20:47:22 +0000843 setOperationAction(ISD::FPOW, MVT::f64, Expand);
844 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000845
Evan Chengd0007f32012-04-10 21:40:28 +0000846 if (!Subtarget->hasVFP4()) {
847 setOperationAction(ISD::FMA, MVT::f64, Expand);
848 setOperationAction(ISD::FMA, MVT::f32, Expand);
849 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000850
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000851 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000852 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000853 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
854 if (Subtarget->hasVFP2()) {
855 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
856 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
857 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
858 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
859 }
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000860 // Special handling for half-precision FP.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000861 if (!Subtarget->hasFP16()) {
862 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
863 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000864 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000865 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000866
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000867 // Combine sin / cos into one node or libcall if possible.
868 if (Subtarget->hasSinCos()) {
869 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
870 setLibcallName(RTLIB::SINCOS_F64, "sincos");
871 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
872 // For iOS, we don't want to the normal expansion of a libcall to
873 // sincos. We want to issue a libcall to __sincos_stret.
874 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
875 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
876 }
877 }
Evan Cheng10043e22007-01-19 07:51:42 +0000878
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000879 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000880 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000881 setTargetDAGCombine(ISD::ADD);
882 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000883 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000884 setTargetDAGCombine(ISD::AND);
885 setTargetDAGCombine(ISD::OR);
886 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000887
Evan Chengf258a152012-02-23 02:58:19 +0000888 if (Subtarget->hasV6Ops())
889 setTargetDAGCombine(ISD::SRL);
890
Evan Cheng10043e22007-01-19 07:51:42 +0000891 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000892
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000893 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
894 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000895 setSchedulingPreference(Sched::RegPressure);
896 else
897 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000898
Evan Cheng3ae2b792011-01-06 06:52:41 +0000899 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000900 MaxStoresPerMemset = 8;
901 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
902 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
903 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
904 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
905 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000906
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000907 // On ARM arguments smaller than 4 bytes are extended, so all arguments
908 // are at least 4 bytes aligned.
909 setMinStackArgumentAlignment(4);
910
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000911 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000912 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000913
Eli Friedman2518f832011-05-06 20:34:06 +0000914 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000915}
916
Andrew Trick43f25632011-01-19 02:35:27 +0000917// FIXME: It might make sense to define the representative register class as the
918// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
919// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
920// SPR's representative would be DPR_VFP2. This should work well if register
921// pressure tracking were modified such that a register use would increment the
922// pressure of the register class's representative and all of it's super
923// classes' representatives transitively. We have not implemented this because
924// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000925// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000926// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000927std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000928ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000929 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000930 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000931 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000932 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000933 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000934 // Use DPR as representative register class for all floating point
935 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
936 // the cost is 1 for both f32 and f64.
937 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000938 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000939 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000940 // When NEON is used for SP, only half of the register file is available
941 // because operations that define both SP and DP results will be constrained
942 // to the VFP2 class (D0-D15). We currently model this constraint prior to
943 // coalescing by double-counting the SP regs. See the FIXME above.
944 if (Subtarget->useNEONForSinglePrecisionFP())
945 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000946 break;
947 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
948 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000949 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000950 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000951 break;
952 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000953 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000954 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000955 break;
956 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000957 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000958 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000959 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000960 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000961 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000962}
963
Evan Cheng10043e22007-01-19 07:51:42 +0000964const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
965 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000966 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000967 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000968 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000969 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
970 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000971 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000972 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
973 case ARMISD::tCALL: return "ARMISD::tCALL";
974 case ARMISD::BRCOND: return "ARMISD::BRCOND";
975 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000976 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000977 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +0000978 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +0000979 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
980 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +0000981 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +0000982 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +0000983 case ARMISD::CMPFP: return "ARMISD::CMPFP";
984 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +0000985 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +0000986 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +0000987
Evan Cheng10043e22007-01-19 07:51:42 +0000988 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +0000989
Jim Grosbach8546ec92010-01-18 19:58:49 +0000990 case ARMISD::RBIT: return "ARMISD::RBIT";
991
Bob Wilsone4191e72010-03-19 22:51:32 +0000992 case ARMISD::FTOSI: return "ARMISD::FTOSI";
993 case ARMISD::FTOUI: return "ARMISD::FTOUI";
994 case ARMISD::SITOF: return "ARMISD::SITOF";
995 case ARMISD::UITOF: return "ARMISD::UITOF";
996
Evan Cheng10043e22007-01-19 07:51:42 +0000997 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
998 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
999 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001000
Evan Chenge8916542011-08-30 01:34:54 +00001001 case ARMISD::ADDC: return "ARMISD::ADDC";
1002 case ARMISD::ADDE: return "ARMISD::ADDE";
1003 case ARMISD::SUBC: return "ARMISD::SUBC";
1004 case ARMISD::SUBE: return "ARMISD::SUBE";
1005
Bob Wilson22806742010-09-22 22:09:21 +00001006 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1007 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001008
Evan Chengec6d7c92009-10-28 06:55:03 +00001009 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1010 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1011
Dale Johannesend679ff72010-06-03 21:09:53 +00001012 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001013
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001014 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001015
Evan Chengb972e562009-08-07 00:34:42 +00001016 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1017
Bob Wilson7ed59712010-10-30 00:54:37 +00001018 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001019
Evan Cheng8740ee32010-11-03 06:34:55 +00001020 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1021
Bob Wilson2e076c42009-06-22 23:27:02 +00001022 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001023 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001024 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001025 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1026 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001027 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1028 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001029 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1030 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001031 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1032 case ARMISD::VTST: return "ARMISD::VTST";
1033
1034 case ARMISD::VSHL: return "ARMISD::VSHL";
1035 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1036 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001037 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1038 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1039 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1040 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1041 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1042 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1043 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1044 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1045 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1046 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1047 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1048 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1049 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1050 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001051 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001052 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001053 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001054 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001055 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001056 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001057 case ARMISD::VREV64: return "ARMISD::VREV64";
1058 case ARMISD::VREV32: return "ARMISD::VREV32";
1059 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001060 case ARMISD::VZIP: return "ARMISD::VZIP";
1061 case ARMISD::VUZP: return "ARMISD::VUZP";
1062 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001063 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1064 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001065 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1066 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001067 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1068 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001069 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001070 case ARMISD::FMAX: return "ARMISD::FMAX";
1071 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001072 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1073 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001074 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001075 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1076 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001077 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001078 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1079 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1080 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001081 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1082 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1083 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1084 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1085 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1086 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1087 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1088 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1089 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1090 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1091 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1092 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1093 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1094 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1095 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1096 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1097 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001098 }
1099}
1100
Matt Arsenault758659232013-05-18 00:21:46 +00001101EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001102 if (!VT.isVector()) return getPointerTy();
1103 return VT.changeVectorElementTypeToInteger();
1104}
1105
Evan Cheng4cad68e2010-05-15 02:18:07 +00001106/// getRegClassFor - Return the register class that should be used for the
1107/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001108const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001109 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1110 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1111 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001112 if (Subtarget->hasNEON()) {
1113 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001114 return &ARM::QQPRRegClass;
1115 if (VT == MVT::v8i64)
1116 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001117 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001118 return TargetLowering::getRegClassFor(VT);
1119}
1120
Eric Christopher84bdfd82010-07-21 22:26:11 +00001121// Create a fast isel object.
1122FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001123ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1124 const TargetLibraryInfo *libInfo) const {
1125 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001126}
1127
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001128/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1129/// be used for loads / stores from the global.
1130unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1131 return (Subtarget->isThumb1Only() ? 127 : 4095);
1132}
1133
Evan Cheng4401f882010-05-20 23:26:43 +00001134Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001135 unsigned NumVals = N->getNumValues();
1136 if (!NumVals)
1137 return Sched::RegPressure;
1138
1139 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001140 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001141 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001142 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001143 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001144 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001145 }
Evan Chengbf914992010-05-28 23:25:23 +00001146
1147 if (!N->isMachineOpcode())
1148 return Sched::RegPressure;
1149
1150 // Load are scheduled for latency even if there instruction itinerary
1151 // is not available.
1152 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001153 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001154
Evan Cheng6cc775f2011-06-28 19:10:37 +00001155 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001156 return Sched::RegPressure;
1157 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001158 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001159 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001160
Evan Cheng4401f882010-05-20 23:26:43 +00001161 return Sched::RegPressure;
1162}
1163
Evan Cheng10043e22007-01-19 07:51:42 +00001164//===----------------------------------------------------------------------===//
1165// Lowering Code
1166//===----------------------------------------------------------------------===//
1167
Evan Cheng10043e22007-01-19 07:51:42 +00001168/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1169static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1170 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001171 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001172 case ISD::SETNE: return ARMCC::NE;
1173 case ISD::SETEQ: return ARMCC::EQ;
1174 case ISD::SETGT: return ARMCC::GT;
1175 case ISD::SETGE: return ARMCC::GE;
1176 case ISD::SETLT: return ARMCC::LT;
1177 case ISD::SETLE: return ARMCC::LE;
1178 case ISD::SETUGT: return ARMCC::HI;
1179 case ISD::SETUGE: return ARMCC::HS;
1180 case ISD::SETULT: return ARMCC::LO;
1181 case ISD::SETULE: return ARMCC::LS;
1182 }
1183}
1184
Bob Wilsona2e83332009-09-09 23:14:54 +00001185/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1186static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001187 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001188 CondCode2 = ARMCC::AL;
1189 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001190 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001191 case ISD::SETEQ:
1192 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1193 case ISD::SETGT:
1194 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1195 case ISD::SETGE:
1196 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1197 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001198 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001199 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1200 case ISD::SETO: CondCode = ARMCC::VC; break;
1201 case ISD::SETUO: CondCode = ARMCC::VS; break;
1202 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1203 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1204 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1205 case ISD::SETLT:
1206 case ISD::SETULT: CondCode = ARMCC::LT; break;
1207 case ISD::SETLE:
1208 case ISD::SETULE: CondCode = ARMCC::LE; break;
1209 case ISD::SETNE:
1210 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1211 }
Evan Cheng10043e22007-01-19 07:51:42 +00001212}
1213
Bob Wilsona4c22902009-04-17 19:07:39 +00001214//===----------------------------------------------------------------------===//
1215// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001216//===----------------------------------------------------------------------===//
1217
1218#include "ARMGenCallingConv.inc"
1219
Oliver Stannardc24f2172014-05-09 14:01:47 +00001220/// getEffectiveCallingConv - Get the effective calling convention, taking into
1221/// account presence of floating point hardware and calling convention
1222/// limitations, such as support for variadic functions.
1223CallingConv::ID
1224ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1225 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001226 switch (CC) {
1227 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001228 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001229 case CallingConv::ARM_AAPCS:
1230 case CallingConv::ARM_APCS:
1231 case CallingConv::GHC:
1232 return CC;
1233 case CallingConv::ARM_AAPCS_VFP:
1234 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1235 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001236 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001237 return CallingConv::ARM_APCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001238 else if (Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001239 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1240 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001241 return CallingConv::ARM_AAPCS_VFP;
1242 else
1243 return CallingConv::ARM_AAPCS;
1244 case CallingConv::Fast:
1245 if (!Subtarget->isAAPCS_ABI()) {
1246 if (Subtarget->hasVFP2() && !isVarArg)
1247 return CallingConv::Fast;
1248 return CallingConv::ARM_APCS;
1249 } else if (Subtarget->hasVFP2() && !isVarArg)
1250 return CallingConv::ARM_AAPCS_VFP;
1251 else
1252 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001253 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001254}
1255
1256/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1257/// CallingConvention.
1258CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1259 bool Return,
1260 bool isVarArg) const {
1261 switch (getEffectiveCallingConv(CC, isVarArg)) {
1262 default:
1263 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001264 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001265 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001266 case CallingConv::ARM_AAPCS:
1267 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1268 case CallingConv::ARM_AAPCS_VFP:
1269 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1270 case CallingConv::Fast:
1271 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001272 case CallingConv::GHC:
1273 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001274 }
1275}
1276
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001277/// LowerCallResult - Lower the result values of a call into the
1278/// appropriate copies out of appropriate physical registers.
1279SDValue
1280ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001281 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001282 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001283 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001284 SmallVectorImpl<SDValue> &InVals,
1285 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001286
Bob Wilsona4c22902009-04-17 19:07:39 +00001287 // Assign locations to each value returned by this call.
1288 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001289 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1290 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001291 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001292 CCAssignFnForNode(CallConv, /* Return*/ true,
1293 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001294
1295 // Copy all of the result registers out of their specified physreg.
1296 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1297 CCValAssign VA = RVLocs[i];
1298
Stephen Linb8bd2322013-04-20 05:14:40 +00001299 // Pass 'this' value directly from the argument to return value, to avoid
1300 // reg unit interference
1301 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001302 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1303 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001304 InVals.push_back(ThisVal);
1305 continue;
1306 }
1307
Bob Wilson0041bd32009-04-25 00:33:20 +00001308 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001309 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001310 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001311 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001312 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001313 Chain = Lo.getValue(1);
1314 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001315 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001316 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001317 InFlag);
1318 Chain = Hi.getValue(1);
1319 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001320 if (!Subtarget->isLittle())
1321 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001322 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001323
Owen Anderson9f944592009-08-11 20:47:22 +00001324 if (VA.getLocVT() == MVT::v2f64) {
1325 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1326 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1327 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001328
1329 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001330 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001331 Chain = Lo.getValue(1);
1332 InFlag = Lo.getValue(2);
1333 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001334 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001335 Chain = Hi.getValue(1);
1336 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001337 if (!Subtarget->isLittle())
1338 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001339 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001340 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1341 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001342 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001343 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001344 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1345 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001346 Chain = Val.getValue(1);
1347 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001348 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001349
1350 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001351 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001352 case CCValAssign::Full: break;
1353 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001354 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001355 break;
1356 }
1357
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001358 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001359 }
1360
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001361 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001362}
1363
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001364/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001365SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001366ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1367 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001368 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001369 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001370 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001371 unsigned LocMemOffset = VA.getLocMemOffset();
1372 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1373 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001374 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001375 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001376 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001377}
1378
Andrew Trickef9de2a2013-05-25 02:42:55 +00001379void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001380 SDValue Chain, SDValue &Arg,
1381 RegsToPassVector &RegsToPass,
1382 CCValAssign &VA, CCValAssign &NextVA,
1383 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001384 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001385 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001386
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001387 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001388 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001389 unsigned id = Subtarget->isLittle() ? 0 : 1;
1390 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001391
1392 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001393 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001394 else {
1395 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001396 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001397 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1398
Christian Pirkerb5728192014-05-08 14:06:24 +00001399 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001400 dl, DAG, NextVA,
1401 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001402 }
1403}
1404
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001405/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001406/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1407/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001408SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001409ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001410 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001411 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001412 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001413 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1414 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1415 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001416 SDValue Chain = CLI.Chain;
1417 SDValue Callee = CLI.Callee;
1418 bool &isTailCall = CLI.IsTailCall;
1419 CallingConv::ID CallConv = CLI.CallConv;
1420 bool doesNotRet = CLI.DoesNotReturn;
1421 bool isVarArg = CLI.IsVarArg;
1422
Dale Johannesend679ff72010-06-03 21:09:53 +00001423 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001424 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1425 bool isThisReturn = false;
1426 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001427
Bob Wilson8decdc42011-10-07 17:17:49 +00001428 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001429 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001430 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001431
Dale Johannesend679ff72010-06-03 21:09:53 +00001432 if (isTailCall) {
1433 // Check if it's really possible to do a tail call.
1434 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001435 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001436 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001437 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1438 report_fatal_error("failed to perform tail call elimination on a call "
1439 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001440 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1441 // detected sibcalls.
1442 if (isTailCall) {
1443 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001444 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001445 }
1446 }
Evan Cheng10043e22007-01-19 07:51:42 +00001447
Bob Wilsona4c22902009-04-17 19:07:39 +00001448 // Analyze operands of the call, assigning locations to each operand.
1449 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001450 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1451 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001452 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001453 CCAssignFnForNode(CallConv, /* Return*/ false,
1454 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001455
Bob Wilsona4c22902009-04-17 19:07:39 +00001456 // Get a count of how many bytes are to be pushed on the stack.
1457 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001458
Dale Johannesend679ff72010-06-03 21:09:53 +00001459 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001460 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001461 NumBytes = 0;
1462
Evan Cheng10043e22007-01-19 07:51:42 +00001463 // Adjust the stack pointer for the new arguments...
1464 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001465 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001466 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1467 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001468
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001469 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001470
Bob Wilson2e076c42009-06-22 23:27:02 +00001471 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001472 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001473
Bob Wilsona4c22902009-04-17 19:07:39 +00001474 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001475 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001476 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1477 i != e;
1478 ++i, ++realArgIdx) {
1479 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001480 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001481 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001482 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001483
Bob Wilsona4c22902009-04-17 19:07:39 +00001484 // Promote the value if needed.
1485 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001486 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001487 case CCValAssign::Full: break;
1488 case CCValAssign::SExt:
1489 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1490 break;
1491 case CCValAssign::ZExt:
1492 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1493 break;
1494 case CCValAssign::AExt:
1495 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1496 break;
1497 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001498 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001499 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001500 }
1501
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001502 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001503 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001504 if (VA.getLocVT() == MVT::v2f64) {
1505 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1506 DAG.getConstant(0, MVT::i32));
1507 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1508 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001509
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001510 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001511 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1512
1513 VA = ArgLocs[++i]; // skip ahead to next loc
1514 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001515 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001516 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1517 } else {
1518 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001519
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001520 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1521 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001522 }
1523 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001524 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001525 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001526 }
1527 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001528 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1529 assert(VA.getLocVT() == MVT::i32 &&
1530 "unexpected calling convention register assignment");
1531 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001532 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001533 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001534 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001535 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001536 } else if (isByVal) {
1537 assert(VA.isMemLoc());
1538 unsigned offset = 0;
1539
1540 // True if this byval aggregate will be split between registers
1541 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001542 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1543 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1544
1545 if (CurByValIdx < ByValArgsCount) {
1546
1547 unsigned RegBegin, RegEnd;
1548 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1549
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001550 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1551 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001552 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001553 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1554 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1555 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1556 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001557 false, false, false,
1558 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001559 MemOpChains.push_back(Load.getValue(1));
1560 RegsToPass.push_back(std::make_pair(j, Load));
1561 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001562
1563 // If parameter size outsides register area, "offset" value
1564 // helps us to calculate stack slot for remained part properly.
1565 offset = RegEnd - RegBegin;
1566
1567 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001568 }
1569
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001570 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001571 unsigned LocMemOffset = VA.getLocMemOffset();
1572 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1573 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1574 StkPtrOff);
1575 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1576 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1577 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1578 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001579 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001580
Manman Ren9f911162012-06-01 02:44:42 +00001581 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001582 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001583 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001584 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001585 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001586 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001587 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001588
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001589 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1590 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001591 }
Evan Cheng10043e22007-01-19 07:51:42 +00001592 }
1593
1594 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001595 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001596
1597 // Build a sequence of copy-to-reg nodes chained together with token chain
1598 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001599 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001600 // Tail call byval lowering might overwrite argument registers so in case of
1601 // tail call optimization the copies to registers are lowered later.
1602 if (!isTailCall)
1603 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1604 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1605 RegsToPass[i].second, InFlag);
1606 InFlag = Chain.getValue(1);
1607 }
Evan Cheng10043e22007-01-19 07:51:42 +00001608
Dale Johannesend679ff72010-06-03 21:09:53 +00001609 // For tail calls lower the arguments to the 'real' stack slot.
1610 if (isTailCall) {
1611 // Force all the incoming stack arguments to be loaded from the stack
1612 // before any new outgoing arguments are stored to the stack, because the
1613 // outgoing stack slots may alias the incoming argument stack slots, and
1614 // the alias isn't otherwise explicit. This is slightly more conservative
1615 // than necessary, because it means that each store effectively depends
1616 // on every argument instead of just those arguments it would clobber.
1617
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001618 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001619 InFlag = SDValue();
1620 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1621 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1622 RegsToPass[i].second, InFlag);
1623 InFlag = Chain.getValue(1);
1624 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001625 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001626 }
1627
Bill Wendling24c79f22008-09-16 21:48:12 +00001628 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1629 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1630 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001631 bool isDirect = false;
1632 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001633 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001634 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001635
1636 if (EnableARMLongCalls) {
1637 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1638 && "long-calls with non-static relocation model!");
1639 // Handle a global address or an external symbol. If it's not one of
1640 // those, the target's already in a register, so we don't need to do
1641 // anything extra.
1642 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001643 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001644 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001645 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001646 ARMConstantPoolValue *CPV =
1647 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1648
Jim Grosbach32bb3622010-04-14 22:28:31 +00001649 // Get the address of the callee into a register
1650 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1651 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1652 Callee = DAG.getLoad(getPointerTy(), dl,
1653 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001654 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001655 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001656 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1657 const char *Sym = S->getSymbol();
1658
1659 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001660 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001661 ARMConstantPoolValue *CPV =
1662 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1663 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001664 // Get the address of the callee into a register
1665 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1666 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1667 Callee = DAG.getLoad(getPointerTy(), dl,
1668 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001669 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001670 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001671 }
1672 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001673 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001674 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001675 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001676 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001677 getTargetMachine().getRelocationModel() != Reloc::Static;
1678 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc3c949b42007-06-19 21:05:09 +00001679 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001680 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001681 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001682 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001683 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001684 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
1685 DAG.getTargetGlobalAddress(GV, dl, getPointerTy()));
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001686 } else {
1687 // On ELF targets for PIC code, direct calls should go through the PLT
1688 unsigned OpFlags = 0;
1689 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001690 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001691 OpFlags = ARMII::MO_PLT;
1692 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1693 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001694 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001695 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001696 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001697 getTargetMachine().getRelocationModel() != Reloc::Static;
1698 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +00001699 // tBX takes a register source operand.
1700 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001701 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001702 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001703 ARMConstantPoolValue *CPV =
1704 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1705 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001706 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001707 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001708 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001709 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001710 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001711 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001712 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001713 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001714 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001715 } else {
1716 unsigned OpFlags = 0;
1717 // On ELF targets for PIC code, direct calls should go through the PLT
1718 if (Subtarget->isTargetELF() &&
1719 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1720 OpFlags = ARMII::MO_PLT;
1721 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1722 }
Evan Cheng10043e22007-01-19 07:51:42 +00001723 }
1724
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001725 // FIXME: handle tail calls differently.
1726 unsigned CallOpc;
Tim Northoverdee86042013-12-02 14:46:26 +00001727 bool HasMinSizeAttr = Subtarget->isMinSize();
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001728 if (Subtarget->isThumb()) {
1729 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001730 CallOpc = ARMISD::CALL_NOLINK;
1731 else
1732 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1733 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001734 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001735 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001736 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001737 // Emit regular call when code size is the priority
1738 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001739 // "mov lr, pc; b _foo" to avoid confusing the RSP
1740 CallOpc = ARMISD::CALL_NOLINK;
1741 else
1742 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001743 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001744
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001745 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001746 Ops.push_back(Chain);
1747 Ops.push_back(Callee);
1748
1749 // Add argument registers to the end of the list so that they are known live
1750 // into the call.
1751 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1752 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1753 RegsToPass[i].second.getValueType()));
1754
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001755 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001756 if (!isTailCall) {
1757 const uint32_t *Mask;
1758 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1759 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1760 if (isThisReturn) {
1761 // For 'this' returns, use the R0-preserving mask if applicable
1762 Mask = ARI->getThisReturnPreservedMask(CallConv);
1763 if (!Mask) {
1764 // Set isThisReturn to false if the calling convention is not one that
1765 // allows 'returned' to be modeled in this way, so LowerCallResult does
1766 // not try to pass 'this' straight through
1767 isThisReturn = false;
1768 Mask = ARI->getCallPreservedMask(CallConv);
1769 }
1770 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001771 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001772
Matthias Braunc22630e2013-10-04 16:52:54 +00001773 assert(Mask && "Missing call preserved mask for calling convention");
1774 Ops.push_back(DAG.getRegisterMask(Mask));
1775 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001776
Gabor Greiff304a7a2008-08-28 21:40:38 +00001777 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001778 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001779
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001780 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001781 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001782 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001783
Duncan Sands739a0542008-07-02 17:40:58 +00001784 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001785 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001786 InFlag = Chain.getValue(1);
1787
Chris Lattner27539552008-10-11 22:08:30 +00001788 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001789 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001790 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001791 InFlag = Chain.getValue(1);
1792
Bob Wilsona4c22902009-04-17 19:07:39 +00001793 // Handle result values, copying them out of physregs into vregs that we
1794 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001795 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001796 InVals, isThisReturn,
1797 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001798}
1799
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001800/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001801/// on the stack. Remember the next parameter register to allocate,
1802/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001803/// this.
1804void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001805ARMTargetLowering::HandleByVal(
1806 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001807 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1808 assert((State->getCallOrPrologue() == Prologue ||
1809 State->getCallOrPrologue() == Call) &&
1810 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001811
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001812 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001813 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1814 unsigned AlignInRegs = Align / 4;
1815 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1816 for (unsigned i = 0; i < Waste; ++i)
1817 reg = State->AllocateReg(GPRArgRegs, 4);
1818 }
1819 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001820 unsigned excess = 4 * (ARM::R4 - reg);
1821
1822 // Special case when NSAA != SP and parameter size greater than size of
1823 // all remained GPR regs. In that case we can't split parameter, we must
1824 // send it to stack. We also must set NCRN to R4, so waste all
1825 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001826 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001827 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1828 while (State->AllocateReg(GPRArgRegs, 4))
1829 ;
1830 return;
1831 }
1832
1833 // First register for byval parameter is the first register that wasn't
1834 // allocated before this method call, so it would be "reg".
1835 // If parameter is small enough to be saved in range [reg, r4), then
1836 // the end (first after last) register would be reg + param-size-in-regs,
1837 // else parameter would be splitted between registers and stack,
1838 // end register would be r4 in this case.
1839 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001840 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001841 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1842 // Note, first register is allocated in the beginning of function already,
1843 // allocate remained amount of registers we need.
1844 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1845 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001846 // A byval parameter that is split between registers and memory needs its
1847 // size truncated here.
1848 // In the case where the entire structure fits in registers, we set the
1849 // size in memory to zero.
1850 if (size < excess)
1851 size = 0;
1852 else
1853 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001854 }
1855 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001856}
1857
Dale Johannesend679ff72010-06-03 21:09:53 +00001858/// MatchingStackOffset - Return true if the given stack call argument is
1859/// already available in the same position (relatively) of the caller's
1860/// incoming argument stack.
1861static
1862bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1863 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001864 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001865 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1866 int FI = INT_MAX;
1867 if (Arg.getOpcode() == ISD::CopyFromReg) {
1868 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001869 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001870 return false;
1871 MachineInstr *Def = MRI->getVRegDef(VR);
1872 if (!Def)
1873 return false;
1874 if (!Flags.isByVal()) {
1875 if (!TII->isLoadFromStackSlot(Def, FI))
1876 return false;
1877 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001878 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001879 }
1880 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1881 if (Flags.isByVal())
1882 // ByVal argument is passed in as a pointer but it's now being
1883 // dereferenced. e.g.
1884 // define @foo(%struct.X* %A) {
1885 // tail call @bar(%struct.X* byval %A)
1886 // }
1887 return false;
1888 SDValue Ptr = Ld->getBasePtr();
1889 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1890 if (!FINode)
1891 return false;
1892 FI = FINode->getIndex();
1893 } else
1894 return false;
1895
1896 assert(FI != INT_MAX);
1897 if (!MFI->isFixedObjectIndex(FI))
1898 return false;
1899 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1900}
1901
1902/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1903/// for tail call optimization. Targets which want to do tail call
1904/// optimization should implement this function.
1905bool
1906ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1907 CallingConv::ID CalleeCC,
1908 bool isVarArg,
1909 bool isCalleeStructRet,
1910 bool isCallerStructRet,
1911 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001912 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001913 const SmallVectorImpl<ISD::InputArg> &Ins,
1914 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001915 const Function *CallerF = DAG.getMachineFunction().getFunction();
1916 CallingConv::ID CallerCC = CallerF->getCallingConv();
1917 bool CCMatch = CallerCC == CalleeCC;
1918
1919 // Look for obvious safe cases to perform tail call optimization that do not
1920 // require ABI changes. This is what gcc calls sibcall.
1921
Jim Grosbache3864cc2010-06-16 23:45:49 +00001922 // Do not sibcall optimize vararg calls unless the call site is not passing
1923 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001924 if (isVarArg && !Outs.empty())
1925 return false;
1926
Tim Northoverd8407452013-10-01 14:33:28 +00001927 // Exception-handling functions need a special set of instructions to indicate
1928 // a return to the hardware. Tail-calling another function would probably
1929 // break this.
1930 if (CallerF->hasFnAttribute("interrupt"))
1931 return false;
1932
Dale Johannesend679ff72010-06-03 21:09:53 +00001933 // Also avoid sibcall optimization if either caller or callee uses struct
1934 // return semantics.
1935 if (isCalleeStructRet || isCallerStructRet)
1936 return false;
1937
Dale Johannesend24c66b2010-06-23 18:52:34 +00001938 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001939 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1940 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1941 // support in the assembler and linker to be used. This would need to be
1942 // fixed to fully support tail calls in Thumb1.
1943 //
Dale Johannesene2289282010-07-08 01:18:23 +00001944 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1945 // LR. This means if we need to reload LR, it takes an extra instructions,
1946 // which outweighs the value of the tail call; but here we don't know yet
1947 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001948 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001949 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001950
1951 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1952 // but we need to make sure there are enough registers; the only valid
1953 // registers are the 4 used for parameters. We don't currently do this
1954 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001955 if (Subtarget->isThumb1Only())
1956 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001957
Dale Johannesend679ff72010-06-03 21:09:53 +00001958 // If the calling conventions do not match, then we'd better make sure the
1959 // results are returned in the same way as what the caller expects.
1960 if (!CCMatch) {
1961 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwarich89019782011-06-10 20:59:24 +00001962 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1963 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001964 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1965
1966 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwarich89019782011-06-10 20:59:24 +00001967 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1968 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001969 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1970
1971 if (RVLocs1.size() != RVLocs2.size())
1972 return false;
1973 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1974 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1975 return false;
1976 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1977 return false;
1978 if (RVLocs1[i].isRegLoc()) {
1979 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1980 return false;
1981 } else {
1982 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1983 return false;
1984 }
1985 }
1986 }
1987
Manman Ren7e48b252012-10-12 23:39:43 +00001988 // If Caller's vararg or byval argument has been split between registers and
1989 // stack, do not perform tail call, since part of the argument is in caller's
1990 // local frame.
1991 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1992 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001993 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00001994 return false;
1995
Dale Johannesend679ff72010-06-03 21:09:53 +00001996 // If the callee takes no arguments then go on to check the results of the
1997 // call.
1998 if (!Outs.empty()) {
1999 // Check if stack adjustment is needed. For now, do not do this if any
2000 // argument is passed on the stack.
2001 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002002 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2003 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002004 CCInfo.AnalyzeCallOperands(Outs,
2005 CCAssignFnForNode(CalleeCC, false, isVarArg));
2006 if (CCInfo.getNextStackOffset()) {
2007 MachineFunction &MF = DAG.getMachineFunction();
2008
2009 // Check if the arguments are already laid out in the right way as
2010 // the caller's fixed stack objects.
2011 MachineFrameInfo *MFI = MF.getFrameInfo();
2012 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topper07720d82012-03-25 23:49:58 +00002013 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002014 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2015 i != e;
2016 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002017 CCValAssign &VA = ArgLocs[i];
2018 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002019 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002020 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002021 if (VA.getLocInfo() == CCValAssign::Indirect)
2022 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002023 if (VA.needsCustom()) {
2024 // f64 and vector types are split into multiple registers or
2025 // register/stack-slot combinations. The types will not match
2026 // the registers; give up on memory f64 refs until we figure
2027 // out what to do about this.
2028 if (!VA.isRegLoc())
2029 return false;
2030 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002031 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002032 if (RegVT == MVT::v2f64) {
2033 if (!ArgLocs[++i].isRegLoc())
2034 return false;
2035 if (!ArgLocs[++i].isRegLoc())
2036 return false;
2037 }
2038 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002039 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2040 MFI, MRI, TII))
2041 return false;
2042 }
2043 }
2044 }
2045 }
2046
2047 return true;
2048}
2049
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002050bool
2051ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2052 MachineFunction &MF, bool isVarArg,
2053 const SmallVectorImpl<ISD::OutputArg> &Outs,
2054 LLVMContext &Context) const {
2055 SmallVector<CCValAssign, 16> RVLocs;
2056 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2057 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2058 isVarArg));
2059}
2060
Tim Northoverd8407452013-10-01 14:33:28 +00002061static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2062 SDLoc DL, SelectionDAG &DAG) {
2063 const MachineFunction &MF = DAG.getMachineFunction();
2064 const Function *F = MF.getFunction();
2065
2066 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2067
2068 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2069 // version of the "preferred return address". These offsets affect the return
2070 // instruction if this is a return from PL1 without hypervisor extensions.
2071 // IRQ/FIQ: +4 "subs pc, lr, #4"
2072 // SWI: 0 "subs pc, lr, #0"
2073 // ABORT: +4 "subs pc, lr, #4"
2074 // UNDEF: +4/+2 "subs pc, lr, #0"
2075 // UNDEF varies depending on where the exception came from ARM or Thumb
2076 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2077
2078 int64_t LROffset;
2079 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2080 IntKind == "ABORT")
2081 LROffset = 4;
2082 else if (IntKind == "SWI" || IntKind == "UNDEF")
2083 LROffset = 0;
2084 else
2085 report_fatal_error("Unsupported interrupt attribute. If present, value "
2086 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2087
2088 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2089
Craig Topper48d114b2014-04-26 18:35:24 +00002090 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002091}
2092
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002093SDValue
2094ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002095 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002096 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002097 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002098 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002099
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002100 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002101 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002102
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002103 // CCState - Info about the registers and stack slots.
Cameron Zwarich89019782011-06-10 20:59:24 +00002104 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2105 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002106
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002107 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002108 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2109 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002110
Bob Wilsona4c22902009-04-17 19:07:39 +00002111 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002112 SmallVector<SDValue, 4> RetOps;
2113 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002114 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002115
2116 // Copy the result values into the output registers.
2117 for (unsigned i = 0, realRVLocIdx = 0;
2118 i != RVLocs.size();
2119 ++i, ++realRVLocIdx) {
2120 CCValAssign &VA = RVLocs[i];
2121 assert(VA.isRegLoc() && "Can only return in registers!");
2122
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002123 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002124
2125 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002126 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002127 case CCValAssign::Full: break;
2128 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002129 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002130 break;
2131 }
2132
Bob Wilsona4c22902009-04-17 19:07:39 +00002133 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002134 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002135 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002136 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2137 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002138 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002139 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002140
Christian Pirkerb5728192014-05-08 14:06:24 +00002141 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2142 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2143 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002144 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002145 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002146 VA = RVLocs[++i]; // skip ahead to next loc
2147 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002148 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2149 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002150 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002151 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002152 VA = RVLocs[++i]; // skip ahead to next loc
2153
2154 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002155 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2156 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002157 }
2158 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2159 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002160 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002161 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002162 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2163 fmrrd.getValue(isLittleEndian ? 0 : 1),
2164 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002165 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002166 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002167 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002168 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2169 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002170 Flag);
2171 } else
2172 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2173
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002174 // Guarantee that all emitted copies are
2175 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002176 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002177 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002178 }
2179
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002180 // Update chain and glue.
2181 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002182 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002183 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002184
Tim Northoverd8407452013-10-01 14:33:28 +00002185 // CPUs which aren't M-class use a special sequence to return from
2186 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2187 // though we use "subs pc, lr, #N").
2188 //
2189 // M-class CPUs actually use a normal return sequence with a special
2190 // (hardware-provided) value in LR, so the normal code path works.
2191 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2192 !Subtarget->isMClass()) {
2193 if (Subtarget->isThumb1Only())
2194 report_fatal_error("interrupt attribute is not supported in Thumb1");
2195 return LowerInterruptReturn(RetOps, dl, DAG);
2196 }
2197
Craig Topper48d114b2014-04-26 18:35:24 +00002198 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002199}
2200
Evan Chengf8bad082012-04-10 01:51:00 +00002201bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002202 if (N->getNumValues() != 1)
2203 return false;
2204 if (!N->hasNUsesOfValue(1, 0))
2205 return false;
2206
Evan Chengf8bad082012-04-10 01:51:00 +00002207 SDValue TCChain = Chain;
2208 SDNode *Copy = *N->use_begin();
2209 if (Copy->getOpcode() == ISD::CopyToReg) {
2210 // If the copy has a glue operand, we conservatively assume it isn't safe to
2211 // perform a tail call.
2212 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2213 return false;
2214 TCChain = Copy->getOperand(0);
2215 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2216 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002217 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002218 SmallPtrSet<SDNode*, 2> Copies;
2219 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002220 UI != UE; ++UI) {
2221 if (UI->getOpcode() != ISD::CopyToReg)
2222 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002223 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002224 }
Evan Chengf8bad082012-04-10 01:51:00 +00002225 if (Copies.size() > 2)
2226 return false;
2227
2228 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2229 UI != UE; ++UI) {
2230 SDValue UseChain = UI->getOperand(0);
2231 if (Copies.count(UseChain.getNode()))
2232 // Second CopyToReg
2233 Copy = *UI;
2234 else
2235 // First CopyToReg
2236 TCChain = UseChain;
2237 }
2238 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002239 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002240 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002241 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002242 Copy = *Copy->use_begin();
2243 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002244 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002245 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002246 } else {
2247 return false;
2248 }
2249
Evan Cheng419ea282010-12-01 22:59:46 +00002250 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002251 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2252 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002253 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2254 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002255 return false;
2256 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002257 }
2258
Evan Chengf8bad082012-04-10 01:51:00 +00002259 if (!HasRet)
2260 return false;
2261
2262 Chain = TCChain;
2263 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002264}
2265
Evan Cheng0663f232011-03-21 01:19:09 +00002266bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002267 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002268 return false;
2269
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002270 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002271 return false;
2272
2273 return !Subtarget->isThumb1Only();
2274}
2275
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002276// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2277// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2278// one of the above mentioned nodes. It has to be wrapped because otherwise
2279// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2280// be used to form addressing mode. These wrapped nodes will be selected
2281// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002282static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002283 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002284 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002285 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002286 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002287 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002288 if (CP->isMachineConstantPoolEntry())
2289 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2290 CP->getAlignment());
2291 else
2292 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2293 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002294 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002295}
2296
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002297unsigned ARMTargetLowering::getJumpTableEncoding() const {
2298 return MachineJumpTableInfo::EK_Inline;
2299}
2300
Dan Gohman21cea8a2010-04-17 15:26:15 +00002301SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2302 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002303 MachineFunction &MF = DAG.getMachineFunction();
2304 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2305 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002306 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002307 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002308 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002309 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2310 SDValue CPAddr;
2311 if (RelocM == Reloc::Static) {
2312 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2313 } else {
2314 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002315 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002316 ARMConstantPoolValue *CPV =
2317 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2318 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002319 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2320 }
2321 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2322 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002323 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002324 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002325 if (RelocM == Reloc::Static)
2326 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002327 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002328 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002329}
2330
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002331// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002332SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002333ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002334 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002335 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002336 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002337 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002338 MachineFunction &MF = DAG.getMachineFunction();
2339 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002340 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002341 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002342 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2343 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002344 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002345 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002346 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002347 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002348 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002349 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002350
Evan Cheng408aa562009-11-06 22:24:13 +00002351 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002352 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002353
2354 // call __tls_get_addr.
2355 ArgListTy Args;
2356 ArgListEntry Entry;
2357 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002358 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002359 Args.push_back(Entry);
Dale Johannesen555a3752009-01-30 23:10:59 +00002360 // FIXME: is there useful debug info available here?
Justin Holewinskiaa583972012-05-25 16:35:28 +00002361 TargetLowering::CallLoweringInfo CLI(Chain,
2362 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng09c070f2009-08-14 19:11:20 +00002363 false, false, false, false,
Evan Cheng65f9d192012-02-28 18:51:51 +00002364 0, CallingConv::C, /*isTailCall=*/false,
2365 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling78c5b7a2010-03-02 01:55:18 +00002366 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00002367 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002368 return CallResult.first;
2369}
2370
2371// Lower ISD::GlobalTLSAddress using the "initial exec" or
2372// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002373SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002374ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002375 SelectionDAG &DAG,
2376 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002377 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002378 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002379 SDValue Offset;
2380 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002381 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002382 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002383 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002384
Hans Wennborgaea41202012-05-04 09:40:39 +00002385 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002386 MachineFunction &MF = DAG.getMachineFunction();
2387 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002388 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002389 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002390 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2391 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002392 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2393 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2394 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002395 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002396 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002397 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002398 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002399 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002400 Chain = Offset.getValue(1);
2401
Evan Cheng408aa562009-11-06 22:24:13 +00002402 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002403 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002404
Evan Chengcdbb70c2009-10-31 03:39:36 +00002405 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002406 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002407 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002408 } else {
2409 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002410 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002411 ARMConstantPoolValue *CPV =
2412 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002413 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002414 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002415 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002416 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002417 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002418 }
2419
2420 // The address of the thread local variable is the add of the thread
2421 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002422 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002423}
2424
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002425SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002426ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002427 // TODO: implement the "local dynamic" model
2428 assert(Subtarget->isTargetELF() &&
2429 "TLS not implemented for non-ELF targets");
2430 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002431
2432 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2433
2434 switch (model) {
2435 case TLSModel::GeneralDynamic:
2436 case TLSModel::LocalDynamic:
2437 return LowerToTLSGeneralDynamicModel(GA, DAG);
2438 case TLSModel::InitialExec:
2439 case TLSModel::LocalExec:
2440 return LowerToTLSExecModels(GA, DAG, model);
2441 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002442 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002443}
2444
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002445SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002446 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002447 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002448 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002449 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002450 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002451 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002452 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002453 ARMConstantPoolConstant::Create(GV,
2454 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002455 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002456 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002457 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002458 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002459 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002460 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002461 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002462 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002463 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002464 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002465 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002466 MachinePointerInfo::getGOT(),
2467 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002468 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002469 }
2470
2471 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002472 // pair. This is always cheaper.
2473 if (Subtarget->useMovt()) {
Evan Cheng68aec142011-01-19 02:16:49 +00002474 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002475 // FIXME: Once remat is capable of dealing with instructions with register
2476 // operands, expand this into two nodes.
2477 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2478 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002479 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002480 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2481 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2482 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2483 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002484 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002485 }
2486}
2487
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002488SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002489 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002490 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002491 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002492 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002493 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002494
Tim Northover72360d22013-12-02 10:35:41 +00002495 if (Subtarget->useMovt())
Evan Cheng68aec142011-01-19 02:16:49 +00002496 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002497
Tim Northover72360d22013-12-02 10:35:41 +00002498 // FIXME: Once remat is capable of dealing with instructions with register
2499 // operands, expand this into multiple nodes
2500 unsigned Wrapper =
2501 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002502
Tim Northover72360d22013-12-02 10:35:41 +00002503 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2504 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002505
Evan Cheng1b389522009-09-03 07:04:02 +00002506 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002507 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2508 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002509 return Result;
2510}
2511
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002512SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2513 SelectionDAG &DAG) const {
2514 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
2515 assert(Subtarget->useMovt() && "Windows on ARM expects to use movw/movt");
2516
2517 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2518 EVT PtrVT = getPointerTy();
2519 SDLoc DL(Op);
2520
2521 ++NumMovwMovt;
2522
2523 // FIXME: Once remat is capable of dealing with instructions with register
2524 // operands, expand this into two nodes.
2525 return DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2526 DAG.getTargetGlobalAddress(GV, DL, PtrVT));
2527}
2528
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002529SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002530 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002531 assert(Subtarget->isTargetELF() &&
2532 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002533 MachineFunction &MF = DAG.getMachineFunction();
2534 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002535 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002536 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002537 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002538 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002539 ARMConstantPoolValue *CPV =
2540 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2541 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002542 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002543 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002544 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002545 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002546 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002547 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002548 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002549}
2550
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002551SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002552ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002553 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002554 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002555 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2556 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002557 Op.getOperand(1), Val);
2558}
2559
2560SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002561ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002562 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002563 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2564 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2565}
2566
2567SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002568ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002569 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002570 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002571 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002572 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002573 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson17f88782009-08-04 00:25:01 +00002574 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002575 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002576 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2577 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002578 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002579 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002580 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002581 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002582 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002583 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2584 SDValue CPAddr;
2585 unsigned PCAdj = (RelocM != Reloc::PIC_)
2586 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002587 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002588 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2589 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002590 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002591 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002592 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002593 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002594 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002595 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002596
2597 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002598 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002599 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2600 }
2601 return Result;
2602 }
Evan Cheng18381b42011-03-29 23:06:19 +00002603 case Intrinsic::arm_neon_vmulls:
2604 case Intrinsic::arm_neon_vmullu: {
2605 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2606 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002607 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002608 Op.getOperand(1), Op.getOperand(2));
2609 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002610 }
2611}
2612
Eli Friedman30a49e92011-08-03 21:06:02 +00002613static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2614 const ARMSubtarget *Subtarget) {
2615 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002616 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002617 if (!Subtarget->hasDataBarrier()) {
2618 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2619 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2620 // here.
2621 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002622 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002623 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002624 DAG.getConstant(0, MVT::i32));
2625 }
2626
Tim Northover36b24172013-07-03 09:20:36 +00002627 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2628 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2629 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002630 if (Subtarget->isMClass()) {
2631 // Only a full system barrier exists in the M-class architectures.
2632 Domain = ARM_MB::SY;
2633 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002634 // Swift happens to implement ISHST barriers in a way that's compatible with
2635 // Release semantics but weaker than ISH so we'd be fools not to use
2636 // it. Beware: other processors probably don't!
2637 Domain = ARM_MB::ISHST;
2638 }
2639
Joey Gouly926d3f52013-09-05 15:35:24 +00002640 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2641 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002642 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002643}
2644
Evan Cheng8740ee32010-11-03 06:34:55 +00002645static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2646 const ARMSubtarget *Subtarget) {
2647 // ARM pre v5TE and Thumb1 does not have preload instructions.
2648 if (!(Subtarget->isThumb2() ||
2649 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2650 // Just preserve the chain.
2651 return Op.getOperand(0);
2652
Andrew Trickef9de2a2013-05-25 02:42:55 +00002653 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002654 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2655 if (!isRead &&
2656 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2657 // ARMv7 with MP extension has PLDW.
2658 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002659
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002660 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2661 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002662 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002663 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002664 isData = ~isData & 1;
2665 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002666
2667 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002668 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2669 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002670}
2671
Dan Gohman31ae5862010-04-17 14:41:14 +00002672static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2673 MachineFunction &MF = DAG.getMachineFunction();
2674 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2675
Evan Cheng10043e22007-01-19 07:51:42 +00002676 // vastart just stores the address of the VarArgsFrameIndex slot into the
2677 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002678 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002679 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002680 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002681 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002682 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2683 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002684}
2685
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002686SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002687ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2688 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002689 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002690 MachineFunction &MF = DAG.getMachineFunction();
2691 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2692
Craig Topper760b1342012-02-22 05:59:10 +00002693 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002694 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002695 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002696 else
Craig Topperc7242e02012-04-20 07:30:17 +00002697 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002698
2699 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002700 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002701 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002702
2703 SDValue ArgValue2;
2704 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002705 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002706 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002707
2708 // Create load node to retrieve arguments from the stack.
2709 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002710 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002711 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002712 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002713 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002714 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002715 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002716 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002717 if (!Subtarget->isLittle())
2718 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002719 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002720}
2721
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002722void
2723ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002724 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002725 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002726 unsigned &ArgRegsSize,
2727 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002728 const {
2729 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002730 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2731 unsigned RBegin, REnd;
2732 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2733 NumGPRs = REnd - RBegin;
2734 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002735 unsigned int firstUnalloced;
2736 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2737 sizeof(GPRArgRegs) /
2738 sizeof(GPRArgRegs[0]));
2739 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2740 }
2741
2742 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002743 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002744
2745 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002746 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002747 (ArgRegsSize < ArgSize ||
2748 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002749 // Add padding for part of param recovered from GPRs. For example,
2750 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002751 // We need to do it, since remained (stack) part of parameter has
2752 // stack alignment, and we need to "attach" "GPRs head" without gaps
2753 // to it:
2754 // Stack:
2755 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2756 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2757 //
2758 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2759 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002760 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002761 ArgRegsSaveSize = ArgRegsSize + Padding;
2762 } else
2763 // We don't need to extend regs save size for byval parameters if they
2764 // are passed via GPRs only.
2765 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002766}
2767
2768// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002769// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002770// byval). Either way, we allocate stack slots adjacent to the data
2771// provided by our caller, and store the unallocated registers there.
2772// If this is a variadic function, the va_list pointer will begin with
2773// these values; otherwise, this reassembles a (byval) structure that
2774// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002775// Return: The frame index registers were stored into.
2776int
2777ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002778 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002779 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002780 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002781 unsigned OffsetFromOrigArg,
2782 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002783 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002784 bool ForceMutable,
2785 unsigned ByValStoreOffset,
2786 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002787
2788 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002789 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002790 // Setup first unallocated register as first byval register;
2791 // eat all remained registers
2792 // (these two actions are performed by HandleByVal method).
2793 // Then, here, we initialize stack frame with
2794 // "store-reg" instructions.
2795 // Case #2. Var-args function, that doesn't contain byval parameters.
2796 // The same: eat all remained unallocated registers,
2797 // initialize stack frame.
2798
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002799 MachineFunction &MF = DAG.getMachineFunction();
2800 MachineFrameInfo *MFI = MF.getFrameInfo();
2801 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002802 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2803 unsigned RBegin, REnd;
2804 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2805 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2806 firstRegToSaveIndex = RBegin - ARM::R0;
2807 lastRegToSaveIndex = REnd - ARM::R0;
2808 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002809 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002810 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002811 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002812 }
2813
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002814 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002815 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2816 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002817
2818 // Store any by-val regs to their spots on the stack so that they may be
2819 // loaded by deferencing the result of formal parameter pointer or va_next.
2820 // Note: once stack area for byval/varargs registers
2821 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002822 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002823 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2824
2825 if (Padding) {
2826 assert(AFI->getStoredByValParamsPadding() == 0 &&
2827 "The only parameter may be padded.");
2828 AFI->setStoredByValParamsPadding(Padding);
2829 }
2830
Oliver Stannardd55e1152014-03-05 15:25:27 +00002831 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2832 Padding +
2833 ByValStoreOffset -
2834 (int64_t)TotalArgRegsSaveSize,
2835 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002836 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002837 if (Padding) {
2838 MFI->CreateFixedObject(Padding,
2839 ArgOffset + ByValStoreOffset -
2840 (int64_t)ArgRegsSaveSize,
2841 false);
2842 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002843
2844 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002845 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2846 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002847 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002848 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002849 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002850 else
Craig Topperc7242e02012-04-20 07:30:17 +00002851 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002852
2853 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2854 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2855 SDValue Store =
2856 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002857 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002858 false, false, 0);
2859 MemOps.push_back(Store);
2860 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2861 DAG.getConstant(4, getPointerTy()));
2862 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002863
2864 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2865
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002866 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002867 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002868 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002869 } else {
2870 if (ArgSize == 0) {
2871 // We cannot allocate a zero-byte object for the first variadic argument,
2872 // so just make up a size.
2873 ArgSize = 4;
2874 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002875 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002876 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002877 ArgSize, ArgOffset, !ForceMutable);
2878 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002879}
2880
2881// Setup stack frame, the va_list pointer will start from.
2882void
2883ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002884 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002885 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002886 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002887 bool ForceMutable) const {
2888 MachineFunction &MF = DAG.getMachineFunction();
2889 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2890
2891 // Try to store any remaining integer argument regs
2892 // to their spots on the stack so that they may be loaded by deferencing
2893 // the result of va_next.
2894 // If there is no regs to be stored, just point address after last
2895 // argument passed via stack.
2896 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00002897 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2898 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
2899 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002900
2901 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002902}
2903
Bob Wilson2e076c42009-06-22 23:27:02 +00002904SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002905ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002906 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002907 const SmallVectorImpl<ISD::InputArg>
2908 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002909 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002910 SmallVectorImpl<SDValue> &InVals)
2911 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002912 MachineFunction &MF = DAG.getMachineFunction();
2913 MachineFrameInfo *MFI = MF.getFrameInfo();
2914
Bob Wilsona4c22902009-04-17 19:07:39 +00002915 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2916
2917 // Assign locations to all of the incoming arguments.
2918 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002919 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2920 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002921 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002922 CCAssignFnForNode(CallConv, /* Return*/ false,
2923 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002924
Bob Wilsona4c22902009-04-17 19:07:39 +00002925 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002926 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002927 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002928 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2929 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002930
2931 // Initially ArgRegsSaveSize is zero.
2932 // Then we increase this value each time we meet byval parameter.
2933 // We also increase this value in case of varargs function.
2934 AFI->setArgRegsSaveSize(0);
2935
Oliver Stannardd55e1152014-03-05 15:25:27 +00002936 unsigned ByValStoreOffset = 0;
2937 unsigned TotalArgRegsSaveSize = 0;
2938 unsigned ArgRegsSaveSizeMaxAlign = 4;
2939
2940 // Calculate the amount of stack space that we need to allocate to store
2941 // byval and variadic arguments that are passed in registers.
2942 // We need to know this before we allocate the first byval or variadic
2943 // argument, as they will be allocated a stack slot below the CFA (Canonical
2944 // Frame Address, the stack pointer at entry to the function).
2945 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2946 CCValAssign &VA = ArgLocs[i];
2947 if (VA.isMemLoc()) {
2948 int index = VA.getValNo();
2949 if (index != lastInsIndex) {
2950 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2951 if (Flags.isByVal()) {
2952 unsigned ExtraArgRegsSize;
2953 unsigned ExtraArgRegsSaveSize;
2954 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2955 Flags.getByValSize(),
2956 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2957
2958 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2959 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
2960 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
2961 CCInfo.nextInRegsParam();
2962 }
2963 lastInsIndex = index;
2964 }
2965 }
2966 }
2967 CCInfo.rewindByValRegsInfo();
2968 lastInsIndex = -1;
2969 if (isVarArg) {
2970 unsigned ExtraArgRegsSize;
2971 unsigned ExtraArgRegsSaveSize;
2972 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
2973 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2974 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2975 }
2976 // If the arg regs save area contains N-byte aligned values, the
2977 // bottom of it must be at least N-byte aligned.
2978 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
2979 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
2980
Bob Wilsona4c22902009-04-17 19:07:39 +00002981 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2982 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002983 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2984 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002985 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00002986 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002987 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00002988
Bob Wilsona4c22902009-04-17 19:07:39 +00002989 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002990 // f64 and vector types are split up into multiple registers or
2991 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00002992 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002993 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002994 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00002995 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00002996 SDValue ArgValue2;
2997 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00002998 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00002999 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3000 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003001 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003002 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003003 } else {
3004 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3005 Chain, DAG, dl);
3006 }
Owen Anderson9f944592009-08-11 20:47:22 +00003007 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3008 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003009 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00003010 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003011 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
3012 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003013 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003014
Bob Wilson2e076c42009-06-22 23:27:02 +00003015 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003016 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003017
Owen Anderson9f944592009-08-11 20:47:22 +00003018 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003019 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003020 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003021 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003022 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003023 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003024 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00003025 RC = AFI->isThumb1OnlyFunction() ?
3026 (const TargetRegisterClass*)&ARM::tGPRRegClass :
3027 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003028 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003029 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003030
3031 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003032 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003033 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003034 }
3035
3036 // If this is an 8 or 16-bit value, it is really passed promoted
3037 // to 32 bits. Insert an assert[sz]ext to capture this, then
3038 // truncate to the right size.
3039 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003040 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003041 case CCValAssign::Full: break;
3042 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003043 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003044 break;
3045 case CCValAssign::SExt:
3046 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3047 DAG.getValueType(VA.getValVT()));
3048 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3049 break;
3050 case CCValAssign::ZExt:
3051 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3052 DAG.getValueType(VA.getValVT()));
3053 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3054 break;
3055 }
3056
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003057 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003058
3059 } else { // VA.isRegLoc()
3060
3061 // sanity check
3062 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003063 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003064
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003065 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003066
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003067 // Some Ins[] entries become multiple ArgLoc[] entries.
3068 // Process them only once.
3069 if (index != lastInsIndex)
3070 {
3071 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003072 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003073 // This can be changed with more analysis.
3074 // In case of tail call optimization mark all arguments mutable.
3075 // Since they could be overwritten by lowering of arguments in case of
3076 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003077 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003078 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003079
3080 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003081 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003082 CCInfo, DAG, dl, Chain, CurOrigArg,
3083 CurByValIndex,
3084 Ins[VA.getValNo()].PartOffset,
3085 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003086 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003087 true /*force mutable frames*/,
3088 ByValStoreOffset,
3089 TotalArgRegsSaveSize);
3090 ByValStoreOffset += Flags.getByValSize();
3091 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003092 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003093 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003094 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003095 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003096 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003097 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003098
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003099 // Create load nodes to retrieve arguments from the stack.
3100 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3101 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3102 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003103 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003104 }
3105 lastInsIndex = index;
3106 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003107 }
3108 }
3109
3110 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003111 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003112 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003113 CCInfo.getNextStackOffset(),
3114 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003115
Oliver Stannardb14c6252014-04-02 16:10:33 +00003116 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3117
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003118 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003119}
3120
3121/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003122static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003123 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003124 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003125 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003126 // Maybe this has already been legalized into the constant pool?
3127 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003128 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003129 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003130 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003131 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003132 }
3133 }
3134 return false;
3135}
3136
Evan Cheng10043e22007-01-19 07:51:42 +00003137/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3138/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003139SDValue
3140ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003141 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003142 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003143 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003144 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003145 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003146 // Constant does not fit, try adjusting it by one?
3147 switch (CC) {
3148 default: break;
3149 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003150 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003151 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003152 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003153 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003154 }
3155 break;
3156 case ISD::SETULT:
3157 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003158 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003159 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003160 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003161 }
3162 break;
3163 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003164 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003165 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003166 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003167 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003168 }
3169 break;
3170 case ISD::SETULE:
3171 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003172 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003173 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003174 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003175 }
3176 break;
3177 }
3178 }
3179 }
3180
3181 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003182 ARMISD::NodeType CompareType;
3183 switch (CondCode) {
3184 default:
3185 CompareType = ARMISD::CMP;
3186 break;
3187 case ARMCC::EQ:
3188 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003189 // Uses only Z Flag
3190 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003191 break;
3192 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003193 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003194 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003195}
3196
3197/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003198SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003199ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003200 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003201 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003202 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003203 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003204 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003205 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3206 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003207}
3208
Bob Wilson45acbd02011-03-08 01:17:20 +00003209/// duplicateCmp - Glue values can have only one use, so this function
3210/// duplicates a comparison node.
3211SDValue
3212ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3213 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003214 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003215 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3216 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3217
3218 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3219 Cmp = Cmp.getOperand(0);
3220 Opc = Cmp.getOpcode();
3221 if (Opc == ARMISD::CMPFP)
3222 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3223 else {
3224 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3225 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3226 }
3227 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3228}
3229
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003230std::pair<SDValue, SDValue>
3231ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3232 SDValue &ARMcc) const {
3233 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3234
3235 SDValue Value, OverflowCmp;
3236 SDValue LHS = Op.getOperand(0);
3237 SDValue RHS = Op.getOperand(1);
3238
3239
3240 // FIXME: We are currently always generating CMPs because we don't support
3241 // generating CMN through the backend. This is not as good as the natural
3242 // CMP case because it causes a register dependency and cannot be folded
3243 // later.
3244
3245 switch (Op.getOpcode()) {
3246 default:
3247 llvm_unreachable("Unknown overflow instruction!");
3248 case ISD::SADDO:
3249 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3250 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3251 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3252 break;
3253 case ISD::UADDO:
3254 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3255 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3256 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3257 break;
3258 case ISD::SSUBO:
3259 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3260 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3261 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3262 break;
3263 case ISD::USUBO:
3264 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3265 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3266 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3267 break;
3268 } // switch (...)
3269
3270 return std::make_pair(Value, OverflowCmp);
3271}
3272
3273
3274SDValue
3275ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3276 // Let legalize expand this if it isn't a legal type yet.
3277 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3278 return SDValue();
3279
3280 SDValue Value, OverflowCmp;
3281 SDValue ARMcc;
3282 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3283 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3284 // We use 0 and 1 as false and true values.
3285 SDValue TVal = DAG.getConstant(1, MVT::i32);
3286 SDValue FVal = DAG.getConstant(0, MVT::i32);
3287 EVT VT = Op.getValueType();
3288
3289 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3290 ARMcc, CCR, OverflowCmp);
3291
3292 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3293 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3294}
3295
3296
Bill Wendling6a981312010-08-11 08:43:16 +00003297SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3298 SDValue Cond = Op.getOperand(0);
3299 SDValue SelectTrue = Op.getOperand(1);
3300 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003301 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003302 unsigned Opc = Cond.getOpcode();
3303
3304 if (Cond.getResNo() == 1 &&
3305 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3306 Opc == ISD::USUBO)) {
3307 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3308 return SDValue();
3309
3310 SDValue Value, OverflowCmp;
3311 SDValue ARMcc;
3312 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3313 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3314 EVT VT = Op.getValueType();
3315
3316 return DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, SelectTrue, SelectFalse,
3317 ARMcc, CCR, OverflowCmp);
3318
3319 }
Bill Wendling6a981312010-08-11 08:43:16 +00003320
3321 // Convert:
3322 //
3323 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3324 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3325 //
3326 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3327 const ConstantSDNode *CMOVTrue =
3328 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3329 const ConstantSDNode *CMOVFalse =
3330 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3331
3332 if (CMOVTrue && CMOVFalse) {
3333 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3334 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3335
3336 SDValue True;
3337 SDValue False;
3338 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3339 True = SelectTrue;
3340 False = SelectFalse;
3341 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3342 True = SelectFalse;
3343 False = SelectTrue;
3344 }
3345
3346 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003347 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003348 SDValue ARMcc = Cond.getOperand(2);
3349 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003350 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003351 assert(True.getValueType() == VT);
3352 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003353 }
3354 }
3355 }
3356
Dan Gohmand4a77c42012-02-24 00:09:36 +00003357 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3358 // undefined bits before doing a full-word comparison with zero.
3359 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3360 DAG.getConstant(1, Cond.getValueType()));
3361
Bill Wendling6a981312010-08-11 08:43:16 +00003362 return DAG.getSelectCC(dl, Cond,
3363 DAG.getConstant(0, Cond.getValueType()),
3364 SelectTrue, SelectFalse, ISD::SETNE);
3365}
3366
Joey Gouly881eab52013-08-22 15:29:11 +00003367static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3368 if (CC == ISD::SETNE)
3369 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003370 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003371}
3372
3373static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3374 bool &swpCmpOps, bool &swpVselOps) {
3375 // Start by selecting the GE condition code for opcodes that return true for
3376 // 'equality'
3377 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3378 CC == ISD::SETULE)
3379 CondCode = ARMCC::GE;
3380
3381 // and GT for opcodes that return false for 'equality'.
3382 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3383 CC == ISD::SETULT)
3384 CondCode = ARMCC::GT;
3385
3386 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3387 // to swap the compare operands.
3388 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3389 CC == ISD::SETULT)
3390 swpCmpOps = true;
3391
3392 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3393 // If we have an unordered opcode, we need to swap the operands to the VSEL
3394 // instruction (effectively negating the condition).
3395 //
3396 // This also has the effect of swapping which one of 'less' or 'greater'
3397 // returns true, so we also swap the compare operands. It also switches
3398 // whether we return true for 'equality', so we compensate by picking the
3399 // opposite condition code to our original choice.
3400 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3401 CC == ISD::SETUGT) {
3402 swpCmpOps = !swpCmpOps;
3403 swpVselOps = !swpVselOps;
3404 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3405 }
3406
3407 // 'ordered' is 'anything but unordered', so use the VS condition code and
3408 // swap the VSEL operands.
3409 if (CC == ISD::SETO) {
3410 CondCode = ARMCC::VS;
3411 swpVselOps = true;
3412 }
3413
3414 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3415 // code and swap the VSEL operands.
3416 if (CC == ISD::SETUNE) {
3417 CondCode = ARMCC::EQ;
3418 swpVselOps = true;
3419 }
3420}
3421
Dan Gohman21cea8a2010-04-17 15:26:15 +00003422SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003423 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003424 SDValue LHS = Op.getOperand(0);
3425 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003426 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003427 SDValue TrueVal = Op.getOperand(2);
3428 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003429 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003430
Owen Anderson9f944592009-08-11 20:47:22 +00003431 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003432 // Try to generate VSEL on ARMv8.
3433 // The VSEL instruction can't use all the usual ARM condition
3434 // codes: it only has two bits to select the condition code, so it's
3435 // constrained to use only GE, GT, VS and EQ.
3436 //
3437 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3438 // swap the operands of the previous compare instruction (effectively
3439 // inverting the compare condition, swapping 'less' and 'greater') and
3440 // sometimes need to swap the operands to the VSEL (which inverts the
3441 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003442 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003443 TrueVal.getValueType() == MVT::f64)) {
3444 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3445 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3446 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3447 CC = getInverseCCForVSEL(CC);
3448 std::swap(TrueVal, FalseVal);
3449 }
3450 }
3451
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003452 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003453 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003454 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003455 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3456 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003457 }
3458
3459 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003460 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003461
Joey Gouly881eab52013-08-22 15:29:11 +00003462 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003463 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003464 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003465 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3466 // same operands, as follows:
3467 // c = fcmp [ogt, olt, ugt, ult] a, b
3468 // select c, a, b
3469 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3470 // handled differently than the original code sequence.
3471 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3472 RHS == FalseVal) {
3473 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3474 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3475 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3476 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3477 }
3478
Joey Gouly881eab52013-08-22 15:29:11 +00003479 bool swpCmpOps = false;
3480 bool swpVselOps = false;
3481 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3482
3483 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3484 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3485 if (swpCmpOps)
3486 std::swap(LHS, RHS);
3487 if (swpVselOps)
3488 std::swap(TrueVal, FalseVal);
3489 }
3490 }
3491
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003492 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3493 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003494 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003495 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003496 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003497 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003498 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003499 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003500 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003501 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003502 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003503 }
3504 return Result;
3505}
3506
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003507/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3508/// to morph to an integer compare sequence.
3509static bool canChangeToInt(SDValue Op, bool &SeenZero,
3510 const ARMSubtarget *Subtarget) {
3511 SDNode *N = Op.getNode();
3512 if (!N->hasOneUse())
3513 // Otherwise it requires moving the value from fp to integer registers.
3514 return false;
3515 if (!N->getNumValues())
3516 return false;
3517 EVT VT = Op.getValueType();
3518 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3519 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3520 // vmrs are very slow, e.g. cortex-a8.
3521 return false;
3522
3523 if (isFloatingPointZero(Op)) {
3524 SeenZero = true;
3525 return true;
3526 }
3527 return ISD::isNormalLoad(N);
3528}
3529
3530static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3531 if (isFloatingPointZero(Op))
3532 return DAG.getConstant(0, MVT::i32);
3533
3534 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003535 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003536 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003537 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003538 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003539
3540 llvm_unreachable("Unknown VFP cmp argument!");
3541}
3542
3543static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3544 SDValue &RetVal1, SDValue &RetVal2) {
3545 if (isFloatingPointZero(Op)) {
3546 RetVal1 = DAG.getConstant(0, MVT::i32);
3547 RetVal2 = DAG.getConstant(0, MVT::i32);
3548 return;
3549 }
3550
3551 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3552 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003553 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003554 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003555 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003556 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003557 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003558
3559 EVT PtrType = Ptr.getValueType();
3560 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003561 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003562 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003563 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003564 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003565 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003566 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003567 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003568 return;
3569 }
3570
3571 llvm_unreachable("Unknown VFP cmp argument!");
3572}
3573
3574/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3575/// f32 and even f64 comparisons to integer ones.
3576SDValue
3577ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3578 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003579 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003580 SDValue LHS = Op.getOperand(2);
3581 SDValue RHS = Op.getOperand(3);
3582 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003583 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003584
Evan Chengd12af5d2012-03-01 23:27:13 +00003585 bool LHSSeenZero = false;
3586 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3587 bool RHSSeenZero = false;
3588 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3589 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003590 // If unsafe fp math optimization is enabled and there are no other uses of
3591 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003592 // to an integer comparison.
3593 if (CC == ISD::SETOEQ)
3594 CC = ISD::SETEQ;
3595 else if (CC == ISD::SETUNE)
3596 CC = ISD::SETNE;
3597
Evan Chengd12af5d2012-03-01 23:27:13 +00003598 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003599 SDValue ARMcc;
3600 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003601 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3602 bitcastf32Toi32(LHS, DAG), Mask);
3603 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3604 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003605 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3606 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3607 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3608 Chain, Dest, ARMcc, CCR, Cmp);
3609 }
3610
3611 SDValue LHS1, LHS2;
3612 SDValue RHS1, RHS2;
3613 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3614 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003615 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3616 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003617 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3618 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003619 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003620 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003621 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003622 }
3623
3624 return SDValue();
3625}
3626
3627SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3628 SDValue Chain = Op.getOperand(0);
3629 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3630 SDValue LHS = Op.getOperand(2);
3631 SDValue RHS = Op.getOperand(3);
3632 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003633 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003634
Owen Anderson9f944592009-08-11 20:47:22 +00003635 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003636 SDValue ARMcc;
3637 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003638 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003639 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003640 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003641 }
3642
Owen Anderson9f944592009-08-11 20:47:22 +00003643 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003644
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003645 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003646 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3647 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3648 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3649 if (Result.getNode())
3650 return Result;
3651 }
3652
Evan Cheng10043e22007-01-19 07:51:42 +00003653 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003654 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003655
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003656 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3657 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003658 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003659 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003660 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003661 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003662 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003663 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3664 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003665 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003666 }
3667 return Res;
3668}
3669
Dan Gohman21cea8a2010-04-17 15:26:15 +00003670SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003671 SDValue Chain = Op.getOperand(0);
3672 SDValue Table = Op.getOperand(1);
3673 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003674 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003675
Owen Anderson53aa7a92009-08-10 22:56:29 +00003676 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003677 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3678 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003679 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003680 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003681 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003682 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3683 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003684 if (Subtarget->isThumb2()) {
3685 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3686 // which does another jump to the destination. This also makes it easier
3687 // to translate it to TBB / TBH later.
3688 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003689 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003690 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003691 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003692 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003693 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003694 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003695 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003696 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003697 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003698 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003699 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003700 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003701 MachinePointerInfo::getJumpTable(),
3702 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003703 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003704 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003705 }
Evan Cheng10043e22007-01-19 07:51:42 +00003706}
3707
Eli Friedman2d4055b2011-11-09 23:36:02 +00003708static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003709 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003710 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003711
James Molloy547d4c02012-02-20 09:24:05 +00003712 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3713 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3714 return Op;
3715 return DAG.UnrollVectorOp(Op.getNode());
3716 }
3717
3718 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3719 "Invalid type for custom lowering!");
3720 if (VT != MVT::v4i16)
3721 return DAG.UnrollVectorOp(Op.getNode());
3722
3723 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3724 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003725}
3726
Bob Wilsone4191e72010-03-19 22:51:32 +00003727static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003728 EVT VT = Op.getValueType();
3729 if (VT.isVector())
3730 return LowerVectorFP_TO_INT(Op, DAG);
3731
Andrew Trickef9de2a2013-05-25 02:42:55 +00003732 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003733 unsigned Opc;
3734
3735 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003736 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003737 case ISD::FP_TO_SINT:
3738 Opc = ARMISD::FTOSI;
3739 break;
3740 case ISD::FP_TO_UINT:
3741 Opc = ARMISD::FTOUI;
3742 break;
3743 }
3744 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003745 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003746}
3747
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003748static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3749 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003750 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003751
Eli Friedman2d4055b2011-11-09 23:36:02 +00003752 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3753 if (VT.getVectorElementType() == MVT::f32)
3754 return Op;
3755 return DAG.UnrollVectorOp(Op.getNode());
3756 }
3757
Duncan Sandsa41634e2011-08-12 14:54:45 +00003758 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3759 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003760 if (VT != MVT::v4f32)
3761 return DAG.UnrollVectorOp(Op.getNode());
3762
3763 unsigned CastOpc;
3764 unsigned Opc;
3765 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003766 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003767 case ISD::SINT_TO_FP:
3768 CastOpc = ISD::SIGN_EXTEND;
3769 Opc = ISD::SINT_TO_FP;
3770 break;
3771 case ISD::UINT_TO_FP:
3772 CastOpc = ISD::ZERO_EXTEND;
3773 Opc = ISD::UINT_TO_FP;
3774 break;
3775 }
3776
3777 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3778 return DAG.getNode(Opc, dl, VT, Op);
3779}
3780
Bob Wilsone4191e72010-03-19 22:51:32 +00003781static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3782 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003783 if (VT.isVector())
3784 return LowerVectorINT_TO_FP(Op, DAG);
3785
Andrew Trickef9de2a2013-05-25 02:42:55 +00003786 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003787 unsigned Opc;
3788
3789 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003790 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003791 case ISD::SINT_TO_FP:
3792 Opc = ARMISD::SITOF;
3793 break;
3794 case ISD::UINT_TO_FP:
3795 Opc = ARMISD::UITOF;
3796 break;
3797 }
3798
Wesley Peck527da1b2010-11-23 03:31:01 +00003799 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003800 return DAG.getNode(Opc, dl, VT, Op);
3801}
3802
Evan Cheng25f93642010-07-08 02:08:50 +00003803SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003804 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003805 SDValue Tmp0 = Op.getOperand(0);
3806 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003807 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003808 EVT VT = Op.getValueType();
3809 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003810 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3811 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3812 bool UseNEON = !InGPR && Subtarget->hasNEON();
3813
3814 if (UseNEON) {
3815 // Use VBSL to copy the sign bit.
3816 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3817 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3818 DAG.getTargetConstant(EncodedVal, MVT::i32));
3819 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3820 if (VT == MVT::f64)
3821 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3822 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3823 DAG.getConstant(32, MVT::i32));
3824 else /*if (VT == MVT::f32)*/
3825 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3826 if (SrcVT == MVT::f32) {
3827 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3828 if (VT == MVT::f64)
3829 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3830 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3831 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003832 } else if (VT == MVT::f32)
3833 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3834 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3835 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003836 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3837 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3838
3839 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3840 MVT::i32);
3841 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3842 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3843 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003844
Evan Chengd6b641e2011-02-23 02:24:55 +00003845 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3846 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3847 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003848 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003849 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3850 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3851 DAG.getConstant(0, MVT::i32));
3852 } else {
3853 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3854 }
3855
3856 return Res;
3857 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003858
3859 // Bitcast operand 1 to i32.
3860 if (SrcVT == MVT::f64)
3861 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003862 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00003863 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3864
Evan Chengd6b641e2011-02-23 02:24:55 +00003865 // Or in the signbit with integer operations.
3866 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3867 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3868 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3869 if (VT == MVT::f32) {
3870 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3871 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3872 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3873 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003874 }
3875
Evan Chengd6b641e2011-02-23 02:24:55 +00003876 // f64: Or the high part with signbit and then combine two parts.
3877 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003878 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00003879 SDValue Lo = Tmp0.getValue(0);
3880 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3881 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3882 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003883}
3884
Evan Cheng168ced92010-05-22 01:47:14 +00003885SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3886 MachineFunction &MF = DAG.getMachineFunction();
3887 MachineFrameInfo *MFI = MF.getFrameInfo();
3888 MFI->setReturnAddressIsTaken(true);
3889
Bill Wendling908bf812014-01-06 00:43:20 +00003890 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003891 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003892
Evan Cheng168ced92010-05-22 01:47:14 +00003893 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003894 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003895 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3896 if (Depth) {
3897 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3898 SDValue Offset = DAG.getConstant(4, MVT::i32);
3899 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3900 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003901 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003902 }
3903
3904 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003905 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003906 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3907}
3908
Dan Gohman21cea8a2010-04-17 15:26:15 +00003909SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003910 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3911 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003912
Owen Anderson53aa7a92009-08-10 22:56:29 +00003913 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003914 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003915 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Tim Northoverd6a729b2014-01-06 14:28:05 +00003916 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetMachO())
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003917 ? ARM::R7 : ARM::R11;
3918 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3919 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003920 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3921 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003922 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003923 return FrameAddr;
3924}
3925
Renato Golinc7aea402014-05-06 16:51:25 +00003926// FIXME? Maybe this could be a TableGen attribute on some registers and
3927// this table could be generated automatically from RegInfo.
3928unsigned ARMTargetLowering::getRegisterByName(const char* RegName) const {
3929 unsigned Reg = StringSwitch<unsigned>(RegName)
3930 .Case("sp", ARM::SP)
3931 .Default(0);
3932 if (Reg)
3933 return Reg;
3934 report_fatal_error("Invalid register name global variable");
3935}
3936
Wesley Peck527da1b2010-11-23 03:31:01 +00003937/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003938/// expand a bit convert where either the source or destination type is i64 to
3939/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3940/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3941/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003942static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003943 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003944 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003945 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003946
Bob Wilson59b70ea2010-04-17 05:30:19 +00003947 // This function is only supposed to be called for i64 types, either as the
3948 // source or destination of the bit convert.
3949 EVT SrcVT = Op.getValueType();
3950 EVT DstVT = N->getValueType(0);
3951 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003952 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003953
Bob Wilson59b70ea2010-04-17 05:30:19 +00003954 // Turn i64->f64 into VMOVDRR.
3955 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00003956 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3957 DAG.getConstant(0, MVT::i32));
3958 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3959 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00003960 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00003961 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00003962 }
Bob Wilson7117a912009-03-20 22:42:55 +00003963
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003964 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00003965 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3966 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00003967 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00003968 // Merge the pieces into a single i64 value.
3969 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3970 }
Bob Wilson7117a912009-03-20 22:42:55 +00003971
Bob Wilson59b70ea2010-04-17 05:30:19 +00003972 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00003973}
3974
Bob Wilson2e076c42009-06-22 23:27:02 +00003975/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00003976/// Zero vectors are used to represent vector negation and in those cases
3977/// will be implemented with the NEON VNEG instruction. However, VNEG does
3978/// not support i64 elements, so sometimes the zero vectors will need to be
3979/// explicitly constructed. Regardless, use a canonical VMOV to create the
3980/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003981static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003982 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00003983 // The canonical modified immediate encoding of a zero vector is....0!
3984 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3985 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3986 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00003987 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00003988}
3989
Jim Grosbach624fcb22009-10-31 21:00:56 +00003990/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3991/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003992SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3993 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00003994 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3995 EVT VT = Op.getValueType();
3996 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003997 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003998 SDValue ShOpLo = Op.getOperand(0);
3999 SDValue ShOpHi = Op.getOperand(1);
4000 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004001 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004002 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004003
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004004 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4005
Jim Grosbach624fcb22009-10-31 21:00:56 +00004006 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4007 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4008 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4009 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4010 DAG.getConstant(VTBits, MVT::i32));
4011 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4012 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004013 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004014
4015 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4016 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004017 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004018 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004019 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004020 CCR, Cmp);
4021
4022 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004023 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004024}
4025
Jim Grosbach5d994042009-10-31 19:38:01 +00004026/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4027/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004028SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4029 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004030 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4031 EVT VT = Op.getValueType();
4032 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004033 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004034 SDValue ShOpLo = Op.getOperand(0);
4035 SDValue ShOpHi = Op.getOperand(1);
4036 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004037 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004038
4039 assert(Op.getOpcode() == ISD::SHL_PARTS);
4040 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4041 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4042 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4043 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4044 DAG.getConstant(VTBits, MVT::i32));
4045 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4046 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4047
4048 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4049 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4050 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004051 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004052 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004053 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004054 CCR, Cmp);
4055
4056 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004057 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004058}
4059
Jim Grosbach535d3b42010-09-08 03:54:02 +00004060SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004061 SelectionDAG &DAG) const {
4062 // The rounding mode is in bits 23:22 of the FPSCR.
4063 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4064 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4065 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004066 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004067 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4068 DAG.getConstant(Intrinsic::arm_get_fpscr,
4069 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004070 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004071 DAG.getConstant(1U << 22, MVT::i32));
4072 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4073 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004074 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004075 DAG.getConstant(3, MVT::i32));
4076}
4077
Jim Grosbach8546ec92010-01-18 19:58:49 +00004078static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4079 const ARMSubtarget *ST) {
4080 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004081 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004082
4083 if (!ST->hasV6T2Ops())
4084 return SDValue();
4085
4086 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4087 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4088}
4089
Evan Chengb4eae132012-12-04 22:41:50 +00004090/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4091/// for each 16-bit element from operand, repeated. The basic idea is to
4092/// leverage vcnt to get the 8-bit counts, gather and add the results.
4093///
4094/// Trace for v4i16:
4095/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4096/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4097/// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004098/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004099/// [b0 b1 b2 b3 b4 b5 b6 b7]
4100/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4101/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4102/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4103static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4104 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004105 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004106
4107 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4108 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4109 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4110 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4111 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4112 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4113}
4114
4115/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4116/// bit-count for each 16-bit element from the operand. We need slightly
4117/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4118/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004119///
Evan Chengb4eae132012-12-04 22:41:50 +00004120/// Trace for v4i16:
4121/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4122/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4123/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4124/// v4i16:Extracted = [k0 k1 k2 k3 ]
4125static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4126 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004127 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004128
4129 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4130 if (VT.is64BitVector()) {
4131 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4132 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4133 DAG.getIntPtrConstant(0));
4134 } else {
4135 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4136 BitCounts, DAG.getIntPtrConstant(0));
4137 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4138 }
4139}
4140
4141/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4142/// bit-count for each 32-bit element from the operand. The idea here is
4143/// to split the vector into 16-bit elements, leverage the 16-bit count
4144/// routine, and then combine the results.
4145///
4146/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4147/// input = [v0 v1 ] (vi: 32-bit elements)
4148/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4149/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004150/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004151/// [k0 k1 k2 k3 ]
4152/// N1 =+[k1 k0 k3 k2 ]
4153/// [k0 k2 k1 k3 ]
4154/// N2 =+[k1 k3 k0 k2 ]
4155/// [k0 k2 k1 k3 ]
4156/// Extended =+[k1 k3 k0 k2 ]
4157/// [k0 k2 ]
4158/// Extracted=+[k1 k3 ]
4159///
4160static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4161 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004162 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004163
4164 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4165
4166 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4167 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4168 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4169 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4170 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4171
4172 if (VT.is64BitVector()) {
4173 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4174 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4175 DAG.getIntPtrConstant(0));
4176 } else {
4177 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4178 DAG.getIntPtrConstant(0));
4179 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4180 }
4181}
4182
4183static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4184 const ARMSubtarget *ST) {
4185 EVT VT = N->getValueType(0);
4186
4187 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004188 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4189 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004190 "Unexpected type for custom ctpop lowering");
4191
4192 if (VT.getVectorElementType() == MVT::i32)
4193 return lowerCTPOP32BitElements(N, DAG);
4194 else
4195 return lowerCTPOP16BitElements(N, DAG);
4196}
4197
Bob Wilson2e076c42009-06-22 23:27:02 +00004198static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4199 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004200 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004201 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004202
Bob Wilson7d471332010-11-18 21:16:28 +00004203 if (!VT.isVector())
4204 return SDValue();
4205
Bob Wilson2e076c42009-06-22 23:27:02 +00004206 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004207 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004208
Bob Wilson7d471332010-11-18 21:16:28 +00004209 // Left shifts translate directly to the vshiftu intrinsic.
4210 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004211 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004212 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4213 N->getOperand(0), N->getOperand(1));
4214
4215 assert((N->getOpcode() == ISD::SRA ||
4216 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4217
4218 // NEON uses the same intrinsics for both left and right shifts. For
4219 // right shifts, the shift amounts are negative, so negate the vector of
4220 // shift amounts.
4221 EVT ShiftVT = N->getOperand(1).getValueType();
4222 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4223 getZeroVector(ShiftVT, DAG, dl),
4224 N->getOperand(1));
4225 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4226 Intrinsic::arm_neon_vshifts :
4227 Intrinsic::arm_neon_vshiftu);
4228 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4229 DAG.getConstant(vshiftInt, MVT::i32),
4230 N->getOperand(0), NegatedCount);
4231}
4232
4233static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4234 const ARMSubtarget *ST) {
4235 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004236 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004237
Eli Friedman682d8c12009-08-22 03:13:10 +00004238 // We can get here for a node like i32 = ISD::SHL i32, i64
4239 if (VT != MVT::i64)
4240 return SDValue();
4241
4242 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004243 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004244
Chris Lattnerf81d5882007-11-24 07:07:01 +00004245 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4246 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004247 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004248 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004249
Chris Lattnerf81d5882007-11-24 07:07:01 +00004250 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004251 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004252
Chris Lattnerf81d5882007-11-24 07:07:01 +00004253 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004254 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004255 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004256 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004257 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004258
Chris Lattnerf81d5882007-11-24 07:07:01 +00004259 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4260 // captures the result into a carry flag.
4261 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004262 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004263
Chris Lattnerf81d5882007-11-24 07:07:01 +00004264 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004265 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004266
Chris Lattnerf81d5882007-11-24 07:07:01 +00004267 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004268 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004269}
4270
Bob Wilson2e076c42009-06-22 23:27:02 +00004271static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4272 SDValue TmpOp0, TmpOp1;
4273 bool Invert = false;
4274 bool Swap = false;
4275 unsigned Opc = 0;
4276
4277 SDValue Op0 = Op.getOperand(0);
4278 SDValue Op1 = Op.getOperand(1);
4279 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004280 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004281 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004282 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004283
4284 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4285 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004286 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004287 case ISD::SETUNE:
4288 case ISD::SETNE: Invert = true; // Fallthrough
4289 case ISD::SETOEQ:
4290 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4291 case ISD::SETOLT:
4292 case ISD::SETLT: Swap = true; // Fallthrough
4293 case ISD::SETOGT:
4294 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4295 case ISD::SETOLE:
4296 case ISD::SETLE: Swap = true; // Fallthrough
4297 case ISD::SETOGE:
4298 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4299 case ISD::SETUGE: Swap = true; // Fallthrough
4300 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4301 case ISD::SETUGT: Swap = true; // Fallthrough
4302 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4303 case ISD::SETUEQ: Invert = true; // Fallthrough
4304 case ISD::SETONE:
4305 // Expand this to (OLT | OGT).
4306 TmpOp0 = Op0;
4307 TmpOp1 = Op1;
4308 Opc = ISD::OR;
4309 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4310 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4311 break;
4312 case ISD::SETUO: Invert = true; // Fallthrough
4313 case ISD::SETO:
4314 // Expand this to (OLT | OGE).
4315 TmpOp0 = Op0;
4316 TmpOp1 = Op1;
4317 Opc = ISD::OR;
4318 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4319 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4320 break;
4321 }
4322 } else {
4323 // Integer comparisons.
4324 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004325 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004326 case ISD::SETNE: Invert = true;
4327 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4328 case ISD::SETLT: Swap = true;
4329 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4330 case ISD::SETLE: Swap = true;
4331 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4332 case ISD::SETULT: Swap = true;
4333 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4334 case ISD::SETULE: Swap = true;
4335 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4336 }
4337
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004338 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004339 if (Opc == ARMISD::VCEQ) {
4340
4341 SDValue AndOp;
4342 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4343 AndOp = Op0;
4344 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4345 AndOp = Op1;
4346
4347 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004348 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004349 AndOp = AndOp.getOperand(0);
4350
4351 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4352 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004353 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4354 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004355 Invert = !Invert;
4356 }
4357 }
4358 }
4359
4360 if (Swap)
4361 std::swap(Op0, Op1);
4362
Owen Andersonc7baee32010-11-08 23:21:22 +00004363 // If one of the operands is a constant vector zero, attempt to fold the
4364 // comparison to a specialized compare-against-zero form.
4365 SDValue SingleOp;
4366 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4367 SingleOp = Op0;
4368 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4369 if (Opc == ARMISD::VCGE)
4370 Opc = ARMISD::VCLEZ;
4371 else if (Opc == ARMISD::VCGT)
4372 Opc = ARMISD::VCLTZ;
4373 SingleOp = Op1;
4374 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004375
Owen Andersonc7baee32010-11-08 23:21:22 +00004376 SDValue Result;
4377 if (SingleOp.getNode()) {
4378 switch (Opc) {
4379 case ARMISD::VCEQ:
4380 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4381 case ARMISD::VCGE:
4382 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4383 case ARMISD::VCLEZ:
4384 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4385 case ARMISD::VCGT:
4386 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4387 case ARMISD::VCLTZ:
4388 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4389 default:
4390 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4391 }
4392 } else {
4393 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4394 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004395
4396 if (Invert)
4397 Result = DAG.getNOT(dl, Result, VT);
4398
4399 return Result;
4400}
4401
Bob Wilson5b2b5042010-06-14 22:19:57 +00004402/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4403/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004404/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004405static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4406 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004407 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004408 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004409
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004410 // SplatBitSize is set to the smallest size that splats the vector, so a
4411 // zero vector will always have SplatBitSize == 8. However, NEON modified
4412 // immediate instructions others than VMOV do not support the 8-bit encoding
4413 // of a zero vector, and the default encoding of zero is supposed to be the
4414 // 32-bit version.
4415 if (SplatBits == 0)
4416 SplatBitSize = 32;
4417
Bob Wilson2e076c42009-06-22 23:27:02 +00004418 switch (SplatBitSize) {
4419 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004420 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004421 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004422 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004423 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004424 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004425 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004426 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004427 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004428
4429 case 16:
4430 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004431 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004432 if ((SplatBits & ~0xff) == 0) {
4433 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004434 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004435 Imm = SplatBits;
4436 break;
4437 }
4438 if ((SplatBits & ~0xff00) == 0) {
4439 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004440 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004441 Imm = SplatBits >> 8;
4442 break;
4443 }
4444 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004445
4446 case 32:
4447 // NEON's 32-bit VMOV supports splat values where:
4448 // * only one byte is nonzero, or
4449 // * the least significant byte is 0xff and the second byte is nonzero, or
4450 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004451 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004452 if ((SplatBits & ~0xff) == 0) {
4453 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004454 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004455 Imm = SplatBits;
4456 break;
4457 }
4458 if ((SplatBits & ~0xff00) == 0) {
4459 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004460 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004461 Imm = SplatBits >> 8;
4462 break;
4463 }
4464 if ((SplatBits & ~0xff0000) == 0) {
4465 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004466 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004467 Imm = SplatBits >> 16;
4468 break;
4469 }
4470 if ((SplatBits & ~0xff000000) == 0) {
4471 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004472 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004473 Imm = SplatBits >> 24;
4474 break;
4475 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004476
Owen Andersona4076922010-11-05 21:57:54 +00004477 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4478 if (type == OtherModImm) return SDValue();
4479
Bob Wilson2e076c42009-06-22 23:27:02 +00004480 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004481 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4482 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004483 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004484 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004485 break;
4486 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004487
4488 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004489 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4490 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004491 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004492 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004493 break;
4494 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004495
4496 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4497 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4498 // VMOV.I32. A (very) minor optimization would be to replicate the value
4499 // and fall through here to test for a valid 64-bit splat. But, then the
4500 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004501 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004502
4503 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004504 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004505 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004506 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004507 uint64_t BitMask = 0xff;
4508 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004509 unsigned ImmMask = 1;
4510 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004511 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004512 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004513 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004514 Imm |= ImmMask;
4515 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004516 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004517 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004518 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004519 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004520 }
Bob Wilson6eae5202010-06-11 21:34:50 +00004521 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004522 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004523 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004524 break;
4525 }
4526
Bob Wilson6eae5202010-06-11 21:34:50 +00004527 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004528 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004529 }
4530
Bob Wilsona3f19012010-07-13 21:16:48 +00004531 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4532 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004533}
4534
Lang Hames591cdaf2012-03-29 21:56:11 +00004535SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4536 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004537 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004538 return SDValue();
4539
Tim Northoverf79c3a52013-08-20 08:57:11 +00004540 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004541 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004542
4543 // Try splatting with a VMOV.f32...
4544 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004545 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4546
Lang Hames591cdaf2012-03-29 21:56:11 +00004547 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004548 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4549 // We have code in place to select a valid ConstantFP already, no need to
4550 // do any mangling.
4551 return Op;
4552 }
4553
4554 // It's a float and we are trying to use NEON operations where
4555 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004556 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004557 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4558 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4559 NewVal);
4560 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4561 DAG.getConstant(0, MVT::i32));
4562 }
4563
Tim Northoverf79c3a52013-08-20 08:57:11 +00004564 // The rest of our options are NEON only, make sure that's allowed before
4565 // proceeding..
4566 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4567 return SDValue();
4568
Lang Hames591cdaf2012-03-29 21:56:11 +00004569 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004570 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4571
4572 // It wouldn't really be worth bothering for doubles except for one very
4573 // important value, which does happen to match: 0.0. So make sure we don't do
4574 // anything stupid.
4575 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4576 return SDValue();
4577
4578 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4579 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4580 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004581 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004582 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004583 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4584 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004585 if (IsDouble)
4586 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4587
4588 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004589 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4590 VecConstant);
4591 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4592 DAG.getConstant(0, MVT::i32));
4593 }
4594
4595 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004596 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4597 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004598 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004599 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004600 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004601
4602 if (IsDouble)
4603 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4604
4605 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004606 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4607 VecConstant);
4608 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4609 DAG.getConstant(0, MVT::i32));
4610 }
4611
4612 return SDValue();
4613}
4614
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004615// check if an VEXT instruction can handle the shuffle mask when the
4616// vector sources of the shuffle are the same.
4617static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4618 unsigned NumElts = VT.getVectorNumElements();
4619
4620 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4621 if (M[0] < 0)
4622 return false;
4623
4624 Imm = M[0];
4625
4626 // If this is a VEXT shuffle, the immediate value is the index of the first
4627 // element. The other shuffle indices must be the successive elements after
4628 // the first one.
4629 unsigned ExpectedElt = Imm;
4630 for (unsigned i = 1; i < NumElts; ++i) {
4631 // Increment the expected index. If it wraps around, just follow it
4632 // back to index zero and keep going.
4633 ++ExpectedElt;
4634 if (ExpectedElt == NumElts)
4635 ExpectedElt = 0;
4636
4637 if (M[i] < 0) continue; // ignore UNDEF indices
4638 if (ExpectedElt != static_cast<unsigned>(M[i]))
4639 return false;
4640 }
4641
4642 return true;
4643}
4644
Lang Hames591cdaf2012-03-29 21:56:11 +00004645
Benjamin Kramer339ced42012-01-15 13:16:05 +00004646static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004647 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004648 unsigned NumElts = VT.getVectorNumElements();
4649 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004650
4651 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4652 if (M[0] < 0)
4653 return false;
4654
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004655 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004656
4657 // If this is a VEXT shuffle, the immediate value is the index of the first
4658 // element. The other shuffle indices must be the successive elements after
4659 // the first one.
4660 unsigned ExpectedElt = Imm;
4661 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004662 // Increment the expected index. If it wraps around, it may still be
4663 // a VEXT but the source vectors must be swapped.
4664 ExpectedElt += 1;
4665 if (ExpectedElt == NumElts * 2) {
4666 ExpectedElt = 0;
4667 ReverseVEXT = true;
4668 }
4669
Bob Wilson411dfad2010-08-17 05:54:34 +00004670 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004671 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004672 return false;
4673 }
4674
4675 // Adjust the index value if the source operands will be swapped.
4676 if (ReverseVEXT)
4677 Imm -= NumElts;
4678
Bob Wilson32cd8552009-08-19 17:03:43 +00004679 return true;
4680}
4681
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004682/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4683/// instruction with the specified blocksize. (The order of the elements
4684/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004685static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004686 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4687 "Only possible block sizes for VREV are: 16, 32, 64");
4688
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004689 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004690 if (EltSz == 64)
4691 return false;
4692
4693 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004694 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004695 // If the first shuffle index is UNDEF, be optimistic.
4696 if (M[0] < 0)
4697 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004698
4699 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4700 return false;
4701
4702 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004703 if (M[i] < 0) continue; // ignore UNDEF indices
4704 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004705 return false;
4706 }
4707
4708 return true;
4709}
4710
Benjamin Kramer339ced42012-01-15 13:16:05 +00004711static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004712 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4713 // range, then 0 is placed into the resulting vector. So pretty much any mask
4714 // of 8 elements can work here.
4715 return VT == MVT::v8i8 && M.size() == 8;
4716}
4717
Benjamin Kramer339ced42012-01-15 13:16:05 +00004718static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004719 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4720 if (EltSz == 64)
4721 return false;
4722
Bob Wilsona7062312009-08-21 20:54:19 +00004723 unsigned NumElts = VT.getVectorNumElements();
4724 WhichResult = (M[0] == 0 ? 0 : 1);
4725 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004726 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4727 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004728 return false;
4729 }
4730 return true;
4731}
4732
Bob Wilson0bbd3072009-12-03 06:40:55 +00004733/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4734/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4735/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004736static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004737 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4738 if (EltSz == 64)
4739 return false;
4740
4741 unsigned NumElts = VT.getVectorNumElements();
4742 WhichResult = (M[0] == 0 ? 0 : 1);
4743 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004744 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4745 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004746 return false;
4747 }
4748 return true;
4749}
4750
Benjamin Kramer339ced42012-01-15 13:16:05 +00004751static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004752 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4753 if (EltSz == 64)
4754 return false;
4755
Bob Wilsona7062312009-08-21 20:54:19 +00004756 unsigned NumElts = VT.getVectorNumElements();
4757 WhichResult = (M[0] == 0 ? 0 : 1);
4758 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004759 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004760 if ((unsigned) M[i] != 2 * i + WhichResult)
4761 return false;
4762 }
4763
4764 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004765 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004766 return false;
4767
4768 return true;
4769}
4770
Bob Wilson0bbd3072009-12-03 06:40:55 +00004771/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4772/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4773/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004774static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004775 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4776 if (EltSz == 64)
4777 return false;
4778
4779 unsigned Half = VT.getVectorNumElements() / 2;
4780 WhichResult = (M[0] == 0 ? 0 : 1);
4781 for (unsigned j = 0; j != 2; ++j) {
4782 unsigned Idx = WhichResult;
4783 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004784 int MIdx = M[i + j * Half];
4785 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004786 return false;
4787 Idx += 2;
4788 }
4789 }
4790
4791 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4792 if (VT.is64BitVector() && EltSz == 32)
4793 return false;
4794
4795 return true;
4796}
4797
Benjamin Kramer339ced42012-01-15 13:16:05 +00004798static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004799 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4800 if (EltSz == 64)
4801 return false;
4802
Bob Wilsona7062312009-08-21 20:54:19 +00004803 unsigned NumElts = VT.getVectorNumElements();
4804 WhichResult = (M[0] == 0 ? 0 : 1);
4805 unsigned Idx = WhichResult * NumElts / 2;
4806 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004807 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4808 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004809 return false;
4810 Idx += 1;
4811 }
4812
4813 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004814 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004815 return false;
4816
4817 return true;
4818}
4819
Bob Wilson0bbd3072009-12-03 06:40:55 +00004820/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4821/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4822/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004823static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004824 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4825 if (EltSz == 64)
4826 return false;
4827
4828 unsigned NumElts = VT.getVectorNumElements();
4829 WhichResult = (M[0] == 0 ? 0 : 1);
4830 unsigned Idx = WhichResult * NumElts / 2;
4831 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004832 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4833 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004834 return false;
4835 Idx += 1;
4836 }
4837
4838 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4839 if (VT.is64BitVector() && EltSz == 32)
4840 return false;
4841
4842 return true;
4843}
4844
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004845/// \return true if this is a reverse operation on an vector.
4846static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4847 unsigned NumElts = VT.getVectorNumElements();
4848 // Make sure the mask has the right size.
4849 if (NumElts != M.size())
4850 return false;
4851
4852 // Look for <15, ..., 3, -1, 1, 0>.
4853 for (unsigned i = 0; i != NumElts; ++i)
4854 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4855 return false;
4856
4857 return true;
4858}
4859
Dale Johannesen2bff5052010-07-29 20:10:08 +00004860// If N is an integer constant that can be moved into a register in one
4861// instruction, return an SDValue of such a constant (will become a MOV
4862// instruction). Otherwise return null.
4863static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004864 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004865 uint64_t Val;
4866 if (!isa<ConstantSDNode>(N))
4867 return SDValue();
4868 Val = cast<ConstantSDNode>(N)->getZExtValue();
4869
4870 if (ST->isThumb1Only()) {
4871 if (Val <= 255 || ~Val <= 255)
4872 return DAG.getConstant(Val, MVT::i32);
4873 } else {
4874 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4875 return DAG.getConstant(Val, MVT::i32);
4876 }
4877 return SDValue();
4878}
4879
Bob Wilson2e076c42009-06-22 23:27:02 +00004880// If this is a case we can't handle, return null and let the default
4881// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004882SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4883 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004884 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004885 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004886 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004887
4888 APInt SplatBits, SplatUndef;
4889 unsigned SplatBitSize;
4890 bool HasAnyUndefs;
4891 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004892 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004893 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004894 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004895 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004896 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004897 DAG, VmovVT, VT.is128BitVector(),
4898 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004899 if (Val.getNode()) {
4900 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004901 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004902 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004903
4904 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004905 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004906 Val = isNEONModifiedImm(NegatedImm,
4907 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004908 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004909 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004910 if (Val.getNode()) {
4911 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004912 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004913 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004914
4915 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004916 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004917 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004918 if (ImmVal != -1) {
4919 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4920 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4921 }
4922 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004923 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004924 }
4925
Bob Wilson91fdf682010-05-22 00:23:12 +00004926 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004927 //
4928 // As an optimisation, even if more than one value is used it may be more
4929 // profitable to splat with one value then change some lanes.
4930 //
4931 // Heuristically we decide to do this if the vector has a "dominant" value,
4932 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004933 unsigned NumElts = VT.getVectorNumElements();
4934 bool isOnlyLowElement = true;
4935 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004936 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004937 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004938
4939 // Map of the number of times a particular SDValue appears in the
4940 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004941 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004942 SDValue Value;
4943 for (unsigned i = 0; i < NumElts; ++i) {
4944 SDValue V = Op.getOperand(i);
4945 if (V.getOpcode() == ISD::UNDEF)
4946 continue;
4947 if (i > 0)
4948 isOnlyLowElement = false;
4949 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4950 isConstant = false;
4951
James Molloy49bdbce2012-09-06 09:55:02 +00004952 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00004953 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00004954
James Molloy49bdbce2012-09-06 09:55:02 +00004955 // Is this value dominant? (takes up more than half of the lanes)
4956 if (++Count > (NumElts / 2)) {
4957 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00004958 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00004959 }
Bob Wilson91fdf682010-05-22 00:23:12 +00004960 }
James Molloy49bdbce2012-09-06 09:55:02 +00004961 if (ValueCounts.size() != 1)
4962 usesOnlyOneValue = false;
4963 if (!Value.getNode() && ValueCounts.size() > 0)
4964 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00004965
James Molloy49bdbce2012-09-06 09:55:02 +00004966 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00004967 return DAG.getUNDEF(VT);
4968
Quentin Colombet0f2fe742013-07-23 22:34:47 +00004969 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
4970 // Keep going if we are hitting this case.
4971 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00004972 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4973
Dale Johannesen2bff5052010-07-29 20:10:08 +00004974 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4975
Dale Johannesen710a2d92010-10-19 20:00:17 +00004976 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4977 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00004978 if (hasDominantValue && EltSize <= 32) {
4979 if (!isConstant) {
4980 SDValue N;
4981
4982 // If we are VDUPing a value that comes directly from a vector, that will
4983 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00004984 // just use VDUPLANE. We can only do this if the lane being extracted
4985 // is at a constant index, as the VDUP from lane instructions only have
4986 // constant-index forms.
4987 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4988 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00004989 // We need to create a new undef vector to use for the VDUPLANE if the
4990 // size of the vector from which we get the value is different than the
4991 // size of the vector that we need to create. We will insert the element
4992 // such that the register coalescer will remove unnecessary copies.
4993 if (VT != Value->getOperand(0).getValueType()) {
4994 ConstantSDNode *constIndex;
4995 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4996 assert(constIndex && "The index is not a constant!");
4997 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4998 VT.getVectorNumElements();
4999 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5000 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
5001 Value, DAG.getConstant(index, MVT::i32)),
5002 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005003 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005004 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005005 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005006 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005007 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5008
5009 if (!usesOnlyOneValue) {
5010 // The dominant value was splatted as 'N', but we now have to insert
5011 // all differing elements.
5012 for (unsigned I = 0; I < NumElts; ++I) {
5013 if (Op.getOperand(I) == Value)
5014 continue;
5015 SmallVector<SDValue, 3> Ops;
5016 Ops.push_back(N);
5017 Ops.push_back(Op.getOperand(I));
5018 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005019 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005020 }
5021 }
5022 return N;
5023 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005024 if (VT.getVectorElementType().isFloatingPoint()) {
5025 SmallVector<SDValue, 8> Ops;
5026 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005027 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005028 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005029 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005030 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005031 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5032 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005033 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005034 }
James Molloy49bdbce2012-09-06 09:55:02 +00005035 if (usesOnlyOneValue) {
5036 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5037 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005038 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005039 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005040 }
5041
5042 // If all elements are constants and the case above didn't get hit, fall back
5043 // to the default expansion, which will generate a load from the constant
5044 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005045 if (isConstant)
5046 return SDValue();
5047
Bob Wilson6f2b8962011-01-07 21:37:30 +00005048 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5049 if (NumElts >= 4) {
5050 SDValue shuffle = ReconstructShuffle(Op, DAG);
5051 if (shuffle != SDValue())
5052 return shuffle;
5053 }
5054
Bob Wilson91fdf682010-05-22 00:23:12 +00005055 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005056 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5057 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005058 if (EltSize >= 32) {
5059 // Do the expansion with floating-point types, since that is what the VFP
5060 // registers are defined to use, and since i64 is not legal.
5061 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5062 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005063 SmallVector<SDValue, 8> Ops;
5064 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005065 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005066 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005067 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005068 }
5069
Jim Grosbach24e102a2013-07-08 18:18:52 +00005070 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5071 // know the default expansion would otherwise fall back on something even
5072 // worse. For a vector with one or two non-undef values, that's
5073 // scalar_to_vector for the elements followed by a shuffle (provided the
5074 // shuffle is valid for the target) and materialization element by element
5075 // on the stack followed by a load for everything else.
5076 if (!isConstant && !usesOnlyOneValue) {
5077 SDValue Vec = DAG.getUNDEF(VT);
5078 for (unsigned i = 0 ; i < NumElts; ++i) {
5079 SDValue V = Op.getOperand(i);
5080 if (V.getOpcode() == ISD::UNDEF)
5081 continue;
5082 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5083 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5084 }
5085 return Vec;
5086 }
5087
Bob Wilson2e076c42009-06-22 23:27:02 +00005088 return SDValue();
5089}
5090
Bob Wilson6f2b8962011-01-07 21:37:30 +00005091// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005092// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005093SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5094 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005095 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005096 EVT VT = Op.getValueType();
5097 unsigned NumElts = VT.getVectorNumElements();
5098
5099 SmallVector<SDValue, 2> SourceVecs;
5100 SmallVector<unsigned, 2> MinElts;
5101 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005102
Bob Wilson6f2b8962011-01-07 21:37:30 +00005103 for (unsigned i = 0; i < NumElts; ++i) {
5104 SDValue V = Op.getOperand(i);
5105 if (V.getOpcode() == ISD::UNDEF)
5106 continue;
5107 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5108 // A shuffle can only come from building a vector from various
5109 // elements of other vectors.
5110 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005111 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5112 VT.getVectorElementType()) {
5113 // This code doesn't know how to handle shuffles where the vector
5114 // element types do not match (this happens because type legalization
5115 // promotes the return type of EXTRACT_VECTOR_ELT).
5116 // FIXME: It might be appropriate to extend this code to handle
5117 // mismatched types.
5118 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005119 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005120
Bob Wilson6f2b8962011-01-07 21:37:30 +00005121 // Record this extraction against the appropriate vector if possible...
5122 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005123 // If the element number isn't a constant, we can't effectively
5124 // analyze what's going on.
5125 if (!isa<ConstantSDNode>(V.getOperand(1)))
5126 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005127 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5128 bool FoundSource = false;
5129 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5130 if (SourceVecs[j] == SourceVec) {
5131 if (MinElts[j] > EltNo)
5132 MinElts[j] = EltNo;
5133 if (MaxElts[j] < EltNo)
5134 MaxElts[j] = EltNo;
5135 FoundSource = true;
5136 break;
5137 }
5138 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005139
Bob Wilson6f2b8962011-01-07 21:37:30 +00005140 // Or record a new source if not...
5141 if (!FoundSource) {
5142 SourceVecs.push_back(SourceVec);
5143 MinElts.push_back(EltNo);
5144 MaxElts.push_back(EltNo);
5145 }
5146 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005147
Bob Wilson6f2b8962011-01-07 21:37:30 +00005148 // Currently only do something sane when at most two source vectors
5149 // involved.
5150 if (SourceVecs.size() > 2)
5151 return SDValue();
5152
5153 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5154 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005155
Bob Wilson6f2b8962011-01-07 21:37:30 +00005156 // This loop extracts the usage patterns of the source vectors
5157 // and prepares appropriate SDValues for a shuffle if possible.
5158 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5159 if (SourceVecs[i].getValueType() == VT) {
5160 // No VEXT necessary
5161 ShuffleSrcs[i] = SourceVecs[i];
5162 VEXTOffsets[i] = 0;
5163 continue;
5164 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5165 // It probably isn't worth padding out a smaller vector just to
5166 // break it down again in a shuffle.
5167 return SDValue();
5168 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005169
Bob Wilson6f2b8962011-01-07 21:37:30 +00005170 // Since only 64-bit and 128-bit vectors are legal on ARM and
5171 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005172 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5173 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005174
Bob Wilson6f2b8962011-01-07 21:37:30 +00005175 if (MaxElts[i] - MinElts[i] >= NumElts) {
5176 // Span too large for a VEXT to cope
5177 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005178 }
5179
Bob Wilson6f2b8962011-01-07 21:37:30 +00005180 if (MinElts[i] >= NumElts) {
5181 // The extraction can just take the second half
5182 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005183 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5184 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005185 DAG.getIntPtrConstant(NumElts));
5186 } else if (MaxElts[i] < NumElts) {
5187 // The extraction can just take the first half
5188 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005189 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5190 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005191 DAG.getIntPtrConstant(0));
5192 } else {
5193 // An actual VEXT is needed
5194 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005195 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5196 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005197 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005198 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5199 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005200 DAG.getIntPtrConstant(NumElts));
5201 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5202 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5203 }
5204 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005205
Bob Wilson6f2b8962011-01-07 21:37:30 +00005206 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005207
Bob Wilson6f2b8962011-01-07 21:37:30 +00005208 for (unsigned i = 0; i < NumElts; ++i) {
5209 SDValue Entry = Op.getOperand(i);
5210 if (Entry.getOpcode() == ISD::UNDEF) {
5211 Mask.push_back(-1);
5212 continue;
5213 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005214
Bob Wilson6f2b8962011-01-07 21:37:30 +00005215 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005216 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5217 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005218 if (ExtractVec == SourceVecs[0]) {
5219 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5220 } else {
5221 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5222 }
5223 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005224
Bob Wilson6f2b8962011-01-07 21:37:30 +00005225 // Final check before we try to produce nonsense...
5226 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005227 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5228 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005229
Bob Wilson6f2b8962011-01-07 21:37:30 +00005230 return SDValue();
5231}
5232
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005233/// isShuffleMaskLegal - Targets can use this to indicate that they only
5234/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5235/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5236/// are assumed to be legal.
5237bool
5238ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5239 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005240 if (VT.getVectorNumElements() == 4 &&
5241 (VT.is128BitVector() || VT.is64BitVector())) {
5242 unsigned PFIndexes[4];
5243 for (unsigned i = 0; i != 4; ++i) {
5244 if (M[i] < 0)
5245 PFIndexes[i] = 8;
5246 else
5247 PFIndexes[i] = M[i];
5248 }
5249
5250 // Compute the index in the perfect shuffle table.
5251 unsigned PFTableIndex =
5252 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5253 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5254 unsigned Cost = (PFEntry >> 30);
5255
5256 if (Cost <= 4)
5257 return true;
5258 }
5259
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005260 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005261 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005262
Bob Wilson846bd792010-06-07 23:53:38 +00005263 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5264 return (EltSize >= 32 ||
5265 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005266 isVREVMask(M, VT, 64) ||
5267 isVREVMask(M, VT, 32) ||
5268 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005269 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005270 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005271 isVTRNMask(M, VT, WhichResult) ||
5272 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005273 isVZIPMask(M, VT, WhichResult) ||
5274 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5275 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005276 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5277 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005278}
5279
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005280/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5281/// the specified operations to build the shuffle.
5282static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5283 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005284 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005285 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5286 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5287 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5288
5289 enum {
5290 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5291 OP_VREV,
5292 OP_VDUP0,
5293 OP_VDUP1,
5294 OP_VDUP2,
5295 OP_VDUP3,
5296 OP_VEXT1,
5297 OP_VEXT2,
5298 OP_VEXT3,
5299 OP_VUZPL, // VUZP, left result
5300 OP_VUZPR, // VUZP, right result
5301 OP_VZIPL, // VZIP, left result
5302 OP_VZIPR, // VZIP, right result
5303 OP_VTRNL, // VTRN, left result
5304 OP_VTRNR // VTRN, right result
5305 };
5306
5307 if (OpNum == OP_COPY) {
5308 if (LHSID == (1*9+2)*9+3) return LHS;
5309 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5310 return RHS;
5311 }
5312
5313 SDValue OpLHS, OpRHS;
5314 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5315 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5316 EVT VT = OpLHS.getValueType();
5317
5318 switch (OpNum) {
5319 default: llvm_unreachable("Unknown shuffle opcode!");
5320 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005321 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005322 if (VT.getVectorElementType() == MVT::i32 ||
5323 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005324 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5325 // vrev <4 x i16> -> VREV32
5326 if (VT.getVectorElementType() == MVT::i16)
5327 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5328 // vrev <4 x i8> -> VREV16
5329 assert(VT.getVectorElementType() == MVT::i8);
5330 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005331 case OP_VDUP0:
5332 case OP_VDUP1:
5333 case OP_VDUP2:
5334 case OP_VDUP3:
5335 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005336 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005337 case OP_VEXT1:
5338 case OP_VEXT2:
5339 case OP_VEXT3:
5340 return DAG.getNode(ARMISD::VEXT, dl, VT,
5341 OpLHS, OpRHS,
5342 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5343 case OP_VUZPL:
5344 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005345 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005346 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5347 case OP_VZIPL:
5348 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005349 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005350 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5351 case OP_VTRNL:
5352 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005353 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5354 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005355 }
5356}
5357
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005358static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005359 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005360 SelectionDAG &DAG) {
5361 // Check to see if we can use the VTBL instruction.
5362 SDValue V1 = Op.getOperand(0);
5363 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005364 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005365
5366 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005367 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005368 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5369 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5370
5371 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5372 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005373 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005374
Owen Anderson77aa2662011-04-05 21:48:57 +00005375 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005376 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005377}
5378
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005379static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5380 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005381 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005382 SDValue OpLHS = Op.getOperand(0);
5383 EVT VT = OpLHS.getValueType();
5384
5385 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5386 "Expect an v8i16/v16i8 type");
5387 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5388 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5389 // extract the first 8 bytes into the top double word and the last 8 bytes
5390 // into the bottom double word. The v8i16 case is similar.
5391 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5392 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5393 DAG.getConstant(ExtractNum, MVT::i32));
5394}
5395
Bob Wilson2e076c42009-06-22 23:27:02 +00005396static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005397 SDValue V1 = Op.getOperand(0);
5398 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005399 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005400 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005401 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005402
Bob Wilsonc6800b52009-08-13 02:13:04 +00005403 // Convert shuffles that are directly supported on NEON to target-specific
5404 // DAG nodes, instead of keeping them as shuffles and matching them again
5405 // during code selection. This is more efficient and avoids the possibility
5406 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005407 // FIXME: floating-point vectors should be canonicalized to integer vectors
5408 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005409 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005410
Bob Wilson846bd792010-06-07 23:53:38 +00005411 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5412 if (EltSize <= 32) {
5413 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5414 int Lane = SVN->getSplatIndex();
5415 // If this is undef splat, generate it via "just" vdup, if possible.
5416 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005417
Dan Gohman198b7ff2011-11-03 21:49:52 +00005418 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005419 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5420 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5421 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005422 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5423 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5424 // reaches it).
5425 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5426 !isa<ConstantSDNode>(V1.getOperand(0))) {
5427 bool IsScalarToVector = true;
5428 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5429 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5430 IsScalarToVector = false;
5431 break;
5432 }
5433 if (IsScalarToVector)
5434 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5435 }
Bob Wilson846bd792010-06-07 23:53:38 +00005436 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5437 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005438 }
Bob Wilson846bd792010-06-07 23:53:38 +00005439
5440 bool ReverseVEXT;
5441 unsigned Imm;
5442 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5443 if (ReverseVEXT)
5444 std::swap(V1, V2);
5445 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5446 DAG.getConstant(Imm, MVT::i32));
5447 }
5448
5449 if (isVREVMask(ShuffleMask, VT, 64))
5450 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5451 if (isVREVMask(ShuffleMask, VT, 32))
5452 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5453 if (isVREVMask(ShuffleMask, VT, 16))
5454 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5455
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005456 if (V2->getOpcode() == ISD::UNDEF &&
5457 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5458 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5459 DAG.getConstant(Imm, MVT::i32));
5460 }
5461
Bob Wilson846bd792010-06-07 23:53:38 +00005462 // Check for Neon shuffles that modify both input vectors in place.
5463 // If both results are used, i.e., if there are two shuffles with the same
5464 // source operands and with masks corresponding to both results of one of
5465 // these operations, DAG memoization will ensure that a single node is
5466 // used for both shuffles.
5467 unsigned WhichResult;
5468 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5469 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5470 V1, V2).getValue(WhichResult);
5471 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5472 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5473 V1, V2).getValue(WhichResult);
5474 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5475 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5476 V1, V2).getValue(WhichResult);
5477
5478 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5479 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5480 V1, V1).getValue(WhichResult);
5481 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5482 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5483 V1, V1).getValue(WhichResult);
5484 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5485 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5486 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005487 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005488
Bob Wilsona7062312009-08-21 20:54:19 +00005489 // If the shuffle is not directly supported and it has 4 elements, use
5490 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005491 unsigned NumElts = VT.getVectorNumElements();
5492 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005493 unsigned PFIndexes[4];
5494 for (unsigned i = 0; i != 4; ++i) {
5495 if (ShuffleMask[i] < 0)
5496 PFIndexes[i] = 8;
5497 else
5498 PFIndexes[i] = ShuffleMask[i];
5499 }
5500
5501 // Compute the index in the perfect shuffle table.
5502 unsigned PFTableIndex =
5503 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005504 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5505 unsigned Cost = (PFEntry >> 30);
5506
5507 if (Cost <= 4)
5508 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5509 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005510
Bob Wilsond8a9a042010-06-04 00:04:02 +00005511 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005512 if (EltSize >= 32) {
5513 // Do the expansion with floating-point types, since that is what the VFP
5514 // registers are defined to use, and since i64 is not legal.
5515 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5516 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005517 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5518 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005519 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005520 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005521 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005522 Ops.push_back(DAG.getUNDEF(EltVT));
5523 else
5524 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5525 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5526 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5527 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005528 }
Craig Topper48d114b2014-04-26 18:35:24 +00005529 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005530 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005531 }
5532
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005533 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5534 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5535
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005536 if (VT == MVT::v8i8) {
5537 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5538 if (NewOp.getNode())
5539 return NewOp;
5540 }
5541
Bob Wilson6f34e272009-08-14 05:16:33 +00005542 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005543}
5544
Eli Friedmana5e244c2011-10-24 23:08:52 +00005545static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5546 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5547 SDValue Lane = Op.getOperand(2);
5548 if (!isa<ConstantSDNode>(Lane))
5549 return SDValue();
5550
5551 return Op;
5552}
5553
Bob Wilson2e076c42009-06-22 23:27:02 +00005554static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005555 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005556 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005557 if (!isa<ConstantSDNode>(Lane))
5558 return SDValue();
5559
5560 SDValue Vec = Op.getOperand(0);
5561 if (Op.getValueType() == MVT::i32 &&
5562 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005563 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005564 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5565 }
5566
5567 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005568}
5569
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005570static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5571 // The only time a CONCAT_VECTORS operation can have legal types is when
5572 // two 64-bit vectors are concatenated to a 128-bit vector.
5573 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5574 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005575 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005576 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005577 SDValue Op0 = Op.getOperand(0);
5578 SDValue Op1 = Op.getOperand(1);
5579 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005580 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005581 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005582 DAG.getIntPtrConstant(0));
5583 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005584 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005585 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005586 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005587 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005588}
5589
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005590/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5591/// element has been zero/sign-extended, depending on the isSigned parameter,
5592/// from an integer type half its size.
5593static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5594 bool isSigned) {
5595 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5596 EVT VT = N->getValueType(0);
5597 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5598 SDNode *BVN = N->getOperand(0).getNode();
5599 if (BVN->getValueType(0) != MVT::v4i32 ||
5600 BVN->getOpcode() != ISD::BUILD_VECTOR)
5601 return false;
5602 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5603 unsigned HiElt = 1 - LoElt;
5604 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5605 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5606 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5607 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5608 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5609 return false;
5610 if (isSigned) {
5611 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5612 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5613 return true;
5614 } else {
5615 if (Hi0->isNullValue() && Hi1->isNullValue())
5616 return true;
5617 }
5618 return false;
5619 }
5620
5621 if (N->getOpcode() != ISD::BUILD_VECTOR)
5622 return false;
5623
5624 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5625 SDNode *Elt = N->getOperand(i).getNode();
5626 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5627 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5628 unsigned HalfSize = EltSize / 2;
5629 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005630 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005631 return false;
5632 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005633 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005634 return false;
5635 }
5636 continue;
5637 }
5638 return false;
5639 }
5640
5641 return true;
5642}
5643
5644/// isSignExtended - Check if a node is a vector value that is sign-extended
5645/// or a constant BUILD_VECTOR with sign-extended elements.
5646static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5647 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5648 return true;
5649 if (isExtendedBUILD_VECTOR(N, DAG, true))
5650 return true;
5651 return false;
5652}
5653
5654/// isZeroExtended - Check if a node is a vector value that is zero-extended
5655/// or a constant BUILD_VECTOR with zero-extended elements.
5656static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5657 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5658 return true;
5659 if (isExtendedBUILD_VECTOR(N, DAG, false))
5660 return true;
5661 return false;
5662}
5663
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005664static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5665 if (OrigVT.getSizeInBits() >= 64)
5666 return OrigVT;
5667
5668 assert(OrigVT.isSimple() && "Expecting a simple value type");
5669
5670 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5671 switch (OrigSimpleTy) {
5672 default: llvm_unreachable("Unexpected Vector Type");
5673 case MVT::v2i8:
5674 case MVT::v2i16:
5675 return MVT::v2i32;
5676 case MVT::v4i8:
5677 return MVT::v4i16;
5678 }
5679}
5680
Sebastian Popa204f722012-11-30 19:08:04 +00005681/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5682/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5683/// We insert the required extension here to get the vector to fill a D register.
5684static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5685 const EVT &OrigTy,
5686 const EVT &ExtTy,
5687 unsigned ExtOpcode) {
5688 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5689 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5690 // 64-bits we need to insert a new extension so that it will be 64-bits.
5691 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5692 if (OrigTy.getSizeInBits() >= 64)
5693 return N;
5694
5695 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005696 EVT NewVT = getExtensionTo64Bits(OrigTy);
5697
Andrew Trickef9de2a2013-05-25 02:42:55 +00005698 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005699}
5700
5701/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5702/// does not do any sign/zero extension. If the original vector is less
5703/// than 64 bits, an appropriate extension will be added after the load to
5704/// reach a total size of 64 bits. We have to add the extension separately
5705/// because ARM does not have a sign/zero extending load for vectors.
5706static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005707 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5708
5709 // The load already has the right type.
5710 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005711 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005712 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5713 LD->isNonTemporal(), LD->isInvariant(),
5714 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005715
5716 // We need to create a zextload/sextload. We cannot just create a load
5717 // followed by a zext/zext node because LowerMUL is also run during normal
5718 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005719 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005720 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5721 LD->getMemoryVT(), LD->isVolatile(),
5722 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005723}
5724
5725/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5726/// extending load, or BUILD_VECTOR with extended elements, return the
5727/// unextended value. The unextended vector should be 64 bits so that it can
5728/// be used as an operand to a VMULL instruction. If the original vector size
5729/// before extension is less than 64 bits we add a an extension to resize
5730/// the vector to 64 bits.
5731static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005732 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005733 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5734 N->getOperand(0)->getValueType(0),
5735 N->getValueType(0),
5736 N->getOpcode());
5737
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005738 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005739 return SkipLoadExtensionForVMULL(LD, DAG);
5740
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005741 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5742 // have been legalized as a BITCAST from v4i32.
5743 if (N->getOpcode() == ISD::BITCAST) {
5744 SDNode *BVN = N->getOperand(0).getNode();
5745 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5746 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5747 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005748 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005749 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5750 }
5751 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5752 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5753 EVT VT = N->getValueType(0);
5754 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5755 unsigned NumElts = VT.getVectorNumElements();
5756 MVT TruncVT = MVT::getIntegerVT(EltSize);
5757 SmallVector<SDValue, 8> Ops;
5758 for (unsigned i = 0; i != NumElts; ++i) {
5759 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5760 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005761 // Element types smaller than 32 bits are not legal, so use i32 elements.
5762 // The values are implicitly truncated so sext vs. zext doesn't matter.
5763 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005764 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005765 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005766 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005767}
5768
Evan Chenge2086e72011-03-29 01:56:09 +00005769static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5770 unsigned Opcode = N->getOpcode();
5771 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5772 SDNode *N0 = N->getOperand(0).getNode();
5773 SDNode *N1 = N->getOperand(1).getNode();
5774 return N0->hasOneUse() && N1->hasOneUse() &&
5775 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5776 }
5777 return false;
5778}
5779
5780static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5781 unsigned Opcode = N->getOpcode();
5782 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5783 SDNode *N0 = N->getOperand(0).getNode();
5784 SDNode *N1 = N->getOperand(1).getNode();
5785 return N0->hasOneUse() && N1->hasOneUse() &&
5786 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5787 }
5788 return false;
5789}
5790
Bob Wilson38ab35a2010-09-01 23:50:19 +00005791static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5792 // Multiplications are only custom-lowered for 128-bit vectors so that
5793 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5794 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005795 assert(VT.is128BitVector() && VT.isInteger() &&
5796 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005797 SDNode *N0 = Op.getOperand(0).getNode();
5798 SDNode *N1 = Op.getOperand(1).getNode();
5799 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005800 bool isMLA = false;
5801 bool isN0SExt = isSignExtended(N0, DAG);
5802 bool isN1SExt = isSignExtended(N1, DAG);
5803 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005804 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005805 else {
5806 bool isN0ZExt = isZeroExtended(N0, DAG);
5807 bool isN1ZExt = isZeroExtended(N1, DAG);
5808 if (isN0ZExt && isN1ZExt)
5809 NewOpc = ARMISD::VMULLu;
5810 else if (isN1SExt || isN1ZExt) {
5811 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5812 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5813 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5814 NewOpc = ARMISD::VMULLs;
5815 isMLA = true;
5816 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5817 NewOpc = ARMISD::VMULLu;
5818 isMLA = true;
5819 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5820 std::swap(N0, N1);
5821 NewOpc = ARMISD::VMULLu;
5822 isMLA = true;
5823 }
5824 }
5825
5826 if (!NewOpc) {
5827 if (VT == MVT::v2i64)
5828 // Fall through to expand this. It is not legal.
5829 return SDValue();
5830 else
5831 // Other vector multiplications are legal.
5832 return Op;
5833 }
5834 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005835
5836 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005837 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005838 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005839 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005840 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005841 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005842 assert(Op0.getValueType().is64BitVector() &&
5843 Op1.getValueType().is64BitVector() &&
5844 "unexpected types for extended operands to VMULL");
5845 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5846 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005847
Evan Chenge2086e72011-03-29 01:56:09 +00005848 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5849 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5850 // vmull q0, d4, d6
5851 // vmlal q0, d5, d6
5852 // is faster than
5853 // vaddl q0, d4, d5
5854 // vmovl q1, d6
5855 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005856 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5857 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005858 EVT Op1VT = Op1.getValueType();
5859 return DAG.getNode(N0->getOpcode(), DL, VT,
5860 DAG.getNode(NewOpc, DL, VT,
5861 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5862 DAG.getNode(NewOpc, DL, VT,
5863 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005864}
5865
Owen Anderson77aa2662011-04-05 21:48:57 +00005866static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005867LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005868 // Convert to float
5869 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5870 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5871 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5872 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5873 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5874 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5875 // Get reciprocal estimate.
5876 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005877 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005878 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5879 // Because char has a smaller range than uchar, we can actually get away
5880 // without any newton steps. This requires that we use a weird bias
5881 // of 0xb000, however (again, this has been exhaustively tested).
5882 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5883 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5884 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5885 Y = DAG.getConstant(0xb000, MVT::i32);
5886 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5887 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5888 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5889 // Convert back to short.
5890 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5891 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5892 return X;
5893}
5894
Owen Anderson77aa2662011-04-05 21:48:57 +00005895static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005896LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005897 SDValue N2;
5898 // Convert to float.
5899 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5900 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5901 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5902 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5903 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5904 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005905
Nate Begemanfa62d502011-02-11 20:53:29 +00005906 // Use reciprocal estimate and one refinement step.
5907 // float4 recip = vrecpeq_f32(yf);
5908 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005909 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005910 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005911 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005912 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5913 N1, N2);
5914 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5915 // Because short has a smaller range than ushort, we can actually get away
5916 // with only a single newton step. This requires that we use a weird bias
5917 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005918 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005919 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5920 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005921 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005922 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5923 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5924 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5925 // Convert back to integer and return.
5926 // return vmovn_s32(vcvt_s32_f32(result));
5927 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5928 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5929 return N0;
5930}
5931
5932static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5933 EVT VT = Op.getValueType();
5934 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5935 "unexpected type for custom-lowering ISD::SDIV");
5936
Andrew Trickef9de2a2013-05-25 02:42:55 +00005937 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005938 SDValue N0 = Op.getOperand(0);
5939 SDValue N1 = Op.getOperand(1);
5940 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005941
Nate Begemanfa62d502011-02-11 20:53:29 +00005942 if (VT == MVT::v8i8) {
5943 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5944 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005945
Nate Begemanfa62d502011-02-11 20:53:29 +00005946 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5947 DAG.getIntPtrConstant(4));
5948 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005949 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005950 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5951 DAG.getIntPtrConstant(0));
5952 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5953 DAG.getIntPtrConstant(0));
5954
5955 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5956 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5957
5958 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5959 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005960
Nate Begemanfa62d502011-02-11 20:53:29 +00005961 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5962 return N0;
5963 }
5964 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5965}
5966
5967static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5968 EVT VT = Op.getValueType();
5969 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5970 "unexpected type for custom-lowering ISD::UDIV");
5971
Andrew Trickef9de2a2013-05-25 02:42:55 +00005972 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005973 SDValue N0 = Op.getOperand(0);
5974 SDValue N1 = Op.getOperand(1);
5975 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005976
Nate Begemanfa62d502011-02-11 20:53:29 +00005977 if (VT == MVT::v8i8) {
5978 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5979 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005980
Nate Begemanfa62d502011-02-11 20:53:29 +00005981 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5982 DAG.getIntPtrConstant(4));
5983 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005984 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005985 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5986 DAG.getIntPtrConstant(0));
5987 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5988 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00005989
Nate Begemanfa62d502011-02-11 20:53:29 +00005990 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5991 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00005992
Nate Begemanfa62d502011-02-11 20:53:29 +00005993 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5994 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005995
5996 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00005997 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5998 N0);
5999 return N0;
6000 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006001
Nate Begemanfa62d502011-02-11 20:53:29 +00006002 // v4i16 sdiv ... Convert to float.
6003 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6004 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6005 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6006 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6007 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006008 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006009
6010 // Use reciprocal estimate and two refinement steps.
6011 // float4 recip = vrecpeq_f32(yf);
6012 // recip *= vrecpsq_f32(yf, recip);
6013 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006014 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006015 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006016 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006017 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006018 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006019 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006020 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006021 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006022 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006023 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6024 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6025 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6026 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006027 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006028 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6029 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6030 N1 = DAG.getConstant(2, MVT::i32);
6031 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6032 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6033 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6034 // Convert back to integer and return.
6035 // return vmovn_u32(vcvt_s32_f32(result));
6036 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6037 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6038 return N0;
6039}
6040
Evan Chenge8916542011-08-30 01:34:54 +00006041static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6042 EVT VT = Op.getNode()->getValueType(0);
6043 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6044
6045 unsigned Opc;
6046 bool ExtraOp = false;
6047 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006048 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006049 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6050 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6051 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6052 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6053 }
6054
6055 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006056 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006057 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006058 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006059 Op.getOperand(1), Op.getOperand(2));
6060}
6061
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006062SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6063 assert(Subtarget->isTargetDarwin());
6064
6065 // For iOS, we want to call an alternative entry point: __sincos_stret,
6066 // return values are passed via sret.
6067 SDLoc dl(Op);
6068 SDValue Arg = Op.getOperand(0);
6069 EVT ArgVT = Arg.getValueType();
6070 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6071
6072 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6073 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6074
6075 // Pair of floats / doubles used to pass the result.
6076 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
6077
6078 // Create stack object for sret.
6079 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6080 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6081 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6082 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6083
6084 ArgListTy Args;
6085 ArgListEntry Entry;
6086
6087 Entry.Node = SRet;
6088 Entry.Ty = RetTy->getPointerTo();
6089 Entry.isSExt = false;
6090 Entry.isZExt = false;
6091 Entry.isSRet = true;
6092 Args.push_back(Entry);
6093
6094 Entry.Node = Arg;
6095 Entry.Ty = ArgTy;
6096 Entry.isSExt = false;
6097 Entry.isZExt = false;
6098 Args.push_back(Entry);
6099
6100 const char *LibcallName = (ArgVT == MVT::f64)
6101 ? "__sincos_stret" : "__sincosf_stret";
6102 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6103
6104 TargetLowering::
6105 CallLoweringInfo CLI(DAG.getEntryNode(), Type::getVoidTy(*DAG.getContext()),
6106 false, false, false, false, 0,
6107 CallingConv::C, /*isTaillCall=*/false,
6108 /*doesNotRet=*/false, /*isReturnValueUsed*/false,
6109 Callee, Args, DAG, dl);
6110 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6111
6112 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6113 MachinePointerInfo(), false, false, false, 0);
6114
6115 // Address of cos field.
6116 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6117 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6118 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6119 MachinePointerInfo(), false, false, false, 0);
6120
6121 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6122 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6123 LoadSin.getValue(0), LoadCos.getValue(0));
6124}
6125
Eli Friedman10f9ce22011-09-15 22:26:18 +00006126static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006127 // Monotonic load/store is legal for all targets
6128 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6129 return Op;
6130
Alp Tokercb402912014-01-24 17:20:08 +00006131 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006132 // dmb or equivalent available.
6133 return SDValue();
6134}
6135
Tim Northoverbc933082013-05-23 19:11:20 +00006136static void ReplaceREADCYCLECOUNTER(SDNode *N,
6137 SmallVectorImpl<SDValue> &Results,
6138 SelectionDAG &DAG,
6139 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006140 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006141 SDValue Cycles32, OutChain;
6142
6143 if (Subtarget->hasPerfMon()) {
6144 // Under Power Management extensions, the cycle-count is:
6145 // mrc p15, #0, <Rt>, c9, c13, #0
6146 SDValue Ops[] = { N->getOperand(0), // Chain
6147 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6148 DAG.getConstant(15, MVT::i32),
6149 DAG.getConstant(0, MVT::i32),
6150 DAG.getConstant(9, MVT::i32),
6151 DAG.getConstant(13, MVT::i32),
6152 DAG.getConstant(0, MVT::i32)
6153 };
6154
6155 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006156 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006157 OutChain = Cycles32.getValue(1);
6158 } else {
6159 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6160 // there are older ARM CPUs that have implementation-specific ways of
6161 // obtaining this information (FIXME!).
6162 Cycles32 = DAG.getConstant(0, MVT::i32);
6163 OutChain = DAG.getEntryNode();
6164 }
6165
6166
6167 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6168 Cycles32, DAG.getConstant(0, MVT::i32));
6169 Results.push_back(Cycles64);
6170 Results.push_back(OutChain);
6171}
6172
Dan Gohman21cea8a2010-04-17 15:26:15 +00006173SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006174 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006175 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006176 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006177 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006178 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006179 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6180 default: llvm_unreachable("unknown object format");
6181 case Triple::COFF:
6182 return LowerGlobalAddressWindows(Op, DAG);
6183 case Triple::ELF:
6184 return LowerGlobalAddressELF(Op, DAG);
6185 case Triple::MachO:
6186 return LowerGlobalAddressDarwin(Op, DAG);
6187 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006188 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006189 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006190 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6191 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006192 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006193 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006194 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006195 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006196 case ISD::SINT_TO_FP:
6197 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6198 case ISD::FP_TO_SINT:
6199 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006200 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006201 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006202 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006203 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006204 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006205 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006206 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6207 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006208 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006209 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006210 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006211 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006212 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006213 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006214 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006215 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006216 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006217 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006218 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006219 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006220 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006221 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006222 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006223 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006224 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006225 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006226 case ISD::SDIV: return LowerSDIV(Op, DAG);
6227 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006228 case ISD::ADDC:
6229 case ISD::ADDE:
6230 case ISD::SUBC:
6231 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006232 case ISD::SADDO:
6233 case ISD::UADDO:
6234 case ISD::SSUBO:
6235 case ISD::USUBO:
6236 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006237 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006238 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006239 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006240 case ISD::SDIVREM:
6241 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006242 }
Evan Cheng10043e22007-01-19 07:51:42 +00006243}
6244
Duncan Sands6ed40142008-12-01 11:39:25 +00006245/// ReplaceNodeResults - Replace the results of node with an illegal result
6246/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006247void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6248 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006249 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006250 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006251 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006252 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006253 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006254 case ISD::BITCAST:
6255 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006256 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006257 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006258 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006259 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006260 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006261 case ISD::READCYCLECOUNTER:
6262 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6263 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006264 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006265 if (Res.getNode())
6266 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006267}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006268
Evan Cheng10043e22007-01-19 07:51:42 +00006269//===----------------------------------------------------------------------===//
6270// ARM Scheduler Hooks
6271//===----------------------------------------------------------------------===//
6272
Bill Wendling030b58e2011-10-06 22:18:16 +00006273/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6274/// registers the function context.
6275void ARMTargetLowering::
6276SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6277 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendling374ee192011-10-03 21:25:38 +00006278 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6279 DebugLoc dl = MI->getDebugLoc();
6280 MachineFunction *MF = MBB->getParent();
6281 MachineRegisterInfo *MRI = &MF->getRegInfo();
6282 MachineConstantPool *MCP = MF->getConstantPool();
6283 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6284 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006285
Bill Wendling374ee192011-10-03 21:25:38 +00006286 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006287 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006288
Bill Wendling374ee192011-10-03 21:25:38 +00006289 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006290 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006291 ARMConstantPoolValue *CPV =
6292 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6293 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6294
Craig Topperc7242e02012-04-20 07:30:17 +00006295 const TargetRegisterClass *TRC = isThumb ?
6296 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6297 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006298
Bill Wendling030b58e2011-10-06 22:18:16 +00006299 // Grab constant pool and fixed stack memory operands.
6300 MachineMemOperand *CPMMO =
6301 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6302 MachineMemOperand::MOLoad, 4, 4);
6303
6304 MachineMemOperand *FIMMOSt =
6305 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6306 MachineMemOperand::MOStore, 4, 4);
6307
6308 // Load the address of the dispatch MBB into the jump buffer.
6309 if (isThumb2) {
6310 // Incoming value: jbuf
6311 // ldr.n r5, LCPI1_1
6312 // orr r5, r5, #1
6313 // add r5, pc
6314 // str r5, [$jbuf, #+4] ; &jbuf[1]
6315 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6316 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6317 .addConstantPoolIndex(CPI)
6318 .addMemOperand(CPMMO));
6319 // Set the low bit because of thumb mode.
6320 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6321 AddDefaultCC(
6322 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6323 .addReg(NewVReg1, RegState::Kill)
6324 .addImm(0x01)));
6325 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6326 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6327 .addReg(NewVReg2, RegState::Kill)
6328 .addImm(PCLabelId);
6329 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6330 .addReg(NewVReg3, RegState::Kill)
6331 .addFrameIndex(FI)
6332 .addImm(36) // &jbuf[1] :: pc
6333 .addMemOperand(FIMMOSt));
6334 } else if (isThumb) {
6335 // Incoming value: jbuf
6336 // ldr.n r1, LCPI1_4
6337 // add r1, pc
6338 // mov r2, #1
6339 // orrs r1, r2
6340 // add r2, $jbuf, #+4 ; &jbuf[1]
6341 // str r1, [r2]
6342 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6343 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6344 .addConstantPoolIndex(CPI)
6345 .addMemOperand(CPMMO));
6346 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6347 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6348 .addReg(NewVReg1, RegState::Kill)
6349 .addImm(PCLabelId);
6350 // Set the low bit because of thumb mode.
6351 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6352 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6353 .addReg(ARM::CPSR, RegState::Define)
6354 .addImm(1));
6355 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6356 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6357 .addReg(ARM::CPSR, RegState::Define)
6358 .addReg(NewVReg2, RegState::Kill)
6359 .addReg(NewVReg3, RegState::Kill));
6360 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6361 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6362 .addFrameIndex(FI)
6363 .addImm(36)); // &jbuf[1] :: pc
6364 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6365 .addReg(NewVReg4, RegState::Kill)
6366 .addReg(NewVReg5, RegState::Kill)
6367 .addImm(0)
6368 .addMemOperand(FIMMOSt));
6369 } else {
6370 // Incoming value: jbuf
6371 // ldr r1, LCPI1_1
6372 // add r1, pc, r1
6373 // str r1, [$jbuf, #+4] ; &jbuf[1]
6374 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6375 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6376 .addConstantPoolIndex(CPI)
6377 .addImm(0)
6378 .addMemOperand(CPMMO));
6379 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6380 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6381 .addReg(NewVReg1, RegState::Kill)
6382 .addImm(PCLabelId));
6383 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6384 .addReg(NewVReg2, RegState::Kill)
6385 .addFrameIndex(FI)
6386 .addImm(36) // &jbuf[1] :: pc
6387 .addMemOperand(FIMMOSt));
6388 }
6389}
6390
6391MachineBasicBlock *ARMTargetLowering::
6392EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6393 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6394 DebugLoc dl = MI->getDebugLoc();
6395 MachineFunction *MF = MBB->getParent();
6396 MachineRegisterInfo *MRI = &MF->getRegInfo();
6397 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6398 MachineFrameInfo *MFI = MF->getFrameInfo();
6399 int FI = MFI->getFunctionContextIndex();
6400
Craig Topperc7242e02012-04-20 07:30:17 +00006401 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6402 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006403 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006404
Bill Wendling362c1b02011-10-06 21:29:56 +00006405 // Get a mapping of the call site numbers to all of the landing pads they're
6406 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006407 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6408 unsigned MaxCSNum = 0;
6409 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006410 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6411 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006412 if (!BB->isLandingPad()) continue;
6413
6414 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6415 // pad.
6416 for (MachineBasicBlock::iterator
6417 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6418 if (!II->isEHLabel()) continue;
6419
6420 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006421 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006422
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006423 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6424 for (SmallVectorImpl<unsigned>::iterator
6425 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6426 CSI != CSE; ++CSI) {
6427 CallSiteNumToLPad[*CSI].push_back(BB);
6428 MaxCSNum = std::max(MaxCSNum, *CSI);
6429 }
Bill Wendling202803e2011-10-05 00:02:33 +00006430 break;
6431 }
6432 }
6433
6434 // Get an ordered list of the machine basic blocks for the jump table.
6435 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006436 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006437 LPadList.reserve(CallSiteNumToLPad.size());
6438 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6439 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6440 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006441 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006442 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006443 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6444 }
Bill Wendling202803e2011-10-05 00:02:33 +00006445 }
6446
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006447 assert(!LPadList.empty() &&
6448 "No landing pad destinations for the dispatch jump table!");
6449
Bill Wendling362c1b02011-10-06 21:29:56 +00006450 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006451 MachineJumpTableInfo *JTI =
6452 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6453 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6454 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006455 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006456
Bill Wendling362c1b02011-10-06 21:29:56 +00006457 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006458
6459 // Shove the dispatch's address into the return slot in the function context.
6460 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6461 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006462
Bill Wendling324be982011-10-05 00:39:32 +00006463 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006464 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006465 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006466 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006467 else
6468 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6469
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006470 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006471 DispatchBB->addSuccessor(TrapBB);
6472
6473 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6474 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006475
Bill Wendling510fbcd2011-10-17 21:32:56 +00006476 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006477 MF->insert(MF->end(), DispatchBB);
6478 MF->insert(MF->end(), DispContBB);
6479 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006480
Bill Wendling030b58e2011-10-06 22:18:16 +00006481 // Insert code into the entry block that creates and registers the function
6482 // context.
6483 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6484
Bill Wendling030b58e2011-10-06 22:18:16 +00006485 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006486 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006487 MachineMemOperand::MOLoad |
6488 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006489
Chad Rosier1ec8e402012-11-06 23:05:24 +00006490 MachineInstrBuilder MIB;
6491 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6492
6493 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6494 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6495
6496 // Add a register mask with no preserved registers. This results in all
6497 // registers being marked as clobbered.
6498 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006499
Bill Wendling85833f72011-10-18 22:49:07 +00006500 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006501 if (Subtarget->isThumb2()) {
6502 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6503 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6504 .addFrameIndex(FI)
6505 .addImm(4)
6506 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006507
Bill Wendling85833f72011-10-18 22:49:07 +00006508 if (NumLPads < 256) {
6509 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6510 .addReg(NewVReg1)
6511 .addImm(LPadList.size()));
6512 } else {
6513 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6514 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006515 .addImm(NumLPads & 0xFFFF));
6516
6517 unsigned VReg2 = VReg1;
6518 if ((NumLPads & 0xFFFF0000) != 0) {
6519 VReg2 = MRI->createVirtualRegister(TRC);
6520 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6521 .addReg(VReg1)
6522 .addImm(NumLPads >> 16));
6523 }
6524
Bill Wendling85833f72011-10-18 22:49:07 +00006525 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6526 .addReg(NewVReg1)
6527 .addReg(VReg2));
6528 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006529
Bill Wendling5626c662011-10-06 22:53:00 +00006530 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6531 .addMBB(TrapBB)
6532 .addImm(ARMCC::HI)
6533 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006534
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006535 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6536 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006537 .addJumpTableIndex(MJTI)
6538 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006539
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006540 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006541 AddDefaultCC(
6542 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006543 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6544 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006545 .addReg(NewVReg1)
6546 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6547
6548 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006549 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006550 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006551 .addJumpTableIndex(MJTI)
6552 .addImm(UId);
6553 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006554 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6555 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6556 .addFrameIndex(FI)
6557 .addImm(1)
6558 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006559
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006560 if (NumLPads < 256) {
6561 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6562 .addReg(NewVReg1)
6563 .addImm(NumLPads));
6564 } else {
6565 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006566 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6567 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6568
6569 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006570 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006571 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006572 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006573 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006574
6575 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6576 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6577 .addReg(VReg1, RegState::Define)
6578 .addConstantPoolIndex(Idx));
6579 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6580 .addReg(NewVReg1)
6581 .addReg(VReg1));
6582 }
6583
Bill Wendlingb3d46782011-10-06 23:37:36 +00006584 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6585 .addMBB(TrapBB)
6586 .addImm(ARMCC::HI)
6587 .addReg(ARM::CPSR);
6588
6589 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6590 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6591 .addReg(ARM::CPSR, RegState::Define)
6592 .addReg(NewVReg1)
6593 .addImm(2));
6594
6595 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006596 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006597 .addJumpTableIndex(MJTI)
6598 .addImm(UId));
6599
6600 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6601 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6602 .addReg(ARM::CPSR, RegState::Define)
6603 .addReg(NewVReg2, RegState::Kill)
6604 .addReg(NewVReg3));
6605
6606 MachineMemOperand *JTMMOLd =
6607 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6608 MachineMemOperand::MOLoad, 4, 4);
6609
6610 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6611 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6612 .addReg(NewVReg4, RegState::Kill)
6613 .addImm(0)
6614 .addMemOperand(JTMMOLd));
6615
Chad Rosier96603432013-03-01 18:30:38 +00006616 unsigned NewVReg6 = NewVReg5;
6617 if (RelocM == Reloc::PIC_) {
6618 NewVReg6 = MRI->createVirtualRegister(TRC);
6619 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6620 .addReg(ARM::CPSR, RegState::Define)
6621 .addReg(NewVReg5, RegState::Kill)
6622 .addReg(NewVReg3));
6623 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006624
6625 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6626 .addReg(NewVReg6, RegState::Kill)
6627 .addJumpTableIndex(MJTI)
6628 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006629 } else {
6630 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6631 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6632 .addFrameIndex(FI)
6633 .addImm(4)
6634 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006635
Bill Wendling4969dcd2011-10-18 22:52:20 +00006636 if (NumLPads < 256) {
6637 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6638 .addReg(NewVReg1)
6639 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006640 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006641 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6642 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006643 .addImm(NumLPads & 0xFFFF));
6644
6645 unsigned VReg2 = VReg1;
6646 if ((NumLPads & 0xFFFF0000) != 0) {
6647 VReg2 = MRI->createVirtualRegister(TRC);
6648 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6649 .addReg(VReg1)
6650 .addImm(NumLPads >> 16));
6651 }
6652
Bill Wendling4969dcd2011-10-18 22:52:20 +00006653 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6654 .addReg(NewVReg1)
6655 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006656 } else {
6657 MachineConstantPool *ConstantPool = MF->getConstantPool();
6658 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6659 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6660
6661 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006662 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006663 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006664 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006665 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6666
6667 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6668 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6669 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006670 .addConstantPoolIndex(Idx)
6671 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006672 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6673 .addReg(NewVReg1)
6674 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006675 }
6676
Bill Wendling5626c662011-10-06 22:53:00 +00006677 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6678 .addMBB(TrapBB)
6679 .addImm(ARMCC::HI)
6680 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006681
Bill Wendling973c8172011-10-18 22:11:18 +00006682 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006683 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006684 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006685 .addReg(NewVReg1)
6686 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006687 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6688 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006689 .addJumpTableIndex(MJTI)
6690 .addImm(UId));
6691
6692 MachineMemOperand *JTMMOLd =
6693 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6694 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006695 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006696 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006697 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6698 .addReg(NewVReg3, RegState::Kill)
6699 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006700 .addImm(0)
6701 .addMemOperand(JTMMOLd));
6702
Chad Rosier96603432013-03-01 18:30:38 +00006703 if (RelocM == Reloc::PIC_) {
6704 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6705 .addReg(NewVReg5, RegState::Kill)
6706 .addReg(NewVReg4)
6707 .addJumpTableIndex(MJTI)
6708 .addImm(UId);
6709 } else {
6710 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6711 .addReg(NewVReg5, RegState::Kill)
6712 .addJumpTableIndex(MJTI)
6713 .addImm(UId);
6714 }
Bill Wendling5626c662011-10-06 22:53:00 +00006715 }
Bill Wendling202803e2011-10-05 00:02:33 +00006716
Bill Wendling324be982011-10-05 00:39:32 +00006717 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006718 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006719 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006720 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6721 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006722 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006723 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006724 }
6725
Bill Wendling26d27802011-10-17 05:25:09 +00006726 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006727 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006728 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006729 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6730 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6731 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006732
6733 // Remove the landing pad successor from the invoke block and replace it
6734 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006735 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6736 BB->succ_end());
6737 while (!Successors.empty()) {
6738 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006739 if (SMBB->isLandingPad()) {
6740 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006741 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006742 }
6743 }
6744
6745 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006746
6747 // Find the invoke call and mark all of the callee-saved registers as
6748 // 'implicit defined' so that they're spilled. This prevents code from
6749 // moving instructions to before the EH block, where they will never be
6750 // executed.
6751 for (MachineBasicBlock::reverse_iterator
6752 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006753 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006754
6755 DenseMap<unsigned, bool> DefRegs;
6756 for (MachineInstr::mop_iterator
6757 OI = II->operands_begin(), OE = II->operands_end();
6758 OI != OE; ++OI) {
6759 if (!OI->isReg()) continue;
6760 DefRegs[OI->getReg()] = true;
6761 }
6762
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006763 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006764
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006765 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006766 unsigned Reg = SavedRegs[i];
6767 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006768 !ARM::tGPRRegClass.contains(Reg) &&
6769 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006770 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006771 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006772 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006773 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006774 continue;
6775 if (!DefRegs[Reg])
6776 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006777 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006778
6779 break;
6780 }
Bill Wendling883ec972011-10-07 23:18:02 +00006781 }
Bill Wendling324be982011-10-05 00:39:32 +00006782
Bill Wendling617075f2011-10-18 18:30:49 +00006783 // Mark all former landing pads as non-landing pads. The dispatch is the only
6784 // landing pad now.
6785 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6786 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6787 (*I)->setIsLandingPad(false);
6788
Bill Wendling324be982011-10-05 00:39:32 +00006789 // The instruction is gone now.
6790 MI->eraseFromParent();
6791
Bill Wendling374ee192011-10-03 21:25:38 +00006792 return MBB;
6793}
6794
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006795static
6796MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6797 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6798 E = MBB->succ_end(); I != E; ++I)
6799 if (*I != Succ)
6800 return *I;
6801 llvm_unreachable("Expecting a BB with two successors!");
6802}
6803
Manman Renb504f492013-10-29 22:27:32 +00006804/// Return the load opcode for a given load size. If load size >= 8,
6805/// neon opcode will be returned.
6806static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6807 if (LdSize >= 8)
6808 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6809 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6810 if (IsThumb1)
6811 return LdSize == 4 ? ARM::tLDRi
6812 : LdSize == 2 ? ARM::tLDRHi
6813 : LdSize == 1 ? ARM::tLDRBi : 0;
6814 if (IsThumb2)
6815 return LdSize == 4 ? ARM::t2LDR_POST
6816 : LdSize == 2 ? ARM::t2LDRH_POST
6817 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6818 return LdSize == 4 ? ARM::LDR_POST_IMM
6819 : LdSize == 2 ? ARM::LDRH_POST
6820 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6821}
6822
6823/// Return the store opcode for a given store size. If store size >= 8,
6824/// neon opcode will be returned.
6825static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6826 if (StSize >= 8)
6827 return StSize == 16 ? ARM::VST1q32wb_fixed
6828 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6829 if (IsThumb1)
6830 return StSize == 4 ? ARM::tSTRi
6831 : StSize == 2 ? ARM::tSTRHi
6832 : StSize == 1 ? ARM::tSTRBi : 0;
6833 if (IsThumb2)
6834 return StSize == 4 ? ARM::t2STR_POST
6835 : StSize == 2 ? ARM::t2STRH_POST
6836 : StSize == 1 ? ARM::t2STRB_POST : 0;
6837 return StSize == 4 ? ARM::STR_POST_IMM
6838 : StSize == 2 ? ARM::STRH_POST
6839 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6840}
6841
6842/// Emit a post-increment load operation with given size. The instructions
6843/// will be added to BB at Pos.
6844static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6845 const TargetInstrInfo *TII, DebugLoc dl,
6846 unsigned LdSize, unsigned Data, unsigned AddrIn,
6847 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6848 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6849 assert(LdOpc != 0 && "Should have a load opcode");
6850 if (LdSize >= 8) {
6851 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6852 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6853 .addImm(0));
6854 } else if (IsThumb1) {
6855 // load + update AddrIn
6856 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6857 .addReg(AddrIn).addImm(0));
6858 MachineInstrBuilder MIB =
6859 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6860 MIB = AddDefaultT1CC(MIB);
6861 MIB.addReg(AddrIn).addImm(LdSize);
6862 AddDefaultPred(MIB);
6863 } else if (IsThumb2) {
6864 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6865 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6866 .addImm(LdSize));
6867 } else { // arm
6868 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6869 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6870 .addReg(0).addImm(LdSize));
6871 }
6872}
6873
6874/// Emit a post-increment store operation with given size. The instructions
6875/// will be added to BB at Pos.
6876static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6877 const TargetInstrInfo *TII, DebugLoc dl,
6878 unsigned StSize, unsigned Data, unsigned AddrIn,
6879 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6880 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6881 assert(StOpc != 0 && "Should have a store opcode");
6882 if (StSize >= 8) {
6883 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6884 .addReg(AddrIn).addImm(0).addReg(Data));
6885 } else if (IsThumb1) {
6886 // store + update AddrIn
6887 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6888 .addReg(AddrIn).addImm(0));
6889 MachineInstrBuilder MIB =
6890 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6891 MIB = AddDefaultT1CC(MIB);
6892 MIB.addReg(AddrIn).addImm(StSize);
6893 AddDefaultPred(MIB);
6894 } else if (IsThumb2) {
6895 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6896 .addReg(Data).addReg(AddrIn).addImm(StSize));
6897 } else { // arm
6898 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6899 .addReg(Data).addReg(AddrIn).addReg(0)
6900 .addImm(StSize));
6901 }
6902}
6903
David Peixottoc32e24a2013-10-17 19:49:22 +00006904MachineBasicBlock *
6905ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6906 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006907 // This pseudo instruction has 3 operands: dst, src, size
6908 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6909 // Otherwise, we will generate unrolled scalar copies.
6910 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6911 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6912 MachineFunction::iterator It = BB;
6913 ++It;
6914
6915 unsigned dest = MI->getOperand(0).getReg();
6916 unsigned src = MI->getOperand(1).getReg();
6917 unsigned SizeVal = MI->getOperand(2).getImm();
6918 unsigned Align = MI->getOperand(3).getImm();
6919 DebugLoc dl = MI->getDebugLoc();
6920
Manman Rene8735522012-06-01 19:33:18 +00006921 MachineFunction *MF = BB->getParent();
6922 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006923 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00006924 const TargetRegisterClass *TRC = nullptr;
6925 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006926
6927 bool IsThumb1 = Subtarget->isThumb1Only();
6928 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006929
6930 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006931 UnitSize = 1;
6932 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006933 UnitSize = 2;
6934 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006935 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006936 if (!MF->getFunction()->getAttributes().
6937 hasAttribute(AttributeSet::FunctionIndex,
6938 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00006939 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00006940 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00006941 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00006942 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00006943 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00006944 }
6945 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00006946 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00006947 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00006948 }
Manman Ren6e1fd462012-06-18 22:23:48 +00006949
David Peixottob0653e532013-10-24 16:39:36 +00006950 // Select the correct opcode and register class for unit size load/store
6951 bool IsNeon = UnitSize >= 8;
6952 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
6953 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00006954 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00006955 VecTRC = UnitSize == 16
6956 ? (const TargetRegisterClass *)&ARM::DPairRegClass
6957 : UnitSize == 8
6958 ? (const TargetRegisterClass *)&ARM::DPRRegClass
Craig Topper062a2ba2014-04-25 05:30:21 +00006959 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006960
Manman Rene8735522012-06-01 19:33:18 +00006961 unsigned BytesLeft = SizeVal % UnitSize;
6962 unsigned LoopSize = SizeVal - BytesLeft;
6963
6964 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6965 // Use LDR and STR to copy.
6966 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6967 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6968 unsigned srcIn = src;
6969 unsigned destIn = dest;
6970 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00006971 unsigned srcOut = MRI.createVirtualRegister(TRC);
6972 unsigned destOut = MRI.createVirtualRegister(TRC);
6973 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006974 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
6975 IsThumb1, IsThumb2);
6976 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
6977 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006978 srcIn = srcOut;
6979 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006980 }
6981
6982 // Handle the leftover bytes with LDRB and STRB.
6983 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6984 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00006985 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006986 unsigned srcOut = MRI.createVirtualRegister(TRC);
6987 unsigned destOut = MRI.createVirtualRegister(TRC);
6988 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006989 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
6990 IsThumb1, IsThumb2);
6991 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
6992 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006993 srcIn = srcOut;
6994 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006995 }
6996 MI->eraseFromParent(); // The instruction is gone now.
6997 return BB;
6998 }
6999
7000 // Expand the pseudo op to a loop.
7001 // thisMBB:
7002 // ...
7003 // movw varEnd, # --> with thumb2
7004 // movt varEnd, #
7005 // ldrcp varEnd, idx --> without thumb2
7006 // fallthrough --> loopMBB
7007 // loopMBB:
7008 // PHI varPhi, varEnd, varLoop
7009 // PHI srcPhi, src, srcLoop
7010 // PHI destPhi, dst, destLoop
7011 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7012 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7013 // subs varLoop, varPhi, #UnitSize
7014 // bne loopMBB
7015 // fallthrough --> exitMBB
7016 // exitMBB:
7017 // epilogue to handle left-over bytes
7018 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7019 // [destOut] = STRB_POST(scratch, destLoop, 1)
7020 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7021 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7022 MF->insert(It, loopMBB);
7023 MF->insert(It, exitMBB);
7024
7025 // Transfer the remainder of BB and its successor edges to exitMBB.
7026 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007027 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007028 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7029
7030 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007031 unsigned varEnd = MRI.createVirtualRegister(TRC);
7032 if (IsThumb2) {
7033 unsigned Vtmp = varEnd;
7034 if ((LoopSize & 0xFFFF0000) != 0)
7035 Vtmp = MRI.createVirtualRegister(TRC);
7036 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7037 .addImm(LoopSize & 0xFFFF));
7038
7039 if ((LoopSize & 0xFFFF0000) != 0)
7040 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7041 .addReg(Vtmp).addImm(LoopSize >> 16));
7042 } else {
7043 MachineConstantPool *ConstantPool = MF->getConstantPool();
7044 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7045 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7046
7047 // MachineConstantPool wants an explicit alignment.
7048 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7049 if (Align == 0)
7050 Align = getDataLayout()->getTypeAllocSize(C->getType());
7051 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7052
7053 if (IsThumb1)
7054 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7055 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7056 else
7057 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7058 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7059 }
Manman Rene8735522012-06-01 19:33:18 +00007060 BB->addSuccessor(loopMBB);
7061
7062 // Generate the loop body:
7063 // varPhi = PHI(varLoop, varEnd)
7064 // srcPhi = PHI(srcLoop, src)
7065 // destPhi = PHI(destLoop, dst)
7066 MachineBasicBlock *entryBB = BB;
7067 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007068 unsigned varLoop = MRI.createVirtualRegister(TRC);
7069 unsigned varPhi = MRI.createVirtualRegister(TRC);
7070 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7071 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7072 unsigned destLoop = MRI.createVirtualRegister(TRC);
7073 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007074
7075 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7076 .addReg(varLoop).addMBB(loopMBB)
7077 .addReg(varEnd).addMBB(entryBB);
7078 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7079 .addReg(srcLoop).addMBB(loopMBB)
7080 .addReg(src).addMBB(entryBB);
7081 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7082 .addReg(destLoop).addMBB(loopMBB)
7083 .addReg(dest).addMBB(entryBB);
7084
7085 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7086 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007087 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007088 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7089 IsThumb1, IsThumb2);
7090 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7091 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007092
7093 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007094 if (IsThumb1) {
7095 MachineInstrBuilder MIB =
7096 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7097 MIB = AddDefaultT1CC(MIB);
7098 MIB.addReg(varPhi).addImm(UnitSize);
7099 AddDefaultPred(MIB);
7100 } else {
7101 MachineInstrBuilder MIB =
7102 BuildMI(*BB, BB->end(), dl,
7103 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7104 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7105 MIB->getOperand(5).setReg(ARM::CPSR);
7106 MIB->getOperand(5).setIsDef(true);
7107 }
7108 BuildMI(*BB, BB->end(), dl,
7109 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7110 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007111
7112 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7113 BB->addSuccessor(loopMBB);
7114 BB->addSuccessor(exitMBB);
7115
7116 // Add epilogue to handle BytesLeft.
7117 BB = exitMBB;
7118 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007119
7120 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7121 // [destOut] = STRB_POST(scratch, destLoop, 1)
7122 unsigned srcIn = srcLoop;
7123 unsigned destIn = destLoop;
7124 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007125 unsigned srcOut = MRI.createVirtualRegister(TRC);
7126 unsigned destOut = MRI.createVirtualRegister(TRC);
7127 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007128 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7129 IsThumb1, IsThumb2);
7130 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7131 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007132 srcIn = srcOut;
7133 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007134 }
7135
7136 MI->eraseFromParent(); // The instruction is gone now.
7137 return BB;
7138}
7139
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007140MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007141ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007142 MachineBasicBlock *BB) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007143 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007144 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007145 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007146 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007147 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007148 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007149 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007150 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007151 // The Thumb2 pre-indexed stores have the same MI operands, they just
7152 // define them differently in the .td files from the isel patterns, so
7153 // they need pseudos.
7154 case ARM::t2STR_preidx:
7155 MI->setDesc(TII->get(ARM::t2STR_PRE));
7156 return BB;
7157 case ARM::t2STRB_preidx:
7158 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7159 return BB;
7160 case ARM::t2STRH_preidx:
7161 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7162 return BB;
7163
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007164 case ARM::STRi_preidx:
7165 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007166 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007167 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7168 // Decode the offset.
7169 unsigned Offset = MI->getOperand(4).getImm();
7170 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7171 Offset = ARM_AM::getAM2Offset(Offset);
7172 if (isSub)
7173 Offset = -Offset;
7174
Jim Grosbachf402f692011-08-12 21:02:34 +00007175 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007176 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007177 .addOperand(MI->getOperand(0)) // Rn_wb
7178 .addOperand(MI->getOperand(1)) // Rt
7179 .addOperand(MI->getOperand(2)) // Rn
7180 .addImm(Offset) // offset (skip GPR==zero_reg)
7181 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007182 .addOperand(MI->getOperand(6))
7183 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007184 MI->eraseFromParent();
7185 return BB;
7186 }
7187 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007188 case ARM::STRBr_preidx:
7189 case ARM::STRH_preidx: {
7190 unsigned NewOpc;
7191 switch (MI->getOpcode()) {
7192 default: llvm_unreachable("unexpected opcode!");
7193 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7194 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7195 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7196 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007197 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7198 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7199 MIB.addOperand(MI->getOperand(i));
7200 MI->eraseFromParent();
7201 return BB;
7202 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007203
Evan Chengbb2af352009-08-12 05:17:19 +00007204 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007205 // To "insert" a SELECT_CC instruction, we actually have to insert the
7206 // diamond control-flow pattern. The incoming instruction knows the
7207 // destination vreg to set, the condition code register to branch on, the
7208 // true/false values to select between, and a branch opcode to use.
7209 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007210 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007211 ++It;
7212
7213 // thisMBB:
7214 // ...
7215 // TrueVal = ...
7216 // cmpTY ccX, r1, r2
7217 // bCC copy1MBB
7218 // fallthrough --> copy0MBB
7219 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007220 MachineFunction *F = BB->getParent();
7221 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7222 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007223 F->insert(It, copy0MBB);
7224 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007225
7226 // Transfer the remainder of BB and its successor edges to sinkMBB.
7227 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007228 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007229 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7230
Dan Gohmanf4f04102010-07-06 15:49:48 +00007231 BB->addSuccessor(copy0MBB);
7232 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007233
Dan Gohman34396292010-07-06 20:24:04 +00007234 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7235 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7236
Evan Cheng10043e22007-01-19 07:51:42 +00007237 // copy0MBB:
7238 // %FalseValue = ...
7239 // # fallthrough to sinkMBB
7240 BB = copy0MBB;
7241
7242 // Update machine-CFG edges
7243 BB->addSuccessor(sinkMBB);
7244
7245 // sinkMBB:
7246 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7247 // ...
7248 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007249 BuildMI(*BB, BB->begin(), dl,
7250 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007251 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7252 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7253
Dan Gohman34396292010-07-06 20:24:04 +00007254 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007255 return BB;
7256 }
Evan Chengb972e562009-08-07 00:34:42 +00007257
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007258 case ARM::BCCi64:
7259 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007260 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007261 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007262
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007263 // Compare both parts that make up the double comparison separately for
7264 // equality.
7265 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7266
7267 unsigned LHS1 = MI->getOperand(1).getReg();
7268 unsigned LHS2 = MI->getOperand(2).getReg();
7269 if (RHSisZero) {
7270 AddDefaultPred(BuildMI(BB, dl,
7271 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7272 .addReg(LHS1).addImm(0));
7273 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7274 .addReg(LHS2).addImm(0)
7275 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7276 } else {
7277 unsigned RHS1 = MI->getOperand(3).getReg();
7278 unsigned RHS2 = MI->getOperand(4).getReg();
7279 AddDefaultPred(BuildMI(BB, dl,
7280 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7281 .addReg(LHS1).addReg(RHS1));
7282 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7283 .addReg(LHS2).addReg(RHS2)
7284 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7285 }
7286
7287 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7288 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7289 if (MI->getOperand(0).getImm() == ARMCC::NE)
7290 std::swap(destMBB, exitMBB);
7291
7292 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7293 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007294 if (isThumb2)
7295 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7296 else
7297 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007298
7299 MI->eraseFromParent(); // The pseudo instruction is gone now.
7300 return BB;
7301 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007302
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007303 case ARM::Int_eh_sjlj_setjmp:
7304 case ARM::Int_eh_sjlj_setjmp_nofp:
7305 case ARM::tInt_eh_sjlj_setjmp:
7306 case ARM::t2Int_eh_sjlj_setjmp:
7307 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7308 EmitSjLjDispatchBlock(MI, BB);
7309 return BB;
7310
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007311 case ARM::ABS:
7312 case ARM::t2ABS: {
7313 // To insert an ABS instruction, we have to insert the
7314 // diamond control-flow pattern. The incoming instruction knows the
7315 // source vreg to test against 0, the destination vreg to set,
7316 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007317 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007318 // It transforms
7319 // V1 = ABS V0
7320 // into
7321 // V2 = MOVS V0
7322 // BCC (branch to SinkBB if V0 >= 0)
7323 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007324 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007325 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7326 MachineFunction::iterator BBI = BB;
7327 ++BBI;
7328 MachineFunction *Fn = BB->getParent();
7329 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7330 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7331 Fn->insert(BBI, RSBBB);
7332 Fn->insert(BBI, SinkBB);
7333
7334 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7335 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7336 bool isThumb2 = Subtarget->isThumb2();
7337 MachineRegisterInfo &MRI = Fn->getRegInfo();
7338 // In Thumb mode S must not be specified if source register is the SP or
7339 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007340 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7341 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7342 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007343
7344 // Transfer the remainder of BB and its successor edges to sinkMBB.
7345 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007346 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007347 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7348
7349 BB->addSuccessor(RSBBB);
7350 BB->addSuccessor(SinkBB);
7351
7352 // fall through to SinkMBB
7353 RSBBB->addSuccessor(SinkBB);
7354
Manman Rene0763c72012-06-15 21:32:12 +00007355 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007356 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007357 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7358 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007359
7360 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007361 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007362 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7363 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7364
7365 // insert rsbri in RSBBB
7366 // Note: BCC and rsbri will be converted into predicated rsbmi
7367 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007368 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007369 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007370 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007371 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7372
Andrew Trick3f07c422011-10-18 18:40:53 +00007373 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007374 // reuse ABSDstReg to not change uses of ABS instruction
7375 BuildMI(*SinkBB, SinkBB->begin(), dl,
7376 TII->get(ARM::PHI), ABSDstReg)
7377 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007378 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007379
7380 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007381 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007382
7383 // return last added BB
7384 return SinkBB;
7385 }
Manman Rene8735522012-06-01 19:33:18 +00007386 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007387 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007388 return EmitStructByval(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007389 }
7390}
7391
Evan Chenge6fba772011-08-30 19:09:48 +00007392void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7393 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007394 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007395 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7396 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7397 return;
7398 }
7399
Evan Cheng7f8e5632011-12-07 07:15:52 +00007400 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007401 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7402 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7403 // operand is still set to noreg. If needed, set the optional operand's
7404 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007405 //
Andrew Trick88b24502011-10-18 19:18:52 +00007406 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007407
Andrew Trick924123a2011-09-21 02:20:46 +00007408 // Rename pseudo opcodes.
7409 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7410 if (NewOpc) {
7411 const ARMBaseInstrInfo *TII =
7412 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007413 MCID = &TII->get(NewOpc);
7414
7415 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7416 "converted opcode should be the same except for cc_out");
7417
7418 MI->setDesc(*MCID);
7419
7420 // Add the optional cc_out operand
7421 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007422 }
Andrew Trick88b24502011-10-18 19:18:52 +00007423 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007424
7425 // Any ARM instruction that sets the 's' bit should specify an optional
7426 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007427 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007428 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007429 return;
7430 }
Andrew Trick924123a2011-09-21 02:20:46 +00007431 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7432 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007433 bool definesCPSR = false;
7434 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007435 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007436 i != e; ++i) {
7437 const MachineOperand &MO = MI->getOperand(i);
7438 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7439 definesCPSR = true;
7440 if (MO.isDead())
7441 deadCPSR = true;
7442 MI->RemoveOperand(i);
7443 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007444 }
7445 }
Andrew Trick8586e622011-09-20 03:17:40 +00007446 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007447 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007448 return;
7449 }
7450 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007451 if (deadCPSR) {
7452 assert(!MI->getOperand(ccOutIdx).getReg() &&
7453 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007454 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007455 }
Andrew Trick8586e622011-09-20 03:17:40 +00007456
Andrew Trick924123a2011-09-21 02:20:46 +00007457 // If this instruction was defined with an optional CPSR def and its dag node
7458 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007459 MachineOperand &MO = MI->getOperand(ccOutIdx);
7460 MO.setReg(ARM::CPSR);
7461 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007462}
7463
Evan Cheng10043e22007-01-19 07:51:42 +00007464//===----------------------------------------------------------------------===//
7465// ARM Optimization Hooks
7466//===----------------------------------------------------------------------===//
7467
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007468// Helper function that checks if N is a null or all ones constant.
7469static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7470 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7471 if (!C)
7472 return false;
7473 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7474}
7475
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007476// Return true if N is conditionally 0 or all ones.
7477// Detects these expressions where cc is an i1 value:
7478//
7479// (select cc 0, y) [AllOnes=0]
7480// (select cc y, 0) [AllOnes=0]
7481// (zext cc) [AllOnes=0]
7482// (sext cc) [AllOnes=0/1]
7483// (select cc -1, y) [AllOnes=1]
7484// (select cc y, -1) [AllOnes=1]
7485//
7486// Invert is set when N is the null/all ones constant when CC is false.
7487// OtherOp is set to the alternative value of N.
7488static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7489 SDValue &CC, bool &Invert,
7490 SDValue &OtherOp,
7491 SelectionDAG &DAG) {
7492 switch (N->getOpcode()) {
7493 default: return false;
7494 case ISD::SELECT: {
7495 CC = N->getOperand(0);
7496 SDValue N1 = N->getOperand(1);
7497 SDValue N2 = N->getOperand(2);
7498 if (isZeroOrAllOnes(N1, AllOnes)) {
7499 Invert = false;
7500 OtherOp = N2;
7501 return true;
7502 }
7503 if (isZeroOrAllOnes(N2, AllOnes)) {
7504 Invert = true;
7505 OtherOp = N1;
7506 return true;
7507 }
7508 return false;
7509 }
7510 case ISD::ZERO_EXTEND:
7511 // (zext cc) can never be the all ones value.
7512 if (AllOnes)
7513 return false;
7514 // Fall through.
7515 case ISD::SIGN_EXTEND: {
7516 EVT VT = N->getValueType(0);
7517 CC = N->getOperand(0);
7518 if (CC.getValueType() != MVT::i1)
7519 return false;
7520 Invert = !AllOnes;
7521 if (AllOnes)
7522 // When looking for an AllOnes constant, N is an sext, and the 'other'
7523 // value is 0.
7524 OtherOp = DAG.getConstant(0, VT);
7525 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7526 // When looking for a 0 constant, N can be zext or sext.
7527 OtherOp = DAG.getConstant(1, VT);
7528 else
7529 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7530 return true;
7531 }
7532 }
7533}
7534
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007535// Combine a constant select operand into its use:
7536//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007537// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7538// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7539// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7540// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7541// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007542//
7543// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007544// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007545//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007546// Also recognize sext/zext from i1:
7547//
7548// (add (zext cc), x) -> (select cc (add x, 1), x)
7549// (add (sext cc), x) -> (select cc (add x, -1), x)
7550//
7551// These transformations eventually create predicated instructions.
7552//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007553// @param N The node to transform.
7554// @param Slct The N operand that is a select.
7555// @param OtherOp The other N operand (x above).
7556// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007557// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007558// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007559static
7560SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007561 TargetLowering::DAGCombinerInfo &DCI,
7562 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007563 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007564 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007565 SDValue NonConstantVal;
7566 SDValue CCOp;
7567 bool SwapSelectOps;
7568 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7569 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007570 return SDValue();
7571
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007572 // Slct is now know to be the desired identity constant when CC is true.
7573 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007574 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007575 OtherOp, NonConstantVal);
7576 // Unless SwapSelectOps says CC should be false.
7577 if (SwapSelectOps)
7578 std::swap(TrueVal, FalseVal);
7579
Andrew Trickef9de2a2013-05-25 02:42:55 +00007580 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007581 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007582}
7583
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007584// Attempt combineSelectAndUse on each operand of a commutative operator N.
7585static
7586SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7587 TargetLowering::DAGCombinerInfo &DCI) {
7588 SDValue N0 = N->getOperand(0);
7589 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007590 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007591 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7592 if (Result.getNode())
7593 return Result;
7594 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007595 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007596 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7597 if (Result.getNode())
7598 return Result;
7599 }
7600 return SDValue();
7601}
7602
Eric Christopher1b8b94192011-06-29 21:10:36 +00007603// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007604// (only after legalization).
7605static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7606 TargetLowering::DAGCombinerInfo &DCI,
7607 const ARMSubtarget *Subtarget) {
7608
7609 // Only perform optimization if after legalize, and if NEON is available. We
7610 // also expected both operands to be BUILD_VECTORs.
7611 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7612 || N0.getOpcode() != ISD::BUILD_VECTOR
7613 || N1.getOpcode() != ISD::BUILD_VECTOR)
7614 return SDValue();
7615
7616 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7617 EVT VT = N->getValueType(0);
7618 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7619 return SDValue();
7620
7621 // Check that the vector operands are of the right form.
7622 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7623 // operands, where N is the size of the formed vector.
7624 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7625 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007626
7627 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007628 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007629 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007630 SDValue Vec = N0->getOperand(0)->getOperand(0);
7631 SDNode *V = Vec.getNode();
7632 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007633
Eric Christopher1b8b94192011-06-29 21:10:36 +00007634 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007635 // check to see if each of their operands are an EXTRACT_VECTOR with
7636 // the same vector and appropriate index.
7637 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7638 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7639 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007640
Tanya Lattnere9e67052011-06-14 23:48:48 +00007641 SDValue ExtVec0 = N0->getOperand(i);
7642 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007643
Tanya Lattnere9e67052011-06-14 23:48:48 +00007644 // First operand is the vector, verify its the same.
7645 if (V != ExtVec0->getOperand(0).getNode() ||
7646 V != ExtVec1->getOperand(0).getNode())
7647 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007648
Tanya Lattnere9e67052011-06-14 23:48:48 +00007649 // Second is the constant, verify its correct.
7650 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7651 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007652
Tanya Lattnere9e67052011-06-14 23:48:48 +00007653 // For the constant, we want to see all the even or all the odd.
7654 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7655 || C1->getZExtValue() != nextIndex+1)
7656 return SDValue();
7657
7658 // Increment index.
7659 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007660 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007661 return SDValue();
7662 }
7663
7664 // Create VPADDL node.
7665 SelectionDAG &DAG = DCI.DAG;
7666 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007667
7668 // Build operand list.
7669 SmallVector<SDValue, 8> Ops;
7670 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7671 TLI.getPointerTy()));
7672
7673 // Input is the vector.
7674 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007675
Tanya Lattnere9e67052011-06-14 23:48:48 +00007676 // Get widened type and narrowed type.
7677 MVT widenType;
7678 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007679
7680 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7681 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007682 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7683 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7684 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7685 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007686 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007687 }
7688
Craig Topper48d114b2014-04-26 18:35:24 +00007689 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007690 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7691 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007692}
7693
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007694static SDValue findMUL_LOHI(SDValue V) {
7695 if (V->getOpcode() == ISD::UMUL_LOHI ||
7696 V->getOpcode() == ISD::SMUL_LOHI)
7697 return V;
7698 return SDValue();
7699}
7700
7701static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7702 TargetLowering::DAGCombinerInfo &DCI,
7703 const ARMSubtarget *Subtarget) {
7704
7705 if (Subtarget->isThumb1Only()) return SDValue();
7706
7707 // Only perform the checks after legalize when the pattern is available.
7708 if (DCI.isBeforeLegalize()) return SDValue();
7709
7710 // Look for multiply add opportunities.
7711 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7712 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7713 // a glue link from the first add to the second add.
7714 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7715 // a S/UMLAL instruction.
7716 // loAdd UMUL_LOHI
7717 // \ / :lo \ :hi
7718 // \ / \ [no multiline comment]
7719 // ADDC | hiAdd
7720 // \ :glue / /
7721 // \ / /
7722 // ADDE
7723 //
7724 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7725 SDValue AddcOp0 = AddcNode->getOperand(0);
7726 SDValue AddcOp1 = AddcNode->getOperand(1);
7727
7728 // Check if the two operands are from the same mul_lohi node.
7729 if (AddcOp0.getNode() == AddcOp1.getNode())
7730 return SDValue();
7731
7732 assert(AddcNode->getNumValues() == 2 &&
7733 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007734 "Expect ADDC with two result values. First: i32");
7735
7736 // Check that we have a glued ADDC node.
7737 if (AddcNode->getValueType(1) != MVT::Glue)
7738 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007739
7740 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7741 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7742 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7743 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7744 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7745 return SDValue();
7746
7747 // Look for the glued ADDE.
7748 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00007749 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007750 return SDValue();
7751
7752 // Make sure it is really an ADDE.
7753 if (AddeNode->getOpcode() != ISD::ADDE)
7754 return SDValue();
7755
7756 assert(AddeNode->getNumOperands() == 3 &&
7757 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7758 "ADDE node has the wrong inputs");
7759
7760 // Check for the triangle shape.
7761 SDValue AddeOp0 = AddeNode->getOperand(0);
7762 SDValue AddeOp1 = AddeNode->getOperand(1);
7763
7764 // Make sure that the ADDE operands are not coming from the same node.
7765 if (AddeOp0.getNode() == AddeOp1.getNode())
7766 return SDValue();
7767
7768 // Find the MUL_LOHI node walking up ADDE's operands.
7769 bool IsLeftOperandMUL = false;
7770 SDValue MULOp = findMUL_LOHI(AddeOp0);
7771 if (MULOp == SDValue())
7772 MULOp = findMUL_LOHI(AddeOp1);
7773 else
7774 IsLeftOperandMUL = true;
7775 if (MULOp == SDValue())
7776 return SDValue();
7777
7778 // Figure out the right opcode.
7779 unsigned Opc = MULOp->getOpcode();
7780 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7781
7782 // Figure out the high and low input values to the MLAL node.
7783 SDValue* HiMul = &MULOp;
Craig Topper062a2ba2014-04-25 05:30:21 +00007784 SDValue* HiAdd = nullptr;
7785 SDValue* LoMul = nullptr;
7786 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007787
7788 if (IsLeftOperandMUL)
7789 HiAdd = &AddeOp1;
7790 else
7791 HiAdd = &AddeOp0;
7792
7793
7794 if (AddcOp0->getOpcode() == Opc) {
7795 LoMul = &AddcOp0;
7796 LowAdd = &AddcOp1;
7797 }
7798 if (AddcOp1->getOpcode() == Opc) {
7799 LoMul = &AddcOp1;
7800 LowAdd = &AddcOp0;
7801 }
7802
Craig Topper062a2ba2014-04-25 05:30:21 +00007803 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007804 return SDValue();
7805
7806 if (LoMul->getNode() != HiMul->getNode())
7807 return SDValue();
7808
7809 // Create the merged node.
7810 SelectionDAG &DAG = DCI.DAG;
7811
7812 // Build operand list.
7813 SmallVector<SDValue, 8> Ops;
7814 Ops.push_back(LoMul->getOperand(0));
7815 Ops.push_back(LoMul->getOperand(1));
7816 Ops.push_back(*LowAdd);
7817 Ops.push_back(*HiAdd);
7818
Andrew Trickef9de2a2013-05-25 02:42:55 +00007819 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00007820 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007821
7822 // Replace the ADDs' nodes uses by the MLA node's values.
7823 SDValue HiMLALResult(MLALNode.getNode(), 1);
7824 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7825
7826 SDValue LoMLALResult(MLALNode.getNode(), 0);
7827 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7828
7829 // Return original node to notify the driver to stop replacing.
7830 SDValue resNode(AddcNode, 0);
7831 return resNode;
7832}
7833
7834/// PerformADDCCombine - Target-specific dag combine transform from
7835/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7836static SDValue PerformADDCCombine(SDNode *N,
7837 TargetLowering::DAGCombinerInfo &DCI,
7838 const ARMSubtarget *Subtarget) {
7839
7840 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7841
7842}
7843
Bob Wilson728eb292010-07-29 20:34:14 +00007844/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7845/// operands N0 and N1. This is a helper for PerformADDCombine that is
7846/// called with the default operands, and if that fails, with commuted
7847/// operands.
7848static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007849 TargetLowering::DAGCombinerInfo &DCI,
7850 const ARMSubtarget *Subtarget){
7851
7852 // Attempt to create vpaddl for this add.
7853 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7854 if (Result.getNode())
7855 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007856
Chris Lattner4147f082009-03-12 06:52:53 +00007857 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007858 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007859 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7860 if (Result.getNode()) return Result;
7861 }
Chris Lattner4147f082009-03-12 06:52:53 +00007862 return SDValue();
7863}
7864
Bob Wilson728eb292010-07-29 20:34:14 +00007865/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7866///
7867static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007868 TargetLowering::DAGCombinerInfo &DCI,
7869 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00007870 SDValue N0 = N->getOperand(0);
7871 SDValue N1 = N->getOperand(1);
7872
7873 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007874 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007875 if (Result.getNode())
7876 return Result;
7877
7878 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007879 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007880}
7881
Chris Lattner4147f082009-03-12 06:52:53 +00007882/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00007883///
Chris Lattner4147f082009-03-12 06:52:53 +00007884static SDValue PerformSUBCombine(SDNode *N,
7885 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00007886 SDValue N0 = N->getOperand(0);
7887 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00007888
Chris Lattner4147f082009-03-12 06:52:53 +00007889 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007890 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007891 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7892 if (Result.getNode()) return Result;
7893 }
Bob Wilson7117a912009-03-20 22:42:55 +00007894
Chris Lattner4147f082009-03-12 06:52:53 +00007895 return SDValue();
7896}
7897
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007898/// PerformVMULCombine
7899/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7900/// special multiplier accumulator forwarding.
7901/// vmul d3, d0, d2
7902/// vmla d3, d1, d2
7903/// is faster than
7904/// vadd d3, d0, d1
7905/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00007906// However, for (A + B) * (A + B),
7907// vadd d2, d0, d1
7908// vmul d3, d0, d2
7909// vmla d3, d1, d2
7910// is slower than
7911// vadd d2, d0, d1
7912// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007913static SDValue PerformVMULCombine(SDNode *N,
7914 TargetLowering::DAGCombinerInfo &DCI,
7915 const ARMSubtarget *Subtarget) {
7916 if (!Subtarget->hasVMLxForwarding())
7917 return SDValue();
7918
7919 SelectionDAG &DAG = DCI.DAG;
7920 SDValue N0 = N->getOperand(0);
7921 SDValue N1 = N->getOperand(1);
7922 unsigned Opcode = N0.getOpcode();
7923 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7924 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00007925 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007926 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7927 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7928 return SDValue();
7929 std::swap(N0, N1);
7930 }
7931
Weiming Zhao2052f482013-09-25 23:12:06 +00007932 if (N0 == N1)
7933 return SDValue();
7934
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007935 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007936 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007937 SDValue N00 = N0->getOperand(0);
7938 SDValue N01 = N0->getOperand(1);
7939 return DAG.getNode(Opcode, DL, VT,
7940 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7941 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7942}
7943
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007944static SDValue PerformMULCombine(SDNode *N,
7945 TargetLowering::DAGCombinerInfo &DCI,
7946 const ARMSubtarget *Subtarget) {
7947 SelectionDAG &DAG = DCI.DAG;
7948
7949 if (Subtarget->isThumb1Only())
7950 return SDValue();
7951
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007952 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7953 return SDValue();
7954
7955 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007956 if (VT.is64BitVector() || VT.is128BitVector())
7957 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007958 if (VT != MVT::i32)
7959 return SDValue();
7960
7961 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7962 if (!C)
7963 return SDValue();
7964
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007965 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00007966 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007967
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007968 ShiftAmt = ShiftAmt & (32 - 1);
7969 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007970 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007971
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007972 SDValue Res;
7973 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007974
7975 if (MulAmt >= 0) {
7976 if (isPowerOf2_32(MulAmt - 1)) {
7977 // (mul x, 2^N + 1) => (add (shl x, N), x)
7978 Res = DAG.getNode(ISD::ADD, DL, VT,
7979 V,
7980 DAG.getNode(ISD::SHL, DL, VT,
7981 V,
7982 DAG.getConstant(Log2_32(MulAmt - 1),
7983 MVT::i32)));
7984 } else if (isPowerOf2_32(MulAmt + 1)) {
7985 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7986 Res = DAG.getNode(ISD::SUB, DL, VT,
7987 DAG.getNode(ISD::SHL, DL, VT,
7988 V,
7989 DAG.getConstant(Log2_32(MulAmt + 1),
7990 MVT::i32)),
7991 V);
7992 } else
7993 return SDValue();
7994 } else {
7995 uint64_t MulAmtAbs = -MulAmt;
7996 if (isPowerOf2_32(MulAmtAbs + 1)) {
7997 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7998 Res = DAG.getNode(ISD::SUB, DL, VT,
7999 V,
8000 DAG.getNode(ISD::SHL, DL, VT,
8001 V,
8002 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8003 MVT::i32)));
8004 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8005 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8006 Res = DAG.getNode(ISD::ADD, DL, VT,
8007 V,
8008 DAG.getNode(ISD::SHL, DL, VT,
8009 V,
8010 DAG.getConstant(Log2_32(MulAmtAbs-1),
8011 MVT::i32)));
8012 Res = DAG.getNode(ISD::SUB, DL, VT,
8013 DAG.getConstant(0, MVT::i32),Res);
8014
8015 } else
8016 return SDValue();
8017 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008018
8019 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008020 Res = DAG.getNode(ISD::SHL, DL, VT,
8021 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008022
8023 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008024 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008025 return SDValue();
8026}
8027
Owen Anderson30c48922010-11-05 19:27:46 +00008028static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008029 TargetLowering::DAGCombinerInfo &DCI,
8030 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008031
Owen Anderson30c48922010-11-05 19:27:46 +00008032 // Attempt to use immediate-form VBIC
8033 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008034 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008035 EVT VT = N->getValueType(0);
8036 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008037
Tanya Lattner266792a2011-04-07 15:24:20 +00008038 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8039 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008040
Owen Anderson30c48922010-11-05 19:27:46 +00008041 APInt SplatBits, SplatUndef;
8042 unsigned SplatBitSize;
8043 bool HasAnyUndefs;
8044 if (BVN &&
8045 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8046 if (SplatBitSize <= 64) {
8047 EVT VbicVT;
8048 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8049 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008050 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008051 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008052 if (Val.getNode()) {
8053 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008054 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008055 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008056 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008057 }
8058 }
8059 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008060
Evan Chenge87681c2012-02-23 01:19:06 +00008061 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008062 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8063 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8064 if (Result.getNode())
8065 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008066 }
8067
Owen Anderson30c48922010-11-05 19:27:46 +00008068 return SDValue();
8069}
8070
Jim Grosbach11013ed2010-07-16 23:05:05 +00008071/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8072static SDValue PerformORCombine(SDNode *N,
8073 TargetLowering::DAGCombinerInfo &DCI,
8074 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008075 // Attempt to use immediate-form VORR
8076 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008077 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008078 EVT VT = N->getValueType(0);
8079 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008080
Tanya Lattner266792a2011-04-07 15:24:20 +00008081 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8082 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008083
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008084 APInt SplatBits, SplatUndef;
8085 unsigned SplatBitSize;
8086 bool HasAnyUndefs;
8087 if (BVN && Subtarget->hasNEON() &&
8088 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8089 if (SplatBitSize <= 64) {
8090 EVT VorrVT;
8091 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8092 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008093 DAG, VorrVT, VT.is128BitVector(),
8094 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008095 if (Val.getNode()) {
8096 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008097 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008098 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008099 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008100 }
8101 }
8102 }
8103
Evan Chenge87681c2012-02-23 01:19:06 +00008104 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008105 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8106 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8107 if (Result.getNode())
8108 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008109 }
8110
Nadav Rotem3a94c542012-08-13 18:52:44 +00008111 // The code below optimizes (or (and X, Y), Z).
8112 // The AND operand needs to have a single user to make these optimizations
8113 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008114 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008115 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008116 return SDValue();
8117 SDValue N1 = N->getOperand(1);
8118
8119 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8120 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8121 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8122 APInt SplatUndef;
8123 unsigned SplatBitSize;
8124 bool HasAnyUndefs;
8125
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008126 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008127 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008128 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8129 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008130 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008131 HasAnyUndefs) && !HasAnyUndefs) {
8132 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8133 HasAnyUndefs) && !HasAnyUndefs) {
8134 // Ensure that the bit width of the constants are the same and that
8135 // the splat arguments are logical inverses as per the pattern we
8136 // are trying to simplify.
8137 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8138 SplatBits0 == ~SplatBits1) {
8139 // Canonicalize the vector type to make instruction selection
8140 // simpler.
8141 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8142 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8143 N0->getOperand(1),
8144 N0->getOperand(0),
8145 N1->getOperand(0));
8146 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8147 }
8148 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008149 }
8150 }
8151
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008152 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8153 // reasonable.
8154
Jim Grosbach11013ed2010-07-16 23:05:05 +00008155 // BFI is only available on V6T2+
8156 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8157 return SDValue();
8158
Andrew Trickef9de2a2013-05-25 02:42:55 +00008159 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008160 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008161 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008162 //
8163 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008164 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008165 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008166 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008167 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008168 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008169
Jim Grosbach11013ed2010-07-16 23:05:05 +00008170 if (VT != MVT::i32)
8171 return SDValue();
8172
Evan Cheng2e51bb42010-12-13 20:32:54 +00008173 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008174
Jim Grosbach11013ed2010-07-16 23:05:05 +00008175 // The value and the mask need to be constants so we can verify this is
8176 // actually a bitfield set. If the mask is 0xffff, we can do better
8177 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008178 SDValue MaskOp = N0.getOperand(1);
8179 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8180 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008181 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008182 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008183 if (Mask == 0xffff)
8184 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008185 SDValue Res;
8186 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008187 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8188 if (N1C) {
8189 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008190 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008191 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008192
Evan Cheng34345752010-12-11 04:11:38 +00008193 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008194 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008195
Evan Cheng2e51bb42010-12-13 20:32:54 +00008196 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008197 DAG.getConstant(Val, MVT::i32),
8198 DAG.getConstant(Mask, MVT::i32));
8199
8200 // Do not add new nodes to DAG combiner worklist.
8201 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008202 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008203 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008204 } else if (N1.getOpcode() == ISD::AND) {
8205 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008206 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8207 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008208 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008209 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008210
Eric Christopherd5530962011-03-26 01:21:03 +00008211 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8212 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008213 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008214 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008215 // The pack halfword instruction works better for masks that fit it,
8216 // so use that when it's available.
8217 if (Subtarget->hasT2ExtractPack() &&
8218 (Mask == 0xffff || Mask == 0xffff0000))
8219 return SDValue();
8220 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008221 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008222 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008223 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008224 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008225 DAG.getConstant(Mask, MVT::i32));
8226 // Do not add new nodes to DAG combiner worklist.
8227 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008228 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008229 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008230 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008231 // The pack halfword instruction works better for masks that fit it,
8232 // so use that when it's available.
8233 if (Subtarget->hasT2ExtractPack() &&
8234 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8235 return SDValue();
8236 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008237 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008238 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008239 DAG.getConstant(lsb, MVT::i32));
8240 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008241 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008242 // Do not add new nodes to DAG combiner worklist.
8243 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008244 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008245 }
8246 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008247
Evan Cheng2e51bb42010-12-13 20:32:54 +00008248 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8249 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8250 ARM::isBitFieldInvertedMask(~Mask)) {
8251 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8252 // where lsb(mask) == #shamt and masked bits of B are known zero.
8253 SDValue ShAmt = N00.getOperand(1);
8254 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008255 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008256 if (ShAmtC != LSB)
8257 return SDValue();
8258
8259 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8260 DAG.getConstant(~Mask, MVT::i32));
8261
8262 // Do not add new nodes to DAG combiner worklist.
8263 DCI.CombineTo(N, Res, false);
8264 }
8265
Jim Grosbach11013ed2010-07-16 23:05:05 +00008266 return SDValue();
8267}
8268
Evan Chenge87681c2012-02-23 01:19:06 +00008269static SDValue PerformXORCombine(SDNode *N,
8270 TargetLowering::DAGCombinerInfo &DCI,
8271 const ARMSubtarget *Subtarget) {
8272 EVT VT = N->getValueType(0);
8273 SelectionDAG &DAG = DCI.DAG;
8274
8275 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8276 return SDValue();
8277
8278 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008279 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8280 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8281 if (Result.getNode())
8282 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008283 }
8284
8285 return SDValue();
8286}
8287
Evan Cheng6d02d902011-06-15 01:12:31 +00008288/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8289/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008290static SDValue PerformBFICombine(SDNode *N,
8291 TargetLowering::DAGCombinerInfo &DCI) {
8292 SDValue N1 = N->getOperand(1);
8293 if (N1.getOpcode() == ISD::AND) {
8294 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8295 if (!N11C)
8296 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008297 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008298 unsigned LSB = countTrailingZeros(~InvMask);
8299 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008300 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008301 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008302 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008303 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008304 N->getOperand(0), N1.getOperand(0),
8305 N->getOperand(2));
8306 }
8307 return SDValue();
8308}
8309
Bob Wilson22806742010-09-22 22:09:21 +00008310/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8311/// ARMISD::VMOVRRD.
8312static SDValue PerformVMOVRRDCombine(SDNode *N,
8313 TargetLowering::DAGCombinerInfo &DCI) {
8314 // vmovrrd(vmovdrr x, y) -> x,y
8315 SDValue InDouble = N->getOperand(0);
8316 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8317 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008318
8319 // vmovrrd(load f64) -> (load i32), (load i32)
8320 SDNode *InNode = InDouble.getNode();
8321 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8322 InNode->getValueType(0) == MVT::f64 &&
8323 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8324 !cast<LoadSDNode>(InNode)->isVolatile()) {
8325 // TODO: Should this be done for non-FrameIndex operands?
8326 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8327
8328 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008329 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008330 SDValue BasePtr = LD->getBasePtr();
8331 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8332 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008333 LD->isNonTemporal(), LD->isInvariant(),
8334 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008335
8336 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8337 DAG.getConstant(4, MVT::i32));
8338 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8339 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008340 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008341 std::min(4U, LD->getAlignment() / 2));
8342
8343 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8344 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8345 DCI.RemoveFromWorklist(LD);
8346 DAG.DeleteNode(LD);
8347 return Result;
8348 }
8349
Bob Wilson22806742010-09-22 22:09:21 +00008350 return SDValue();
8351}
8352
8353/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8354/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8355static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8356 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8357 SDValue Op0 = N->getOperand(0);
8358 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008359 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008360 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008361 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008362 Op1 = Op1.getOperand(0);
8363 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8364 Op0.getNode() == Op1.getNode() &&
8365 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008366 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008367 N->getValueType(0), Op0.getOperand(0));
8368 return SDValue();
8369}
8370
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008371/// PerformSTORECombine - Target-specific dag combine xforms for
8372/// ISD::STORE.
8373static SDValue PerformSTORECombine(SDNode *N,
8374 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008375 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008376 if (St->isVolatile())
8377 return SDValue();
8378
Andrew Trickbc325162012-07-18 18:34:24 +00008379 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008380 // pack all of the elements in one place. Next, store to memory in fewer
8381 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008382 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008383 EVT VT = StVal.getValueType();
8384 if (St->isTruncatingStore() && VT.isVector()) {
8385 SelectionDAG &DAG = DCI.DAG;
8386 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8387 EVT StVT = St->getMemoryVT();
8388 unsigned NumElems = VT.getVectorNumElements();
8389 assert(StVT != VT && "Cannot truncate to the same type");
8390 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8391 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8392
8393 // From, To sizes and ElemCount must be pow of two
8394 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8395
8396 // We are going to use the original vector elt for storing.
8397 // Accumulated smaller vector elements must be a multiple of the store size.
8398 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8399
8400 unsigned SizeRatio = FromEltSz / ToEltSz;
8401 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8402
8403 // Create a type on which we perform the shuffle.
8404 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8405 NumElems*SizeRatio);
8406 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8407
Andrew Trickef9de2a2013-05-25 02:42:55 +00008408 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008409 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8410 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8411 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8412
8413 // Can't shuffle using an illegal type.
8414 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8415
8416 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8417 DAG.getUNDEF(WideVec.getValueType()),
8418 ShuffleVec.data());
8419 // At this point all of the data is stored at the bottom of the
8420 // register. We now need to save it to mem.
8421
8422 // Find the largest store unit
8423 MVT StoreType = MVT::i8;
8424 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8425 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8426 MVT Tp = (MVT::SimpleValueType)tp;
8427 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8428 StoreType = Tp;
8429 }
8430 // Didn't find a legal store type.
8431 if (!TLI.isTypeLegal(StoreType))
8432 return SDValue();
8433
8434 // Bitcast the original vector into a vector of store-size units
8435 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8436 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8437 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8438 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8439 SmallVector<SDValue, 8> Chains;
8440 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8441 TLI.getPointerTy());
8442 SDValue BasePtr = St->getBasePtr();
8443
8444 // Perform one or more big stores into memory.
8445 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8446 for (unsigned I = 0; I < E; I++) {
8447 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8448 StoreType, ShuffWide,
8449 DAG.getIntPtrConstant(I));
8450 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8451 St->getPointerInfo(), St->isVolatile(),
8452 St->isNonTemporal(), St->getAlignment());
8453 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8454 Increment);
8455 Chains.push_back(Ch);
8456 }
Craig Topper48d114b2014-04-26 18:35:24 +00008457 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008458 }
8459
8460 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008461 return SDValue();
8462
Chad Rosier99cbde92012-04-09 19:38:15 +00008463 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8464 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008465 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008466 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008467 SelectionDAG &DAG = DCI.DAG;
Christian Pirkerb5728192014-05-08 14:06:24 +00008468 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008469 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008470 SDValue BasePtr = St->getBasePtr();
8471 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
Christian Pirkerb5728192014-05-08 14:06:24 +00008472 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
8473 BasePtr, St->getPointerInfo(), St->isVolatile(),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008474 St->isNonTemporal(), St->getAlignment());
8475
8476 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8477 DAG.getConstant(4, MVT::i32));
Christian Pirkerb5728192014-05-08 14:06:24 +00008478 return DAG.getStore(NewST1.getValue(0), DL,
8479 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008480 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8481 St->isNonTemporal(),
8482 std::min(4U, St->getAlignment() / 2));
8483 }
8484
8485 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008486 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8487 return SDValue();
8488
Chad Rosier99cbde92012-04-09 19:38:15 +00008489 // Bitcast an i64 store extracted from a vector to f64.
8490 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008491 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008492 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008493 SDValue IntVec = StVal.getOperand(0);
8494 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8495 IntVec.getValueType().getVectorNumElements());
8496 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8497 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8498 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008499 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008500 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8501 // Make the DAGCombiner fold the bitcasts.
8502 DCI.AddToWorklist(Vec.getNode());
8503 DCI.AddToWorklist(ExtElt.getNode());
8504 DCI.AddToWorklist(V.getNode());
8505 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8506 St->getPointerInfo(), St->isVolatile(),
8507 St->isNonTemporal(), St->getAlignment(),
8508 St->getTBAAInfo());
8509}
8510
8511/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8512/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8513/// i64 vector to have f64 elements, since the value can then be loaded
8514/// directly into a VFP register.
8515static bool hasNormalLoadOperand(SDNode *N) {
8516 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8517 for (unsigned i = 0; i < NumElts; ++i) {
8518 SDNode *Elt = N->getOperand(i).getNode();
8519 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8520 return true;
8521 }
8522 return false;
8523}
8524
Bob Wilsoncb6db982010-09-17 22:59:05 +00008525/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8526/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008527static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8528 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008529 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8530 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8531 // into a pair of GPRs, which is fine when the value is used as a scalar,
8532 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008533 SelectionDAG &DAG = DCI.DAG;
8534 if (N->getNumOperands() == 2) {
8535 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8536 if (RV.getNode())
8537 return RV;
8538 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008539
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008540 // Load i64 elements as f64 values so that type legalization does not split
8541 // them up into i32 values.
8542 EVT VT = N->getValueType(0);
8543 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8544 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008545 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008546 SmallVector<SDValue, 8> Ops;
8547 unsigned NumElts = VT.getVectorNumElements();
8548 for (unsigned i = 0; i < NumElts; ++i) {
8549 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8550 Ops.push_back(V);
8551 // Make the DAGCombiner fold the bitcast.
8552 DCI.AddToWorklist(V.getNode());
8553 }
8554 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008555 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008556 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8557}
8558
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008559/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8560static SDValue
8561PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8562 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8563 // At that time, we may have inserted bitcasts from integer to float.
8564 // If these bitcasts have survived DAGCombine, change the lowering of this
8565 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8566 // force to use floating point types.
8567
8568 // Make sure we can change the type of the vector.
8569 // This is possible iff:
8570 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8571 // 1.1. Vector is used only once.
8572 // 1.2. Use is a bit convert to an integer type.
8573 // 2. The size of its operands are 32-bits (64-bits are not legal).
8574 EVT VT = N->getValueType(0);
8575 EVT EltVT = VT.getVectorElementType();
8576
8577 // Check 1.1. and 2.
8578 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8579 return SDValue();
8580
8581 // By construction, the input type must be float.
8582 assert(EltVT == MVT::f32 && "Unexpected type!");
8583
8584 // Check 1.2.
8585 SDNode *Use = *N->use_begin();
8586 if (Use->getOpcode() != ISD::BITCAST ||
8587 Use->getValueType(0).isFloatingPoint())
8588 return SDValue();
8589
8590 // Check profitability.
8591 // Model is, if more than half of the relevant operands are bitcast from
8592 // i32, turn the build_vector into a sequence of insert_vector_elt.
8593 // Relevant operands are everything that is not statically
8594 // (i.e., at compile time) bitcasted.
8595 unsigned NumOfBitCastedElts = 0;
8596 unsigned NumElts = VT.getVectorNumElements();
8597 unsigned NumOfRelevantElts = NumElts;
8598 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8599 SDValue Elt = N->getOperand(Idx);
8600 if (Elt->getOpcode() == ISD::BITCAST) {
8601 // Assume only bit cast to i32 will go away.
8602 if (Elt->getOperand(0).getValueType() == MVT::i32)
8603 ++NumOfBitCastedElts;
8604 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8605 // Constants are statically casted, thus do not count them as
8606 // relevant operands.
8607 --NumOfRelevantElts;
8608 }
8609
8610 // Check if more than half of the elements require a non-free bitcast.
8611 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8612 return SDValue();
8613
8614 SelectionDAG &DAG = DCI.DAG;
8615 // Create the new vector type.
8616 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8617 // Check if the type is legal.
8618 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8619 if (!TLI.isTypeLegal(VecVT))
8620 return SDValue();
8621
8622 // Combine:
8623 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8624 // => BITCAST INSERT_VECTOR_ELT
8625 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8626 // (BITCAST EN), N.
8627 SDValue Vec = DAG.getUNDEF(VecVT);
8628 SDLoc dl(N);
8629 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8630 SDValue V = N->getOperand(Idx);
8631 if (V.getOpcode() == ISD::UNDEF)
8632 continue;
8633 if (V.getOpcode() == ISD::BITCAST &&
8634 V->getOperand(0).getValueType() == MVT::i32)
8635 // Fold obvious case.
8636 V = V.getOperand(0);
8637 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008638 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008639 // Make the DAGCombiner fold the bitcasts.
8640 DCI.AddToWorklist(V.getNode());
8641 }
8642 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8643 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8644 }
8645 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8646 // Make the DAGCombiner fold the bitcasts.
8647 DCI.AddToWorklist(Vec.getNode());
8648 return Vec;
8649}
8650
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008651/// PerformInsertEltCombine - Target-specific dag combine xforms for
8652/// ISD::INSERT_VECTOR_ELT.
8653static SDValue PerformInsertEltCombine(SDNode *N,
8654 TargetLowering::DAGCombinerInfo &DCI) {
8655 // Bitcast an i64 load inserted into a vector to f64.
8656 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8657 EVT VT = N->getValueType(0);
8658 SDNode *Elt = N->getOperand(1).getNode();
8659 if (VT.getVectorElementType() != MVT::i64 ||
8660 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8661 return SDValue();
8662
8663 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008664 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008665 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8666 VT.getVectorNumElements());
8667 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8668 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8669 // Make the DAGCombiner fold the bitcasts.
8670 DCI.AddToWorklist(Vec.getNode());
8671 DCI.AddToWorklist(V.getNode());
8672 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8673 Vec, V, N->getOperand(2));
8674 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008675}
8676
Bob Wilsonc7334a12010-10-27 20:38:28 +00008677/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8678/// ISD::VECTOR_SHUFFLE.
8679static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8680 // The LLVM shufflevector instruction does not require the shuffle mask
8681 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8682 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8683 // operands do not match the mask length, they are extended by concatenating
8684 // them with undef vectors. That is probably the right thing for other
8685 // targets, but for NEON it is better to concatenate two double-register
8686 // size vector operands into a single quad-register size vector. Do that
8687 // transformation here:
8688 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8689 // shuffle(concat(v1, v2), undef)
8690 SDValue Op0 = N->getOperand(0);
8691 SDValue Op1 = N->getOperand(1);
8692 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8693 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8694 Op0.getNumOperands() != 2 ||
8695 Op1.getNumOperands() != 2)
8696 return SDValue();
8697 SDValue Concat0Op1 = Op0.getOperand(1);
8698 SDValue Concat1Op1 = Op1.getOperand(1);
8699 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8700 Concat1Op1.getOpcode() != ISD::UNDEF)
8701 return SDValue();
8702 // Skip the transformation if any of the types are illegal.
8703 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8704 EVT VT = N->getValueType(0);
8705 if (!TLI.isTypeLegal(VT) ||
8706 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8707 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8708 return SDValue();
8709
Andrew Trickef9de2a2013-05-25 02:42:55 +00008710 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008711 Op0.getOperand(0), Op1.getOperand(0));
8712 // Translate the shuffle mask.
8713 SmallVector<int, 16> NewMask;
8714 unsigned NumElts = VT.getVectorNumElements();
8715 unsigned HalfElts = NumElts/2;
8716 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8717 for (unsigned n = 0; n < NumElts; ++n) {
8718 int MaskElt = SVN->getMaskElt(n);
8719 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008720 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008721 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008722 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008723 NewElt = HalfElts + MaskElt - NumElts;
8724 NewMask.push_back(NewElt);
8725 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008726 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008727 DAG.getUNDEF(VT), NewMask.data());
8728}
8729
Bob Wilson06fce872011-02-07 17:43:21 +00008730/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8731/// NEON load/store intrinsics to merge base address updates.
8732static SDValue CombineBaseUpdate(SDNode *N,
8733 TargetLowering::DAGCombinerInfo &DCI) {
8734 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8735 return SDValue();
8736
8737 SelectionDAG &DAG = DCI.DAG;
8738 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8739 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8740 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8741 SDValue Addr = N->getOperand(AddrOpIdx);
8742
8743 // Search for a use of the address operand that is an increment.
8744 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8745 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8746 SDNode *User = *UI;
8747 if (User->getOpcode() != ISD::ADD ||
8748 UI.getUse().getResNo() != Addr.getResNo())
8749 continue;
8750
8751 // Check that the add is independent of the load/store. Otherwise, folding
8752 // it would create a cycle.
8753 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8754 continue;
8755
8756 // Find the new opcode for the updating load/store.
8757 bool isLoad = true;
8758 bool isLaneOp = false;
8759 unsigned NewOpc = 0;
8760 unsigned NumVecs = 0;
8761 if (isIntrinsic) {
8762 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8763 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008764 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008765 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8766 NumVecs = 1; break;
8767 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8768 NumVecs = 2; break;
8769 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8770 NumVecs = 3; break;
8771 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8772 NumVecs = 4; break;
8773 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8774 NumVecs = 2; isLaneOp = true; break;
8775 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8776 NumVecs = 3; isLaneOp = true; break;
8777 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8778 NumVecs = 4; isLaneOp = true; break;
8779 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8780 NumVecs = 1; isLoad = false; break;
8781 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8782 NumVecs = 2; isLoad = false; break;
8783 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8784 NumVecs = 3; isLoad = false; break;
8785 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8786 NumVecs = 4; isLoad = false; break;
8787 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8788 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8789 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8790 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8791 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8792 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8793 }
8794 } else {
8795 isLaneOp = true;
8796 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008797 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008798 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8799 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8800 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8801 }
8802 }
8803
8804 // Find the size of memory referenced by the load/store.
8805 EVT VecTy;
8806 if (isLoad)
8807 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008808 else
Bob Wilson06fce872011-02-07 17:43:21 +00008809 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8810 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8811 if (isLaneOp)
8812 NumBytes /= VecTy.getVectorNumElements();
8813
8814 // If the increment is a constant, it must match the memory ref size.
8815 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8816 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8817 uint64_t IncVal = CInc->getZExtValue();
8818 if (IncVal != NumBytes)
8819 continue;
8820 } else if (NumBytes >= 3 * 16) {
8821 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8822 // separate instructions that make it harder to use a non-constant update.
8823 continue;
8824 }
8825
8826 // Create the new updating load/store node.
8827 EVT Tys[6];
8828 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8829 unsigned n;
8830 for (n = 0; n < NumResultVecs; ++n)
8831 Tys[n] = VecTy;
8832 Tys[n++] = MVT::i32;
8833 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008834 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008835 SmallVector<SDValue, 8> Ops;
8836 Ops.push_back(N->getOperand(0)); // incoming chain
8837 Ops.push_back(N->getOperand(AddrOpIdx));
8838 Ops.push_back(Inc);
8839 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8840 Ops.push_back(N->getOperand(i));
8841 }
8842 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008843 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008844 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00008845 MemInt->getMemOperand());
8846
8847 // Update the uses.
8848 std::vector<SDValue> NewResults;
8849 for (unsigned i = 0; i < NumResultVecs; ++i) {
8850 NewResults.push_back(SDValue(UpdN.getNode(), i));
8851 }
8852 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8853 DCI.CombineTo(N, NewResults);
8854 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8855
8856 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008857 }
Bob Wilson06fce872011-02-07 17:43:21 +00008858 return SDValue();
8859}
8860
Bob Wilson2d790df2010-11-28 06:51:26 +00008861/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8862/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8863/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8864/// return true.
8865static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8866 SelectionDAG &DAG = DCI.DAG;
8867 EVT VT = N->getValueType(0);
8868 // vldN-dup instructions only support 64-bit vectors for N > 1.
8869 if (!VT.is64BitVector())
8870 return false;
8871
8872 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8873 SDNode *VLD = N->getOperand(0).getNode();
8874 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8875 return false;
8876 unsigned NumVecs = 0;
8877 unsigned NewOpc = 0;
8878 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8879 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8880 NumVecs = 2;
8881 NewOpc = ARMISD::VLD2DUP;
8882 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8883 NumVecs = 3;
8884 NewOpc = ARMISD::VLD3DUP;
8885 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8886 NumVecs = 4;
8887 NewOpc = ARMISD::VLD4DUP;
8888 } else {
8889 return false;
8890 }
8891
8892 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8893 // numbers match the load.
8894 unsigned VLDLaneNo =
8895 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8896 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8897 UI != UE; ++UI) {
8898 // Ignore uses of the chain result.
8899 if (UI.getUse().getResNo() == NumVecs)
8900 continue;
8901 SDNode *User = *UI;
8902 if (User->getOpcode() != ARMISD::VDUPLANE ||
8903 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8904 return false;
8905 }
8906
8907 // Create the vldN-dup node.
8908 EVT Tys[5];
8909 unsigned n;
8910 for (n = 0; n < NumVecs; ++n)
8911 Tys[n] = VT;
8912 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008913 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00008914 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8915 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008916 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008917 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00008918 VLDMemInt->getMemOperand());
8919
8920 // Update the uses.
8921 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8922 UI != UE; ++UI) {
8923 unsigned ResNo = UI.getUse().getResNo();
8924 // Ignore uses of the chain result.
8925 if (ResNo == NumVecs)
8926 continue;
8927 SDNode *User = *UI;
8928 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8929 }
8930
8931 // Now the vldN-lane intrinsic is dead except for its chain result.
8932 // Update uses of the chain.
8933 std::vector<SDValue> VLDDupResults;
8934 for (unsigned n = 0; n < NumVecs; ++n)
8935 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8936 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8937 DCI.CombineTo(VLD, VLDDupResults);
8938
8939 return true;
8940}
8941
Bob Wilson103a0dc2010-07-14 01:22:12 +00008942/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8943/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00008944static SDValue PerformVDUPLANECombine(SDNode *N,
8945 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00008946 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008947
Bob Wilson2d790df2010-11-28 06:51:26 +00008948 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8949 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8950 if (CombineVLDDUP(N, DCI))
8951 return SDValue(N, 0);
8952
8953 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8954 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00008955 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008956 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00008957 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008958 return SDValue();
8959
8960 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8961 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8962 // The canonical VMOV for a zero vector uses a 32-bit element size.
8963 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8964 unsigned EltBits;
8965 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8966 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00008967 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008968 if (EltSize > VT.getVectorElementType().getSizeInBits())
8969 return SDValue();
8970
Andrew Trickef9de2a2013-05-25 02:42:55 +00008971 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008972}
8973
Eric Christopher1b8b94192011-06-29 21:10:36 +00008974// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00008975// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8976static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8977{
Chad Rosier6b610b32011-06-28 17:26:57 +00008978 integerPart cN;
8979 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008980 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8981 I != E; I++) {
8982 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8983 if (!C)
8984 return false;
8985
Eric Christopher1b8b94192011-06-29 21:10:36 +00008986 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008987 APFloat APF = C->getValueAPF();
8988 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8989 != APFloat::opOK || !isExact)
8990 return false;
8991
8992 c0 = (I == 0) ? cN : c0;
8993 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8994 return false;
8995 }
8996 C = c0;
8997 return true;
8998}
8999
9000/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9001/// can replace combinations of VMUL and VCVT (floating-point to integer)
9002/// when the VMUL has a constant operand that is a power of 2.
9003///
9004/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9005/// vmul.f32 d16, d17, d16
9006/// vcvt.s32.f32 d16, d16
9007/// becomes:
9008/// vcvt.s32.f32 d16, d16, #3
9009static SDValue PerformVCVTCombine(SDNode *N,
9010 TargetLowering::DAGCombinerInfo &DCI,
9011 const ARMSubtarget *Subtarget) {
9012 SelectionDAG &DAG = DCI.DAG;
9013 SDValue Op = N->getOperand(0);
9014
9015 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9016 Op.getOpcode() != ISD::FMUL)
9017 return SDValue();
9018
9019 uint64_t C;
9020 SDValue N0 = Op->getOperand(0);
9021 SDValue ConstVec = Op->getOperand(1);
9022 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9023
Eric Christopher1b8b94192011-06-29 21:10:36 +00009024 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009025 !isConstVecPow2(ConstVec, isSigned, C))
9026 return SDValue();
9027
Tim Northover7cbc2152013-06-28 15:29:25 +00009028 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9029 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9030 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9031 // These instructions only exist converting from f32 to i32. We can handle
9032 // smaller integers by generating an extra truncate, but larger ones would
9033 // be lossy.
9034 return SDValue();
9035 }
9036
Chad Rosierfa8d8932011-06-24 19:23:04 +00009037 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9038 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009039 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9040 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9041 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9042 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9043 DAG.getConstant(Log2_64(C), MVT::i32));
9044
9045 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9046 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9047
9048 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009049}
9050
9051/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9052/// can replace combinations of VCVT (integer to floating-point) and VDIV
9053/// when the VDIV has a constant operand that is a power of 2.
9054///
9055/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9056/// vcvt.f32.s32 d16, d16
9057/// vdiv.f32 d16, d17, d16
9058/// becomes:
9059/// vcvt.f32.s32 d16, d16, #3
9060static SDValue PerformVDIVCombine(SDNode *N,
9061 TargetLowering::DAGCombinerInfo &DCI,
9062 const ARMSubtarget *Subtarget) {
9063 SelectionDAG &DAG = DCI.DAG;
9064 SDValue Op = N->getOperand(0);
9065 unsigned OpOpcode = Op.getNode()->getOpcode();
9066
9067 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9068 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9069 return SDValue();
9070
9071 uint64_t C;
9072 SDValue ConstVec = N->getOperand(1);
9073 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9074
9075 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9076 !isConstVecPow2(ConstVec, isSigned, C))
9077 return SDValue();
9078
Tim Northover7cbc2152013-06-28 15:29:25 +00009079 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9080 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9081 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9082 // These instructions only exist converting from i32 to f32. We can handle
9083 // smaller integers by generating an extra extend, but larger ones would
9084 // be lossy.
9085 return SDValue();
9086 }
9087
9088 SDValue ConvInput = Op.getOperand(0);
9089 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9090 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9091 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9092 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9093 ConvInput);
9094
Eric Christopher1b8b94192011-06-29 21:10:36 +00009095 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009096 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009097 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009098 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009099 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009100 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009101}
9102
9103/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009104/// operand of a vector shift operation, where all the elements of the
9105/// build_vector must have the same constant integer value.
9106static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9107 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009108 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009109 Op = Op.getOperand(0);
9110 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9111 APInt SplatBits, SplatUndef;
9112 unsigned SplatBitSize;
9113 bool HasAnyUndefs;
9114 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9115 HasAnyUndefs, ElementBits) ||
9116 SplatBitSize > ElementBits)
9117 return false;
9118 Cnt = SplatBits.getSExtValue();
9119 return true;
9120}
9121
9122/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9123/// operand of a vector shift left operation. That value must be in the range:
9124/// 0 <= Value < ElementBits for a left shift; or
9125/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009126static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009127 assert(VT.isVector() && "vector shift count is not a vector type");
9128 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9129 if (! getVShiftImm(Op, ElementBits, Cnt))
9130 return false;
9131 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9132}
9133
9134/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9135/// operand of a vector shift right operation. For a shift opcode, the value
9136/// is positive, but for an intrinsic the value count must be negative. The
9137/// absolute value must be in the range:
9138/// 1 <= |Value| <= ElementBits for a right shift; or
9139/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009140static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009141 int64_t &Cnt) {
9142 assert(VT.isVector() && "vector shift count is not a vector type");
9143 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9144 if (! getVShiftImm(Op, ElementBits, Cnt))
9145 return false;
9146 if (isIntrinsic)
9147 Cnt = -Cnt;
9148 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9149}
9150
9151/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9152static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9153 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9154 switch (IntNo) {
9155 default:
9156 // Don't do anything for most intrinsics.
9157 break;
9158
9159 // Vector shifts: check for immediate versions and lower them.
9160 // Note: This is done during DAG combining instead of DAG legalizing because
9161 // the build_vectors for 64-bit vector element shift counts are generally
9162 // not legal, and it is hard to see their values after they get legalized to
9163 // loads from a constant pool.
9164 case Intrinsic::arm_neon_vshifts:
9165 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009166 case Intrinsic::arm_neon_vrshifts:
9167 case Intrinsic::arm_neon_vrshiftu:
9168 case Intrinsic::arm_neon_vrshiftn:
9169 case Intrinsic::arm_neon_vqshifts:
9170 case Intrinsic::arm_neon_vqshiftu:
9171 case Intrinsic::arm_neon_vqshiftsu:
9172 case Intrinsic::arm_neon_vqshiftns:
9173 case Intrinsic::arm_neon_vqshiftnu:
9174 case Intrinsic::arm_neon_vqshiftnsu:
9175 case Intrinsic::arm_neon_vqrshiftns:
9176 case Intrinsic::arm_neon_vqrshiftnu:
9177 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009178 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009179 int64_t Cnt;
9180 unsigned VShiftOpc = 0;
9181
9182 switch (IntNo) {
9183 case Intrinsic::arm_neon_vshifts:
9184 case Intrinsic::arm_neon_vshiftu:
9185 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9186 VShiftOpc = ARMISD::VSHL;
9187 break;
9188 }
9189 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9190 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9191 ARMISD::VSHRs : ARMISD::VSHRu);
9192 break;
9193 }
9194 return SDValue();
9195
Bob Wilson2e076c42009-06-22 23:27:02 +00009196 case Intrinsic::arm_neon_vrshifts:
9197 case Intrinsic::arm_neon_vrshiftu:
9198 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9199 break;
9200 return SDValue();
9201
9202 case Intrinsic::arm_neon_vqshifts:
9203 case Intrinsic::arm_neon_vqshiftu:
9204 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9205 break;
9206 return SDValue();
9207
9208 case Intrinsic::arm_neon_vqshiftsu:
9209 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9210 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009211 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009212
Bob Wilson2e076c42009-06-22 23:27:02 +00009213 case Intrinsic::arm_neon_vrshiftn:
9214 case Intrinsic::arm_neon_vqshiftns:
9215 case Intrinsic::arm_neon_vqshiftnu:
9216 case Intrinsic::arm_neon_vqshiftnsu:
9217 case Intrinsic::arm_neon_vqrshiftns:
9218 case Intrinsic::arm_neon_vqrshiftnu:
9219 case Intrinsic::arm_neon_vqrshiftnsu:
9220 // Narrowing shifts require an immediate right shift.
9221 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9222 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009223 llvm_unreachable("invalid shift count for narrowing vector shift "
9224 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009225
9226 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009227 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009228 }
9229
9230 switch (IntNo) {
9231 case Intrinsic::arm_neon_vshifts:
9232 case Intrinsic::arm_neon_vshiftu:
9233 // Opcode already set above.
9234 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009235 case Intrinsic::arm_neon_vrshifts:
9236 VShiftOpc = ARMISD::VRSHRs; break;
9237 case Intrinsic::arm_neon_vrshiftu:
9238 VShiftOpc = ARMISD::VRSHRu; break;
9239 case Intrinsic::arm_neon_vrshiftn:
9240 VShiftOpc = ARMISD::VRSHRN; break;
9241 case Intrinsic::arm_neon_vqshifts:
9242 VShiftOpc = ARMISD::VQSHLs; break;
9243 case Intrinsic::arm_neon_vqshiftu:
9244 VShiftOpc = ARMISD::VQSHLu; break;
9245 case Intrinsic::arm_neon_vqshiftsu:
9246 VShiftOpc = ARMISD::VQSHLsu; break;
9247 case Intrinsic::arm_neon_vqshiftns:
9248 VShiftOpc = ARMISD::VQSHRNs; break;
9249 case Intrinsic::arm_neon_vqshiftnu:
9250 VShiftOpc = ARMISD::VQSHRNu; break;
9251 case Intrinsic::arm_neon_vqshiftnsu:
9252 VShiftOpc = ARMISD::VQSHRNsu; break;
9253 case Intrinsic::arm_neon_vqrshiftns:
9254 VShiftOpc = ARMISD::VQRSHRNs; break;
9255 case Intrinsic::arm_neon_vqrshiftnu:
9256 VShiftOpc = ARMISD::VQRSHRNu; break;
9257 case Intrinsic::arm_neon_vqrshiftnsu:
9258 VShiftOpc = ARMISD::VQRSHRNsu; break;
9259 }
9260
Andrew Trickef9de2a2013-05-25 02:42:55 +00009261 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009262 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009263 }
9264
9265 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009266 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009267 int64_t Cnt;
9268 unsigned VShiftOpc = 0;
9269
9270 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9271 VShiftOpc = ARMISD::VSLI;
9272 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9273 VShiftOpc = ARMISD::VSRI;
9274 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009275 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009276 }
9277
Andrew Trickef9de2a2013-05-25 02:42:55 +00009278 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009279 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009280 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009281 }
9282
9283 case Intrinsic::arm_neon_vqrshifts:
9284 case Intrinsic::arm_neon_vqrshiftu:
9285 // No immediate versions of these to check for.
9286 break;
9287 }
9288
9289 return SDValue();
9290}
9291
9292/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9293/// lowers them. As with the vector shift intrinsics, this is done during DAG
9294/// combining instead of DAG legalizing because the build_vectors for 64-bit
9295/// vector element shift counts are generally not legal, and it is hard to see
9296/// their values after they get legalized to loads from a constant pool.
9297static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9298 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009299 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009300 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9301 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9302 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9303 SDValue N1 = N->getOperand(1);
9304 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9305 SDValue N0 = N->getOperand(0);
9306 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9307 DAG.MaskedValueIsZero(N0.getOperand(0),
9308 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009309 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009310 }
9311 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009312
9313 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009314 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9315 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009316 return SDValue();
9317
9318 assert(ST->hasNEON() && "unexpected vector shift");
9319 int64_t Cnt;
9320
9321 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009322 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009323
9324 case ISD::SHL:
9325 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009326 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009327 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009328 break;
9329
9330 case ISD::SRA:
9331 case ISD::SRL:
9332 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9333 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9334 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009335 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009336 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009337 }
9338 }
9339 return SDValue();
9340}
9341
9342/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9343/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9344static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9345 const ARMSubtarget *ST) {
9346 SDValue N0 = N->getOperand(0);
9347
9348 // Check for sign- and zero-extensions of vector extract operations of 8-
9349 // and 16-bit vector elements. NEON supports these directly. They are
9350 // handled during DAG combining because type legalization will promote them
9351 // to 32-bit types and it is messy to recognize the operations after that.
9352 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9353 SDValue Vec = N0.getOperand(0);
9354 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009355 EVT VT = N->getValueType(0);
9356 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009357 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9358
Owen Anderson9f944592009-08-11 20:47:22 +00009359 if (VT == MVT::i32 &&
9360 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009361 TLI.isTypeLegal(Vec.getValueType()) &&
9362 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009363
9364 unsigned Opc = 0;
9365 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009366 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009367 case ISD::SIGN_EXTEND:
9368 Opc = ARMISD::VGETLANEs;
9369 break;
9370 case ISD::ZERO_EXTEND:
9371 case ISD::ANY_EXTEND:
9372 Opc = ARMISD::VGETLANEu;
9373 break;
9374 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009375 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009376 }
9377 }
9378
9379 return SDValue();
9380}
9381
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009382/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9383/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9384static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9385 const ARMSubtarget *ST) {
9386 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009387 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009388 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9389 // a NaN; only do the transformation when it matches that behavior.
9390
9391 // For now only do this when using NEON for FP operations; if using VFP, it
9392 // is not obvious that the benefit outweighs the cost of switching to the
9393 // NEON pipeline.
9394 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9395 N->getValueType(0) != MVT::f32)
9396 return SDValue();
9397
9398 SDValue CondLHS = N->getOperand(0);
9399 SDValue CondRHS = N->getOperand(1);
9400 SDValue LHS = N->getOperand(2);
9401 SDValue RHS = N->getOperand(3);
9402 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9403
9404 unsigned Opcode = 0;
9405 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009406 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009407 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009408 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009409 IsReversed = true ; // x CC y ? y : x
9410 } else {
9411 return SDValue();
9412 }
9413
Bob Wilsonba8ac742010-02-24 22:15:53 +00009414 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009415 switch (CC) {
9416 default: break;
9417 case ISD::SETOLT:
9418 case ISD::SETOLE:
9419 case ISD::SETLT:
9420 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009421 case ISD::SETULT:
9422 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009423 // If LHS is NaN, an ordered comparison will be false and the result will
9424 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9425 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9426 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9427 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9428 break;
9429 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9430 // will return -0, so vmin can only be used for unsafe math or if one of
9431 // the operands is known to be nonzero.
9432 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009433 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009434 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9435 break;
9436 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009437 break;
9438
9439 case ISD::SETOGT:
9440 case ISD::SETOGE:
9441 case ISD::SETGT:
9442 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009443 case ISD::SETUGT:
9444 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009445 // If LHS is NaN, an ordered comparison will be false and the result will
9446 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9447 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9448 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9449 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9450 break;
9451 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9452 // will return +0, so vmax can only be used for unsafe math or if one of
9453 // the operands is known to be nonzero.
9454 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009455 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009456 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9457 break;
9458 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009459 break;
9460 }
9461
9462 if (!Opcode)
9463 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009464 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009465}
9466
Evan Chengf863e3f2011-07-13 00:42:17 +00009467/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9468SDValue
9469ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9470 SDValue Cmp = N->getOperand(4);
9471 if (Cmp.getOpcode() != ARMISD::CMPZ)
9472 // Only looking at EQ and NE cases.
9473 return SDValue();
9474
9475 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009476 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009477 SDValue LHS = Cmp.getOperand(0);
9478 SDValue RHS = Cmp.getOperand(1);
9479 SDValue FalseVal = N->getOperand(0);
9480 SDValue TrueVal = N->getOperand(1);
9481 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009482 ARMCC::CondCodes CC =
9483 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009484
9485 // Simplify
9486 // mov r1, r0
9487 // cmp r1, x
9488 // mov r0, y
9489 // moveq r0, x
9490 // to
9491 // cmp r0, x
9492 // movne r0, y
9493 //
9494 // mov r1, r0
9495 // cmp r1, x
9496 // mov r0, x
9497 // movne r0, y
9498 // to
9499 // cmp r0, x
9500 // movne r0, y
9501 /// FIXME: Turn this into a target neutral optimization?
9502 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009503 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009504 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9505 N->getOperand(3), Cmp);
9506 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9507 SDValue ARMcc;
9508 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9509 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9510 N->getOperand(3), NewCmp);
9511 }
9512
9513 if (Res.getNode()) {
9514 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00009515 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009516 // Capture demanded bits information that would be otherwise lost.
9517 if (KnownZero == 0xfffffffe)
9518 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9519 DAG.getValueType(MVT::i1));
9520 else if (KnownZero == 0xffffff00)
9521 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9522 DAG.getValueType(MVT::i8));
9523 else if (KnownZero == 0xffff0000)
9524 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9525 DAG.getValueType(MVT::i16));
9526 }
9527
9528 return Res;
9529}
9530
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009531SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009532 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009533 switch (N->getOpcode()) {
9534 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009535 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009536 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009537 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009538 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009539 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009540 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9541 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009542 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009543 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009544 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009545 case ISD::STORE: return PerformSTORECombine(N, DCI);
9546 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9547 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009548 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009549 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009550 case ISD::FP_TO_SINT:
9551 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9552 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009553 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009554 case ISD::SHL:
9555 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009556 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009557 case ISD::SIGN_EXTEND:
9558 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009559 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9560 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009561 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009562 case ARMISD::VLD2DUP:
9563 case ARMISD::VLD3DUP:
9564 case ARMISD::VLD4DUP:
9565 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009566 case ARMISD::BUILD_VECTOR:
9567 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009568 case ISD::INTRINSIC_VOID:
9569 case ISD::INTRINSIC_W_CHAIN:
9570 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9571 case Intrinsic::arm_neon_vld1:
9572 case Intrinsic::arm_neon_vld2:
9573 case Intrinsic::arm_neon_vld3:
9574 case Intrinsic::arm_neon_vld4:
9575 case Intrinsic::arm_neon_vld2lane:
9576 case Intrinsic::arm_neon_vld3lane:
9577 case Intrinsic::arm_neon_vld4lane:
9578 case Intrinsic::arm_neon_vst1:
9579 case Intrinsic::arm_neon_vst2:
9580 case Intrinsic::arm_neon_vst3:
9581 case Intrinsic::arm_neon_vst4:
9582 case Intrinsic::arm_neon_vst2lane:
9583 case Intrinsic::arm_neon_vst3lane:
9584 case Intrinsic::arm_neon_vst4lane:
9585 return CombineBaseUpdate(N, DCI);
9586 default: break;
9587 }
9588 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009589 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009590 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009591}
9592
Evan Chengd42641c2011-02-02 01:06:55 +00009593bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9594 EVT VT) const {
9595 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9596}
9597
Matt Arsenault25793a32014-02-05 23:15:53 +00009598bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, unsigned,
9599 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009600 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009601 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009602
9603 switch (VT.getSimpleVT().SimpleTy) {
9604 default:
9605 return false;
9606 case MVT::i8:
9607 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009608 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009609 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009610 if (AllowsUnaligned) {
9611 if (Fast)
9612 *Fast = Subtarget->hasV7Ops();
9613 return true;
9614 }
9615 return false;
9616 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009617 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009618 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009619 // For any little-endian targets with neon, we can support unaligned ld/st
9620 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009621 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009622 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9623 if (Fast)
9624 *Fast = true;
9625 return true;
9626 }
9627 return false;
9628 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009629 }
9630}
9631
Lang Hames9929c422011-11-02 22:52:45 +00009632static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9633 unsigned AlignCheck) {
9634 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9635 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9636}
9637
9638EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9639 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009640 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009641 bool MemcpyStrSrc,
9642 MachineFunction &MF) const {
9643 const Function *F = MF.getFunction();
9644
9645 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009646 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009647 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009648 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9649 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009650 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009651 if (Size >= 16 &&
9652 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009653 (allowsUnalignedMemoryAccesses(MVT::v2f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009654 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009655 } else if (Size >= 8 &&
9656 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009657 (allowsUnalignedMemoryAccesses(MVT::f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009658 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009659 }
9660 }
9661
Lang Hamesb85fcd02011-11-08 18:56:23 +00009662 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009663 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009664 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009665 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009666 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009667
Lang Hames9929c422011-11-02 22:52:45 +00009668 // Let the target-independent logic figure it out.
9669 return MVT::Other;
9670}
9671
Evan Cheng9ec512d2012-12-06 19:13:27 +00009672bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9673 if (Val.getOpcode() != ISD::LOAD)
9674 return false;
9675
9676 EVT VT1 = Val.getValueType();
9677 if (!VT1.isSimple() || !VT1.isInteger() ||
9678 !VT2.isSimple() || !VT2.isInteger())
9679 return false;
9680
9681 switch (VT1.getSimpleVT().SimpleTy) {
9682 default: break;
9683 case MVT::i1:
9684 case MVT::i8:
9685 case MVT::i16:
9686 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9687 return true;
9688 }
9689
9690 return false;
9691}
9692
Tim Northovercc2e9032013-08-06 13:58:03 +00009693bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9694 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9695 return false;
9696
9697 if (!isTypeLegal(EVT::getEVT(Ty1)))
9698 return false;
9699
9700 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9701
9702 // Assuming the caller doesn't have a zeroext or signext return parameter,
9703 // truncation all the way down to i1 is valid.
9704 return true;
9705}
9706
9707
Evan Chengdc49a8d2009-08-14 20:09:37 +00009708static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9709 if (V < 0)
9710 return false;
9711
9712 unsigned Scale = 1;
9713 switch (VT.getSimpleVT().SimpleTy) {
9714 default: return false;
9715 case MVT::i1:
9716 case MVT::i8:
9717 // Scale == 1;
9718 break;
9719 case MVT::i16:
9720 // Scale == 2;
9721 Scale = 2;
9722 break;
9723 case MVT::i32:
9724 // Scale == 4;
9725 Scale = 4;
9726 break;
9727 }
9728
9729 if ((V & (Scale - 1)) != 0)
9730 return false;
9731 V /= Scale;
9732 return V == (V & ((1LL << 5) - 1));
9733}
9734
9735static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9736 const ARMSubtarget *Subtarget) {
9737 bool isNeg = false;
9738 if (V < 0) {
9739 isNeg = true;
9740 V = - V;
9741 }
9742
9743 switch (VT.getSimpleVT().SimpleTy) {
9744 default: return false;
9745 case MVT::i1:
9746 case MVT::i8:
9747 case MVT::i16:
9748 case MVT::i32:
9749 // + imm12 or - imm8
9750 if (isNeg)
9751 return V == (V & ((1LL << 8) - 1));
9752 return V == (V & ((1LL << 12) - 1));
9753 case MVT::f32:
9754 case MVT::f64:
9755 // Same as ARM mode. FIXME: NEON?
9756 if (!Subtarget->hasVFP2())
9757 return false;
9758 if ((V & 3) != 0)
9759 return false;
9760 V >>= 2;
9761 return V == (V & ((1LL << 8) - 1));
9762 }
9763}
9764
Evan Cheng2150b922007-03-12 23:30:29 +00009765/// isLegalAddressImmediate - Return true if the integer value can be used
9766/// as the offset of the target addressing mode for load / store of the
9767/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009768static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009769 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009770 if (V == 0)
9771 return true;
9772
Evan Chengce5dfb62009-03-09 19:15:00 +00009773 if (!VT.isSimple())
9774 return false;
9775
Evan Chengdc49a8d2009-08-14 20:09:37 +00009776 if (Subtarget->isThumb1Only())
9777 return isLegalT1AddressImmediate(V, VT);
9778 else if (Subtarget->isThumb2())
9779 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009780
Evan Chengdc49a8d2009-08-14 20:09:37 +00009781 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009782 if (V < 0)
9783 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009784 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009785 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009786 case MVT::i1:
9787 case MVT::i8:
9788 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009789 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009790 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009791 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009792 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009793 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009794 case MVT::f32:
9795 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009796 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009797 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009798 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009799 return false;
9800 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009801 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009802 }
Evan Cheng10043e22007-01-19 07:51:42 +00009803}
9804
Evan Chengdc49a8d2009-08-14 20:09:37 +00009805bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9806 EVT VT) const {
9807 int Scale = AM.Scale;
9808 if (Scale < 0)
9809 return false;
9810
9811 switch (VT.getSimpleVT().SimpleTy) {
9812 default: return false;
9813 case MVT::i1:
9814 case MVT::i8:
9815 case MVT::i16:
9816 case MVT::i32:
9817 if (Scale == 1)
9818 return true;
9819 // r + r << imm
9820 Scale = Scale & ~1;
9821 return Scale == 2 || Scale == 4 || Scale == 8;
9822 case MVT::i64:
9823 // r + r
9824 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9825 return true;
9826 return false;
9827 case MVT::isVoid:
9828 // Note, we allow "void" uses (basically, uses that aren't loads or
9829 // stores), because arm allows folding a scale into many arithmetic
9830 // operations. This should be made more precise and revisited later.
9831
9832 // Allow r << imm, but the imm has to be a multiple of two.
9833 if (Scale & 1) return false;
9834 return isPowerOf2_32(Scale);
9835 }
9836}
9837
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009838/// isLegalAddressingMode - Return true if the addressing mode represented
9839/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009840bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009841 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009842 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009843 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009844 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009845
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009846 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009847 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009848 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009849
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009850 switch (AM.Scale) {
9851 case 0: // no scale reg, must be "r+i" or "r", or "i".
9852 break;
9853 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009854 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009855 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009856 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009857 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009858 // ARM doesn't support any R+R*scale+imm addr modes.
9859 if (AM.BaseOffs)
9860 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009861
Bob Wilson866c1742009-04-08 17:55:28 +00009862 if (!VT.isSimple())
9863 return false;
9864
Evan Chengdc49a8d2009-08-14 20:09:37 +00009865 if (Subtarget->isThumb2())
9866 return isLegalT2ScaledAddressingMode(AM, VT);
9867
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009868 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +00009869 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009870 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009871 case MVT::i1:
9872 case MVT::i8:
9873 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009874 if (Scale < 0) Scale = -Scale;
9875 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009876 return true;
9877 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +00009878 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +00009879 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009880 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009881 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009882 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009883 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +00009884 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009885
Owen Anderson9f944592009-08-11 20:47:22 +00009886 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009887 // Note, we allow "void" uses (basically, uses that aren't loads or
9888 // stores), because arm allows folding a scale into many arithmetic
9889 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +00009890
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009891 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +00009892 if (Scale & 1) return false;
9893 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009894 }
Evan Cheng2150b922007-03-12 23:30:29 +00009895 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009896 return true;
Evan Cheng2150b922007-03-12 23:30:29 +00009897}
9898
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009899/// isLegalICmpImmediate - Return true if the specified immediate is legal
9900/// icmp immediate, that is the target has icmp instructions which can compare
9901/// a register against the immediate without having to materialize the
9902/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +00009903bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009904 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009905 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009906 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009907 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009908 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009909 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +00009910 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009911}
9912
Andrew Tricka22cdb72012-07-18 18:34:27 +00009913/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9914/// *or sub* immediate, that is the target has add or sub instructions which can
9915/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +00009916/// immediate into a register.
9917bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +00009918 // Same encoding for add/sub, just flip the sign.
9919 int64_t AbsImm = llvm::abs64(Imm);
9920 if (!Subtarget->isThumb())
9921 return ARM_AM::getSOImmVal(AbsImm) != -1;
9922 if (Subtarget->isThumb2())
9923 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9924 // Thumb1 only has 8-bit unsigned immediate.
9925 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +00009926}
9927
Owen Anderson53aa7a92009-08-10 22:56:29 +00009928static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009929 bool isSEXTLoad, SDValue &Base,
9930 SDValue &Offset, bool &isInc,
9931 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +00009932 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9933 return false;
9934
Owen Anderson9f944592009-08-11 20:47:22 +00009935 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +00009936 // AddressingMode 3
9937 Base = Ptr->getOperand(0);
9938 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009939 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009940 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009941 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009942 isInc = false;
9943 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9944 return true;
9945 }
9946 }
9947 isInc = (Ptr->getOpcode() == ISD::ADD);
9948 Offset = Ptr->getOperand(1);
9949 return true;
Owen Anderson9f944592009-08-11 20:47:22 +00009950 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +00009951 // AddressingMode 2
9952 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009953 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009954 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009955 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009956 isInc = false;
9957 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9958 Base = Ptr->getOperand(0);
9959 return true;
9960 }
9961 }
9962
9963 if (Ptr->getOpcode() == ISD::ADD) {
9964 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +00009965 ARM_AM::ShiftOpc ShOpcVal=
9966 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +00009967 if (ShOpcVal != ARM_AM::no_shift) {
9968 Base = Ptr->getOperand(1);
9969 Offset = Ptr->getOperand(0);
9970 } else {
9971 Base = Ptr->getOperand(0);
9972 Offset = Ptr->getOperand(1);
9973 }
9974 return true;
9975 }
9976
9977 isInc = (Ptr->getOpcode() == ISD::ADD);
9978 Base = Ptr->getOperand(0);
9979 Offset = Ptr->getOperand(1);
9980 return true;
9981 }
9982
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009983 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +00009984 return false;
9985}
9986
Owen Anderson53aa7a92009-08-10 22:56:29 +00009987static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009988 bool isSEXTLoad, SDValue &Base,
9989 SDValue &Offset, bool &isInc,
9990 SelectionDAG &DAG) {
9991 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9992 return false;
9993
9994 Base = Ptr->getOperand(0);
9995 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9996 int RHSC = (int)RHS->getZExtValue();
9997 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9998 assert(Ptr->getOpcode() == ISD::ADD);
9999 isInc = false;
10000 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10001 return true;
10002 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10003 isInc = Ptr->getOpcode() == ISD::ADD;
10004 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10005 return true;
10006 }
10007 }
10008
10009 return false;
10010}
10011
Evan Cheng10043e22007-01-19 07:51:42 +000010012/// getPreIndexedAddressParts - returns true by value, base pointer and
10013/// offset pointer and addressing mode by reference if the node's address
10014/// can be legally represented as pre-indexed load / store address.
10015bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010016ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10017 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010018 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010019 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010020 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010021 return false;
10022
Owen Anderson53aa7a92009-08-10 22:56:29 +000010023 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010024 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010025 bool isSEXTLoad = false;
10026 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10027 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010028 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010029 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10030 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10031 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010032 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010033 } else
10034 return false;
10035
10036 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010037 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010038 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010039 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10040 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010041 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010042 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010043 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010044 if (!isLegal)
10045 return false;
10046
10047 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10048 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010049}
10050
10051/// getPostIndexedAddressParts - returns true by value, base pointer and
10052/// offset pointer and addressing mode by reference if this node can be
10053/// combined with a load / store to form a post-indexed load / store.
10054bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010055 SDValue &Base,
10056 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010057 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010058 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010059 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010060 return false;
10061
Owen Anderson53aa7a92009-08-10 22:56:29 +000010062 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010063 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010064 bool isSEXTLoad = false;
10065 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010066 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010067 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010068 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10069 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010070 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010071 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010072 } else
10073 return false;
10074
10075 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010076 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010077 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010078 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010079 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010080 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010081 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10082 isInc, DAG);
10083 if (!isLegal)
10084 return false;
10085
Evan Chengf19384d2010-05-18 21:31:17 +000010086 if (Ptr != Base) {
10087 // Swap base ptr and offset to catch more post-index load / store when
10088 // it's legal. In Thumb2 mode, offset must be an immediate.
10089 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10090 !Subtarget->isThumb2())
10091 std::swap(Base, Offset);
10092
10093 // Post-indexed load / store update the base pointer.
10094 if (Ptr != Base)
10095 return false;
10096 }
10097
Evan Cheng84c6cda2009-07-02 07:28:31 +000010098 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10099 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010100}
10101
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010102void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson7117a912009-03-20 22:42:55 +000010103 APInt &KnownZero,
Dan Gohmanf990faf2008-02-13 00:35:47 +000010104 APInt &KnownOne,
Dan Gohman309d3d52007-06-22 14:59:07 +000010105 const SelectionDAG &DAG,
Evan Cheng10043e22007-01-19 07:51:42 +000010106 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010107 unsigned BitWidth = KnownOne.getBitWidth();
10108 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010109 switch (Op.getOpcode()) {
10110 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010111 case ARMISD::ADDC:
10112 case ARMISD::ADDE:
10113 case ARMISD::SUBC:
10114 case ARMISD::SUBE:
10115 // These nodes' second result is a boolean
10116 if (Op.getResNo() == 0)
10117 break;
10118 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10119 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010120 case ARMISD::CMOV: {
10121 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +000010122 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010123 if (KnownZero == 0 && KnownOne == 0) return;
10124
Dan Gohmanf990faf2008-02-13 00:35:47 +000010125 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +000010126 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010127 KnownZero &= KnownZeroRHS;
10128 KnownOne &= KnownOneRHS;
10129 return;
10130 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010131 case ISD::INTRINSIC_W_CHAIN: {
10132 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10133 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10134 switch (IntID) {
10135 default: return;
10136 case Intrinsic::arm_ldaex:
10137 case Intrinsic::arm_ldrex: {
10138 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10139 unsigned MemBits = VT.getScalarType().getSizeInBits();
10140 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10141 return;
10142 }
10143 }
10144 }
Evan Cheng10043e22007-01-19 07:51:42 +000010145 }
10146}
10147
10148//===----------------------------------------------------------------------===//
10149// ARM Inline Assembly Support
10150//===----------------------------------------------------------------------===//
10151
Evan Cheng078b0b02011-01-08 01:24:27 +000010152bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10153 // Looking for "rev" which is V6+.
10154 if (!Subtarget->hasV6Ops())
10155 return false;
10156
10157 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10158 std::string AsmStr = IA->getAsmString();
10159 SmallVector<StringRef, 4> AsmPieces;
10160 SplitString(AsmStr, AsmPieces, ";\n");
10161
10162 switch (AsmPieces.size()) {
10163 default: return false;
10164 case 1:
10165 AsmStr = AsmPieces[0];
10166 AsmPieces.clear();
10167 SplitString(AsmStr, AsmPieces, " \t,");
10168
10169 // rev $0, $1
10170 if (AsmPieces.size() == 3 &&
10171 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10172 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010173 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010174 if (Ty && Ty->getBitWidth() == 32)
10175 return IntrinsicLowering::LowerToByteSwap(CI);
10176 }
10177 break;
10178 }
10179
10180 return false;
10181}
10182
Evan Cheng10043e22007-01-19 07:51:42 +000010183/// getConstraintType - Given a constraint letter, return the type of
10184/// constraint it is for this target.
10185ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010186ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10187 if (Constraint.size() == 1) {
10188 switch (Constraint[0]) {
10189 default: break;
10190 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010191 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010192 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010193 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010194 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010195 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010196 // An address with a single base register. Due to the way we
10197 // currently handle addresses it is the same as an 'r' memory constraint.
10198 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010199 }
Eric Christophere256cd02011-06-21 22:10:57 +000010200 } else if (Constraint.size() == 2) {
10201 switch (Constraint[0]) {
10202 default: break;
10203 // All 'U+' constraints are addresses.
10204 case 'U': return C_Memory;
10205 }
Evan Cheng10043e22007-01-19 07:51:42 +000010206 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010207 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010208}
10209
John Thompsone8360b72010-10-29 17:29:13 +000010210/// Examine constraint type and operand type and determine a weight value.
10211/// This object must already have been set up with the operand type
10212/// and the current alternative constraint selected.
10213TargetLowering::ConstraintWeight
10214ARMTargetLowering::getSingleConstraintMatchWeight(
10215 AsmOperandInfo &info, const char *constraint) const {
10216 ConstraintWeight weight = CW_Invalid;
10217 Value *CallOperandVal = info.CallOperandVal;
10218 // If we don't have a value, we can't do a match,
10219 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010220 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010221 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010222 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010223 // Look at the constraint type.
10224 switch (*constraint) {
10225 default:
10226 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10227 break;
10228 case 'l':
10229 if (type->isIntegerTy()) {
10230 if (Subtarget->isThumb())
10231 weight = CW_SpecificReg;
10232 else
10233 weight = CW_Register;
10234 }
10235 break;
10236 case 'w':
10237 if (type->isFloatingPointTy())
10238 weight = CW_Register;
10239 break;
10240 }
10241 return weight;
10242}
10243
Eric Christophercf2007c2011-06-30 23:50:52 +000010244typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10245RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010246ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010247 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010248 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010249 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010250 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010251 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010252 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010253 return RCPair(0U, &ARM::tGPRRegClass);
10254 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010255 case 'h': // High regs or no regs.
10256 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010257 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010258 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010259 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010260 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010261 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010262 if (VT == MVT::Other)
10263 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010264 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010265 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010266 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010267 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010268 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010269 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010270 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010271 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010272 if (VT == MVT::Other)
10273 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010274 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010275 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010276 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010277 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010278 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010279 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010280 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010281 case 't':
10282 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010283 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010284 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010285 }
10286 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010287 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010288 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010289
Evan Cheng10043e22007-01-19 07:51:42 +000010290 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10291}
10292
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010293/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10294/// vector. If it is invalid, don't add anything to Ops.
10295void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010296 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010297 std::vector<SDValue>&Ops,
10298 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010299 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010300
Eric Christopherde9399b2011-06-02 23:16:42 +000010301 // Currently only support length 1 constraints.
10302 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010303
Eric Christopherde9399b2011-06-02 23:16:42 +000010304 char ConstraintLetter = Constraint[0];
10305 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010306 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010307 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010308 case 'I': case 'J': case 'K': case 'L':
10309 case 'M': case 'N': case 'O':
10310 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10311 if (!C)
10312 return;
10313
10314 int64_t CVal64 = C->getSExtValue();
10315 int CVal = (int) CVal64;
10316 // None of these constraints allow values larger than 32 bits. Check
10317 // that the value fits in an int.
10318 if (CVal != CVal64)
10319 return;
10320
Eric Christopherde9399b2011-06-02 23:16:42 +000010321 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010322 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010323 // Constant suitable for movw, must be between 0 and
10324 // 65535.
10325 if (Subtarget->hasV6T2Ops())
10326 if (CVal >= 0 && CVal <= 65535)
10327 break;
10328 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010329 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010330 if (Subtarget->isThumb1Only()) {
10331 // This must be a constant between 0 and 255, for ADD
10332 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010333 if (CVal >= 0 && CVal <= 255)
10334 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010335 } else if (Subtarget->isThumb2()) {
10336 // A constant that can be used as an immediate value in a
10337 // data-processing instruction.
10338 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10339 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010340 } else {
10341 // A constant that can be used as an immediate value in a
10342 // data-processing instruction.
10343 if (ARM_AM::getSOImmVal(CVal) != -1)
10344 break;
10345 }
10346 return;
10347
10348 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010349 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010350 // This must be a constant between -255 and -1, for negated ADD
10351 // immediates. This can be used in GCC with an "n" modifier that
10352 // prints the negated value, for use with SUB instructions. It is
10353 // not useful otherwise but is implemented for compatibility.
10354 if (CVal >= -255 && CVal <= -1)
10355 break;
10356 } else {
10357 // This must be a constant between -4095 and 4095. It is not clear
10358 // what this constraint is intended for. Implemented for
10359 // compatibility with GCC.
10360 if (CVal >= -4095 && CVal <= 4095)
10361 break;
10362 }
10363 return;
10364
10365 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010366 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010367 // A 32-bit value where only one byte has a nonzero value. Exclude
10368 // zero to match GCC. This constraint is used by GCC internally for
10369 // constants that can be loaded with a move/shift combination.
10370 // It is not useful otherwise but is implemented for compatibility.
10371 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10372 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010373 } else if (Subtarget->isThumb2()) {
10374 // A constant whose bitwise inverse can be used as an immediate
10375 // value in a data-processing instruction. This can be used in GCC
10376 // with a "B" modifier that prints the inverted value, for use with
10377 // BIC and MVN instructions. It is not useful otherwise but is
10378 // implemented for compatibility.
10379 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10380 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010381 } else {
10382 // A constant whose bitwise inverse can be used as an immediate
10383 // value in a data-processing instruction. This can be used in GCC
10384 // with a "B" modifier that prints the inverted value, for use with
10385 // BIC and MVN instructions. It is not useful otherwise but is
10386 // implemented for compatibility.
10387 if (ARM_AM::getSOImmVal(~CVal) != -1)
10388 break;
10389 }
10390 return;
10391
10392 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010393 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010394 // This must be a constant between -7 and 7,
10395 // for 3-operand ADD/SUB immediate instructions.
10396 if (CVal >= -7 && CVal < 7)
10397 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010398 } else if (Subtarget->isThumb2()) {
10399 // A constant whose negation can be used as an immediate value in a
10400 // data-processing instruction. This can be used in GCC with an "n"
10401 // modifier that prints the negated value, for use with SUB
10402 // instructions. It is not useful otherwise but is implemented for
10403 // compatibility.
10404 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10405 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010406 } else {
10407 // A constant whose negation can be used as an immediate value in a
10408 // data-processing instruction. This can be used in GCC with an "n"
10409 // modifier that prints the negated value, for use with SUB
10410 // instructions. It is not useful otherwise but is implemented for
10411 // compatibility.
10412 if (ARM_AM::getSOImmVal(-CVal) != -1)
10413 break;
10414 }
10415 return;
10416
10417 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010418 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010419 // This must be a multiple of 4 between 0 and 1020, for
10420 // ADD sp + immediate.
10421 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10422 break;
10423 } else {
10424 // A power of two or a constant between 0 and 32. This is used in
10425 // GCC for the shift amount on shifted register operands, but it is
10426 // useful in general for any shift amounts.
10427 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10428 break;
10429 }
10430 return;
10431
10432 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010433 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010434 // This must be a constant between 0 and 31, for shift amounts.
10435 if (CVal >= 0 && CVal <= 31)
10436 break;
10437 }
10438 return;
10439
10440 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010441 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010442 // This must be a multiple of 4 between -508 and 508, for
10443 // ADD/SUB sp = sp + immediate.
10444 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10445 break;
10446 }
10447 return;
10448 }
10449 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10450 break;
10451 }
10452
10453 if (Result.getNode()) {
10454 Ops.push_back(Result);
10455 return;
10456 }
Dale Johannesence97d552010-06-25 21:55:36 +000010457 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010458}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010459
Renato Golin87610692013-07-16 09:32:17 +000010460SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10461 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10462 unsigned Opcode = Op->getOpcode();
10463 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10464 "Invalid opcode for Div/Rem lowering");
10465 bool isSigned = (Opcode == ISD::SDIVREM);
10466 EVT VT = Op->getValueType(0);
10467 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10468
10469 RTLIB::Libcall LC;
10470 switch (VT.getSimpleVT().SimpleTy) {
10471 default: llvm_unreachable("Unexpected request for libcall!");
10472 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10473 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10474 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10475 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10476 }
10477
10478 SDValue InChain = DAG.getEntryNode();
10479
10480 TargetLowering::ArgListTy Args;
10481 TargetLowering::ArgListEntry Entry;
10482 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10483 EVT ArgVT = Op->getOperand(i).getValueType();
10484 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10485 Entry.Node = Op->getOperand(i);
10486 Entry.Ty = ArgTy;
10487 Entry.isSExt = isSigned;
10488 Entry.isZExt = !isSigned;
10489 Args.push_back(Entry);
10490 }
10491
10492 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10493 getPointerTy());
10494
10495 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10496
10497 SDLoc dl(Op);
10498 TargetLowering::
10499 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, true,
10500 0, getLibcallCallingConv(LC), /*isTailCall=*/false,
10501 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
10502 Callee, Args, DAG, dl);
10503 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
10504
10505 return CallInfo.first;
10506}
10507
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010508bool
10509ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10510 // The ARM target isn't yet aware of offsets.
10511 return false;
10512}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010513
Jim Grosbach11013ed2010-07-16 23:05:05 +000010514bool ARM::isBitFieldInvertedMask(unsigned v) {
10515 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010516 return false;
10517
Jim Grosbach11013ed2010-07-16 23:05:05 +000010518 // there can be 1's on either or both "outsides", all the "inside"
10519 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010520 unsigned TO = CountTrailingOnes_32(v);
10521 unsigned LO = CountLeadingOnes_32(v);
10522 v = (v >> TO) << TO;
10523 v = (v << LO) >> LO;
10524 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010525}
10526
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010527/// isFPImmLegal - Returns true if the target can instruction select the
10528/// specified FP immediate natively. If false, the legalizer will
10529/// materialize the FP immediate as a load from a constant pool.
10530bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10531 if (!Subtarget->hasVFP3())
10532 return false;
10533 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010534 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010535 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010536 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010537 return false;
10538}
Bob Wilson5549d492010-09-21 17:56:22 +000010539
Wesley Peck527da1b2010-11-23 03:31:01 +000010540/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010541/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10542/// specified in the intrinsic calls.
10543bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10544 const CallInst &I,
10545 unsigned Intrinsic) const {
10546 switch (Intrinsic) {
10547 case Intrinsic::arm_neon_vld1:
10548 case Intrinsic::arm_neon_vld2:
10549 case Intrinsic::arm_neon_vld3:
10550 case Intrinsic::arm_neon_vld4:
10551 case Intrinsic::arm_neon_vld2lane:
10552 case Intrinsic::arm_neon_vld3lane:
10553 case Intrinsic::arm_neon_vld4lane: {
10554 Info.opc = ISD::INTRINSIC_W_CHAIN;
10555 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010556 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010557 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10558 Info.ptrVal = I.getArgOperand(0);
10559 Info.offset = 0;
10560 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10561 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10562 Info.vol = false; // volatile loads with NEON intrinsics not supported
10563 Info.readMem = true;
10564 Info.writeMem = false;
10565 return true;
10566 }
10567 case Intrinsic::arm_neon_vst1:
10568 case Intrinsic::arm_neon_vst2:
10569 case Intrinsic::arm_neon_vst3:
10570 case Intrinsic::arm_neon_vst4:
10571 case Intrinsic::arm_neon_vst2lane:
10572 case Intrinsic::arm_neon_vst3lane:
10573 case Intrinsic::arm_neon_vst4lane: {
10574 Info.opc = ISD::INTRINSIC_VOID;
10575 // Conservatively set memVT to the entire set of vectors stored.
10576 unsigned NumElts = 0;
10577 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010578 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010579 if (!ArgTy->isVectorTy())
10580 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010581 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010582 }
10583 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10584 Info.ptrVal = I.getArgOperand(0);
10585 Info.offset = 0;
10586 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10587 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10588 Info.vol = false; // volatile stores with NEON intrinsics not supported
10589 Info.readMem = false;
10590 Info.writeMem = true;
10591 return true;
10592 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010593 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010594 case Intrinsic::arm_ldrex: {
10595 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10596 Info.opc = ISD::INTRINSIC_W_CHAIN;
10597 Info.memVT = MVT::getVT(PtrTy->getElementType());
10598 Info.ptrVal = I.getArgOperand(0);
10599 Info.offset = 0;
10600 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10601 Info.vol = true;
10602 Info.readMem = true;
10603 Info.writeMem = false;
10604 return true;
10605 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010606 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010607 case Intrinsic::arm_strex: {
10608 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10609 Info.opc = ISD::INTRINSIC_W_CHAIN;
10610 Info.memVT = MVT::getVT(PtrTy->getElementType());
10611 Info.ptrVal = I.getArgOperand(1);
10612 Info.offset = 0;
10613 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10614 Info.vol = true;
10615 Info.readMem = false;
10616 Info.writeMem = true;
10617 return true;
10618 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010619 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010620 case Intrinsic::arm_strexd: {
10621 Info.opc = ISD::INTRINSIC_W_CHAIN;
10622 Info.memVT = MVT::i64;
10623 Info.ptrVal = I.getArgOperand(2);
10624 Info.offset = 0;
10625 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010626 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010627 Info.readMem = false;
10628 Info.writeMem = true;
10629 return true;
10630 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010631 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010632 case Intrinsic::arm_ldrexd: {
10633 Info.opc = ISD::INTRINSIC_W_CHAIN;
10634 Info.memVT = MVT::i64;
10635 Info.ptrVal = I.getArgOperand(0);
10636 Info.offset = 0;
10637 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010638 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010639 Info.readMem = true;
10640 Info.writeMem = false;
10641 return true;
10642 }
Bob Wilson5549d492010-09-21 17:56:22 +000010643 default:
10644 break;
10645 }
10646
10647 return false;
10648}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010649
10650/// \brief Returns true if it is beneficial to convert a load of a constant
10651/// to just the constant itself.
10652bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10653 Type *Ty) const {
10654 assert(Ty->isIntegerTy());
10655
10656 unsigned Bits = Ty->getPrimitiveSizeInBits();
10657 if (Bits == 0 || Bits > 32)
10658 return false;
10659 return true;
10660}
Tim Northover037f26f22014-04-17 18:22:47 +000010661
10662bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10663 // Loads and stores less than 64-bits are already atomic; ones above that
10664 // are doomed anyway, so defer to the default libcall and blame the OS when
10665 // things go wrong:
10666 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
10667 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() == 64;
10668 else if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
10669 return LI->getType()->getPrimitiveSizeInBits() == 64;
10670
10671 // For the real atomic operations, we have ldrex/strex up to 64 bits.
10672 return Inst->getType()->getPrimitiveSizeInBits() <= 64;
10673}
10674
10675Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10676 AtomicOrdering Ord) const {
10677 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10678 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10679 bool IsAcquire =
10680 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10681
10682 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10683 // intrinsic must return {i32, i32} and we have to recombine them into a
10684 // single i64 here.
10685 if (ValTy->getPrimitiveSizeInBits() == 64) {
10686 Intrinsic::ID Int =
10687 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10688 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10689
10690 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10691 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10692
10693 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10694 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010695 if (!Subtarget->isLittle())
10696 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010697 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10698 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10699 return Builder.CreateOr(
10700 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10701 }
10702
10703 Type *Tys[] = { Addr->getType() };
10704 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10705 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10706
10707 return Builder.CreateTruncOrBitCast(
10708 Builder.CreateCall(Ldrex, Addr),
10709 cast<PointerType>(Addr->getType())->getElementType());
10710}
10711
10712Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10713 Value *Addr,
10714 AtomicOrdering Ord) const {
10715 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10716 bool IsRelease =
10717 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10718
10719 // Since the intrinsics must have legal type, the i64 intrinsics take two
10720 // parameters: "i32, i32". We must marshal Val into the appropriate form
10721 // before the call.
10722 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10723 Intrinsic::ID Int =
10724 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10725 Function *Strex = Intrinsic::getDeclaration(M, Int);
10726 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10727
10728 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10729 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010730 if (!Subtarget->isLittle())
10731 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010732 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10733 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10734 }
10735
10736 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10737 Type *Tys[] = { Addr->getType() };
10738 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10739
10740 return Builder.CreateCall2(
10741 Strex, Builder.CreateZExtOrBitCast(
10742 Val, Strex->getFunctionType()->getParamType(0)),
10743 Addr);
10744}
Oliver Stannardc24f2172014-05-09 14:01:47 +000010745
10746enum HABaseType {
10747 HA_UNKNOWN = 0,
10748 HA_FLOAT,
10749 HA_DOUBLE,
10750 HA_VECT64,
10751 HA_VECT128
10752};
10753
10754static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
10755 uint64_t &Members) {
10756 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
10757 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
10758 uint64_t SubMembers = 0;
10759 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
10760 return false;
10761 Members += SubMembers;
10762 }
10763 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
10764 uint64_t SubMembers = 0;
10765 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
10766 return false;
10767 Members += SubMembers * AT->getNumElements();
10768 } else if (Ty->isFloatTy()) {
10769 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
10770 return false;
10771 Members = 1;
10772 Base = HA_FLOAT;
10773 } else if (Ty->isDoubleTy()) {
10774 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
10775 return false;
10776 Members = 1;
10777 Base = HA_DOUBLE;
10778 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
10779 Members = 1;
10780 switch (Base) {
10781 case HA_FLOAT:
10782 case HA_DOUBLE:
10783 return false;
10784 case HA_VECT64:
10785 return VT->getBitWidth() == 64;
10786 case HA_VECT128:
10787 return VT->getBitWidth() == 128;
10788 case HA_UNKNOWN:
10789 switch (VT->getBitWidth()) {
10790 case 64:
10791 Base = HA_VECT64;
10792 return true;
10793 case 128:
10794 Base = HA_VECT128;
10795 return true;
10796 default:
10797 return false;
10798 }
10799 }
10800 }
10801
10802 return (Members > 0 && Members <= 4);
10803}
10804
10805/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
10806bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
10807 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
10808 if (getEffectiveCallingConv(CallConv, isVarArg) ==
10809 CallingConv::ARM_AAPCS_VFP) {
10810 HABaseType Base = HA_UNKNOWN;
10811 uint64_t Members = 0;
10812 bool result = isHomogeneousAggregate(Ty, Base, Members);
10813 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump(); dbgs() << "\n");
10814 return result;
10815 } else {
10816 return false;
10817 }
10818}