blob: 3c7356ada6cac98ce78b83b6de3b3dd8b2af1652 [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"
Pete Cooperef21bd42015-03-04 01:24:11 +000026#include "llvm/ADT/StringSwitch.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000027#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000028#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/Function.h"
40#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000041#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Instructions.h"
John Brawn0dbcd652015-03-18 12:01:59 +000044#include "llvm/IR/IntrinsicInst.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000045#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000047#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000048#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000049#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000052#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000053#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000054#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000055using namespace llvm;
56
Chandler Carruth84e68b22014-04-22 02:41:26 +000057#define DEBUG_TYPE "arm-isel"
58
Dale Johannesend679ff72010-06-03 21:09:53 +000059STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000060STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000061STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000062
Evan Chengf128bdc2010-06-16 07:35:02 +000063static cl::opt<bool>
64ARMInterworking("arm-interworking", cl::Hidden,
65 cl::desc("Enable / disable ARM interworking (for debugging only)"),
66 cl::init(true));
67
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000068namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000069 class ARMCCState : public CCState {
70 public:
71 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000072 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
73 ParmContext PC)
74 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000075 assert(((PC == Call) || (PC == Prologue)) &&
76 "ARMCCState users must specify whether their context is call"
77 "or prologue generation.");
78 CallOrPrologue = PC;
79 }
80 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000081}
Cameron Zwarich89019782011-06-10 20:59:24 +000082
Stuart Hastings45fe3c32011-04-20 16:47:52 +000083// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000084static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000085 ARM::R0, ARM::R1, ARM::R2, ARM::R3
86};
87
Craig Topper4fa625f2012-08-12 03:16:37 +000088void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
89 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000090 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000091 setOperationAction(ISD::LOAD, VT, Promote);
92 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000093
Craig Topper4fa625f2012-08-12 03:16:37 +000094 setOperationAction(ISD::STORE, VT, Promote);
95 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000096 }
97
Craig Topper4fa625f2012-08-12 03:16:37 +000098 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +000099 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000100 setOperationAction(ISD::SETCC, VT, Custom);
101 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
102 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000103 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000104 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
105 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
107 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000108 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
110 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
112 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000113 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000114 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
115 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
116 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
117 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
118 setOperationAction(ISD::SELECT, VT, Expand);
119 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000120 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000122 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000123 setOperationAction(ISD::SHL, VT, Custom);
124 setOperationAction(ISD::SRA, VT, Custom);
125 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000126 }
127
128 // Promote all bit-wise operations.
129 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000130 setOperationAction(ISD::AND, VT, Promote);
131 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
132 setOperationAction(ISD::OR, VT, Promote);
133 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
134 setOperationAction(ISD::XOR, VT, Promote);
135 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000136 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000137
138 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000139 setOperationAction(ISD::SDIV, VT, Expand);
140 setOperationAction(ISD::UDIV, VT, Expand);
141 setOperationAction(ISD::FDIV, VT, Expand);
142 setOperationAction(ISD::SREM, VT, Expand);
143 setOperationAction(ISD::UREM, VT, Expand);
144 setOperationAction(ISD::FREM, VT, Expand);
James Molloya6702e22015-07-17 17:10:55 +0000145
Silviu Barangaad1b19f2015-08-19 14:11:27 +0000146 if (!VT.isFloatingPoint() &&
147 VT != MVT::v2i64 && VT != MVT::v1i64)
148 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
149 setOperationAction(Opcode, VT, Legal);
Bob Wilson2e076c42009-06-22 23:27:02 +0000150}
151
Craig Topper4fa625f2012-08-12 03:16:37 +0000152void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000153 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Craig Topper4fa625f2012-08-12 03:16:37 +0000157void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000158 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000160}
161
Eric Christopher1889fdc2015-01-29 00:19:39 +0000162ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
163 const ARMSubtarget &STI)
164 : TargetLowering(TM), Subtarget(&STI) {
165 RegInfo = Subtarget->getRegisterInfo();
166 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000167
Duncan Sandsf2641e12011-09-06 19:07:46 +0000168 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
169
Tim Northoverd6a729b2014-01-06 14:28:05 +0000170 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000171 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000172 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000173 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000174 static const struct {
175 const RTLIB::Libcall Op;
176 const char * const Name;
177 const ISD::CondCode Cond;
178 } LibraryCalls[] = {
179 // Single-precision floating-point arithmetic.
180 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
181 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
182 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
183 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000184
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000185 // Double-precision floating-point arithmetic.
186 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
187 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
188 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
189 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
Evan Cheng143576d2007-01-31 09:30:58 +0000190
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000191 // Single-precision comparisons.
192 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE },
193 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE },
194 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE },
195 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE },
196 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE },
197 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE },
198 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE },
199 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ },
Evan Cheng10043e22007-01-19 07:51:42 +0000200
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000201 // Double-precision comparisons.
202 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE },
203 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE },
204 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE },
205 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE },
206 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE },
207 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE },
208 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE },
209 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ },
Evan Cheng143576d2007-01-31 09:30:58 +0000210
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000211 // Floating-point to integer conversions.
212 // i64 conversions are done via library routines even when generating VFP
213 // instructions, so use the same ones.
214 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID },
215 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
216 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID },
217 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000218
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000219 // Conversions between floating types.
220 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID },
221 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000222
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000223 // Integer to floating-point conversions.
224 // i64 conversions are done via library routines even when generating VFP
225 // instructions, so use the same ones.
226 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
227 // e.g., __floatunsidf vs. __floatunssidfvfp.
228 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID },
229 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
230 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID },
231 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
232 };
Evan Cheng10043e22007-01-19 07:51:42 +0000233
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000234 for (const auto &LC : LibraryCalls) {
235 setLibcallName(LC.Op, LC.Name);
236 if (LC.Cond != ISD::SETCC_INVALID)
237 setCmpLibcallCC(LC.Op, LC.Cond);
238 }
Evan Chengc9f22fd12007-04-27 08:15:43 +0000239 }
Tim Northover8b403662015-10-28 22:51:16 +0000240
241 // Set the correct calling convention for ARMv7k WatchOS. It's just
242 // AAPCS_VFP for functions as simple as libcalls.
243 if (Subtarget->isTargetWatchOS()) {
244 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i)
245 setLibcallCallingConv((RTLIB::Libcall)i, CallingConv::ARM_AAPCS_VFP);
246 }
Evan Cheng10043e22007-01-19 07:51:42 +0000247 }
248
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000249 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000250 setLibcallName(RTLIB::SHL_I128, nullptr);
251 setLibcallName(RTLIB::SRL_I128, nullptr);
252 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000253
Renato Golin6d435f12015-11-09 12:40:30 +0000254 // RTLIB
255 if (Subtarget->isAAPCS_ABI() &&
256 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
257 Subtarget->isTargetAndroid())) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000258 static const struct {
259 const RTLIB::Libcall Op;
260 const char * const Name;
261 const CallingConv::ID CC;
262 const ISD::CondCode Cond;
263 } LibraryCalls[] = {
264 // Double-precision floating-point arithmetic helper functions
265 // RTABI chapter 4.1.2, Table 2
266 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
267 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
268 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
269 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000270
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000271 // Double-precision floating-point comparison helper functions
272 // RTABI chapter 4.1.2, Table 3
273 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
275 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
276 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
277 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
278 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
279 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
280 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000281
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000282 // Single-precision floating-point arithmetic helper functions
283 // RTABI chapter 4.1.2, Table 4
284 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
285 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
286 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
287 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000288
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000289 // Single-precision floating-point comparison helper functions
290 // RTABI chapter 4.1.2, Table 5
291 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
293 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
294 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
295 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
296 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
297 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
298 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000299
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000300 // Floating-point to integer conversions.
301 // RTABI chapter 4.1.2, Table 6
302 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
307 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
308 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
309 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000310
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000311 // Conversions between floating types.
312 // RTABI chapter 4.1.2, Table 7
313 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000314 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000315 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000316
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000317 // Integer to floating-point conversions.
318 // RTABI chapter 4.1.2, Table 8
319 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000327
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000328 // Long long helper functions
329 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000330 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
331 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000334
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000335 // Integer division functions
336 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000337 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000345 };
346
347 for (const auto &LC : LibraryCalls) {
348 setLibcallName(LC.Op, LC.Name);
349 setLibcallCallingConv(LC.Op, LC.CC);
350 if (LC.Cond != ISD::SETCC_INVALID)
351 setCmpLibcallCC(LC.Op, LC.Cond);
352 }
Renato Golin6d435f12015-11-09 12:40:30 +0000353
354 // EABI dependent RTLIB
355 if (TM.Options.EABIVersion == EABI::EABI4 ||
356 TM.Options.EABIVersion == EABI::EABI5) {
357 static const struct {
358 const RTLIB::Libcall Op;
359 const char *const Name;
360 const CallingConv::ID CC;
361 const ISD::CondCode Cond;
362 } MemOpsLibraryCalls[] = {
363 // Memory operations
364 // RTABI chapter 4.3.4
365 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
366 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
367 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
368 };
369
370 for (const auto &LC : MemOpsLibraryCalls) {
371 setLibcallName(LC.Op, LC.Name);
372 setLibcallCallingConv(LC.Op, LC.CC);
373 if (LC.Cond != ISD::SETCC_INVALID)
374 setCmpLibcallCC(LC.Op, LC.Cond);
375 }
376 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000377 }
378
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000379 if (Subtarget->isTargetWindows()) {
380 static const struct {
381 const RTLIB::Libcall Op;
382 const char * const Name;
383 const CallingConv::ID CC;
384 } LibraryCalls[] = {
385 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
386 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
387 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
388 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
389 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
390 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
391 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
392 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
Martell Malonea6b867e2015-11-23 13:11:39 +0000393 { RTLIB::SDIV_I32, "__rt_sdiv", CallingConv::ARM_AAPCS_VFP },
Martell Maloned1229242015-11-26 15:34:03 +0000394 { RTLIB::UDIV_I32, "__rt_udiv", CallingConv::ARM_AAPCS_VFP },
Martell Malonea6b867e2015-11-23 13:11:39 +0000395 { RTLIB::SDIV_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS_VFP },
Martell Maloned1229242015-11-26 15:34:03 +0000396 { RTLIB::UDIV_I64, "__rt_udiv64", CallingConv::ARM_AAPCS_VFP },
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000397 };
398
399 for (const auto &LC : LibraryCalls) {
400 setLibcallName(LC.Op, LC.Name);
401 setLibcallCallingConv(LC.Op, LC.CC);
402 }
403 }
404
Bob Wilsonbc158992011-10-07 16:59:21 +0000405 // Use divmod compiler-rt calls for iOS 5.0 and later.
Tim Northover8b403662015-10-28 22:51:16 +0000406 if (Subtarget->isTargetWatchOS() ||
407 (Subtarget->isTargetIOS() &&
408 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
Bob Wilsonbc158992011-10-07 16:59:21 +0000409 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
410 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
411 }
412
Oliver Stannard11790b22014-08-11 09:12:32 +0000413 // The half <-> float conversion functions are always soft-float, but are
414 // needed for some targets which use a hard-float calling convention by
415 // default.
416 if (Subtarget->isAAPCS_ABI()) {
417 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
418 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
419 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
420 } else {
421 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
422 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
423 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
424 }
425
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000426 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
427 // a __gnu_ prefix (which is the default).
428 if (Subtarget->isTargetAEABI()) {
429 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
430 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
431 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
432 }
433
David Goodwin22c2fba2009-07-08 23:10:31 +0000434 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000435 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000436 else
Craig Topperc7242e02012-04-20 07:30:17 +0000437 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000438 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000439 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000440 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000441 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000442 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000443
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000444 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000445 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000446 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000447 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
448 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
449 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
450 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000451
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000452 setOperationAction(ISD::MULHS, VT, Expand);
453 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
454 setOperationAction(ISD::MULHU, VT, Expand);
455 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000456
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000457 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000458 }
459
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000460 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000461 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000462
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000463 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
464 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
465
Bob Wilson2e076c42009-06-22 23:27:02 +0000466 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000467 addDRTypeForNEON(MVT::v2f32);
468 addDRTypeForNEON(MVT::v8i8);
469 addDRTypeForNEON(MVT::v4i16);
470 addDRTypeForNEON(MVT::v2i32);
471 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000472
Owen Anderson9f944592009-08-11 20:47:22 +0000473 addQRTypeForNEON(MVT::v4f32);
474 addQRTypeForNEON(MVT::v2f64);
475 addQRTypeForNEON(MVT::v16i8);
476 addQRTypeForNEON(MVT::v8i16);
477 addQRTypeForNEON(MVT::v4i32);
478 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000479
Bob Wilson194a2512009-09-15 23:55:57 +0000480 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
481 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000482 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
483 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000484 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
485 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
486 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000487 // FIXME: Code duplication: FDIV and FREM are expanded always, see
488 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000489 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
490 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000491 // FIXME: Create unittest.
492 // In another words, find a way when "copysign" appears in DAG with vector
493 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000494 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000495 // FIXME: Code duplication: SETCC has custom operation action, see
496 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000497 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000498 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000499 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
500 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
501 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
502 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
503 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
504 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
505 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
506 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
507 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
508 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
509 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
510 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000511 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000512 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
513 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
514 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
515 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
516 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000517 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000518
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000519 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
520 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
521 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
522 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
523 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
524 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
525 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
526 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
527 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
528 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000529 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
530 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
531 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
532 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000533 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000534
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000535 // Mark v2f32 intrinsics.
536 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
537 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
538 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
539 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
540 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
541 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
542 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
543 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
544 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
545 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
546 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
547 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
548 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
549 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
550 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
551
Bob Wilson6cc46572009-09-16 00:32:15 +0000552 // Neon does not support some operations on v1i64 and v2i64 types.
553 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000554 // Custom handling for some quad-vector types to detect VMULL.
555 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
556 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
557 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000558 // Custom handling for some vector types to avoid expensive expansions
559 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
560 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
561 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
562 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000563 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
564 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000565 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000566 // a destination type that is wider than the source, and nor does
567 // it have a FP_TO_[SU]INT instruction with a narrower destination than
568 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000569 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
570 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000571 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
572 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000573
Eli Friedmane6385e62012-11-15 22:44:27 +0000574 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000575 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000576
Evan Chengb4eae132012-12-04 22:41:50 +0000577 // NEON does not have single instruction CTPOP for vectors with element
578 // types wider than 8-bits. However, custom lowering can leverage the
579 // v8i8/v16i8 vcnt instruction.
580 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
581 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
582 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
583 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
584
Logan Chien0a43abc2015-07-13 15:37:30 +0000585 // NEON does not have single instruction CTTZ for vectors.
586 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
587 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
588 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
589 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
590
591 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
592 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
593 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
594 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
595
596 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
597 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
598 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
599 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
600
601 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
602 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
603 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
604 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
605
Jim Grosbach5f215872013-02-27 21:31:12 +0000606 // NEON only has FMA instructions as of VFP4.
607 if (!Subtarget->hasVFP4()) {
608 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
609 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
610 }
611
Bob Wilson06fce872011-02-07 17:43:21 +0000612 setTargetDAGCombine(ISD::INTRINSIC_VOID);
613 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000614 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
615 setTargetDAGCombine(ISD::SHL);
616 setTargetDAGCombine(ISD::SRL);
617 setTargetDAGCombine(ISD::SRA);
618 setTargetDAGCombine(ISD::SIGN_EXTEND);
619 setTargetDAGCombine(ISD::ZERO_EXTEND);
620 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000621 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000622 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000623 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
624 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000625 setTargetDAGCombine(ISD::FP_TO_SINT);
626 setTargetDAGCombine(ISD::FP_TO_UINT);
627 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000628 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000629
James Molloy547d4c02012-02-20 09:24:05 +0000630 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000631 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
632 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000633 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000634 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
635 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
636 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000637 }
James Molloy547d4c02012-02-20 09:24:05 +0000638 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000639 }
640
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000641 // ARM and Thumb2 support UMLAL/SMLAL.
642 if (!Subtarget->isThumb1Only())
643 setTargetDAGCombine(ISD::ADDC);
644
Oliver Stannard51b1d462014-08-21 12:50:31 +0000645 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000646 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000647 // operations, f64 is legal for the few double-precision instructions which
648 // are present However, no double-precision operations other than moves,
649 // loads and stores are provided by the hardware.
650 setOperationAction(ISD::FADD, MVT::f64, Expand);
651 setOperationAction(ISD::FSUB, MVT::f64, Expand);
652 setOperationAction(ISD::FMUL, MVT::f64, Expand);
653 setOperationAction(ISD::FMA, MVT::f64, Expand);
654 setOperationAction(ISD::FDIV, MVT::f64, Expand);
655 setOperationAction(ISD::FREM, MVT::f64, Expand);
656 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
657 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
658 setOperationAction(ISD::FNEG, MVT::f64, Expand);
659 setOperationAction(ISD::FABS, MVT::f64, Expand);
660 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
661 setOperationAction(ISD::FSIN, MVT::f64, Expand);
662 setOperationAction(ISD::FCOS, MVT::f64, Expand);
663 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
664 setOperationAction(ISD::FPOW, MVT::f64, Expand);
665 setOperationAction(ISD::FLOG, MVT::f64, Expand);
666 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
667 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
668 setOperationAction(ISD::FEXP, MVT::f64, Expand);
669 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
670 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
671 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
672 setOperationAction(ISD::FRINT, MVT::f64, Expand);
673 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
674 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000675 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
676 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
677 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
678 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
679 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
680 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000681 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
682 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
683 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000684
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000685 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000686
Tim Northover4e80b582014-07-18 13:01:19 +0000687 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000688 for (MVT VT : MVT::fp_valuetypes()) {
689 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
690 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
691 }
Tim Northover4e80b582014-07-18 13:01:19 +0000692
693 // ... or truncating stores
694 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
695 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
696 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000697
Duncan Sands95d46ef2008-01-23 20:39:46 +0000698 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000699 for (MVT VT : MVT::integer_valuetypes())
700 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000701
Evan Cheng10043e22007-01-19 07:51:42 +0000702 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000703 if (!Subtarget->isThumb1Only()) {
704 for (unsigned im = (unsigned)ISD::PRE_INC;
705 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000706 setIndexedLoadAction(im, MVT::i1, Legal);
707 setIndexedLoadAction(im, MVT::i8, Legal);
708 setIndexedLoadAction(im, MVT::i16, Legal);
709 setIndexedLoadAction(im, MVT::i32, Legal);
710 setIndexedStoreAction(im, MVT::i1, Legal);
711 setIndexedStoreAction(im, MVT::i8, Legal);
712 setIndexedStoreAction(im, MVT::i16, Legal);
713 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000714 }
Evan Cheng10043e22007-01-19 07:51:42 +0000715 }
716
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000717 setOperationAction(ISD::SADDO, MVT::i32, Custom);
718 setOperationAction(ISD::UADDO, MVT::i32, Custom);
719 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
720 setOperationAction(ISD::USUBO, MVT::i32, Custom);
721
Evan Cheng10043e22007-01-19 07:51:42 +0000722 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000723 setOperationAction(ISD::MUL, MVT::i64, Expand);
724 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000725 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000726 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
727 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000728 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000729 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000730 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000731 setOperationAction(ISD::MULHS, MVT::i32, Expand);
732
Jim Grosbach5d994042009-10-31 19:38:01 +0000733 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000734 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000735 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000736 setOperationAction(ISD::SRL, MVT::i64, Custom);
737 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000738
Evan Chenge8916542011-08-30 01:34:54 +0000739 if (!Subtarget->isThumb1Only()) {
740 // FIXME: We should do this for Thumb1 as well.
741 setOperationAction(ISD::ADDC, MVT::i32, Custom);
742 setOperationAction(ISD::ADDE, MVT::i32, Custom);
743 setOperationAction(ISD::SUBC, MVT::i32, Custom);
744 setOperationAction(ISD::SUBE, MVT::i32, Custom);
745 }
746
Weiming Zhao4b3b13d2016-01-08 18:43:41 +0000747 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
James Molloyb5640982015-11-13 16:05:22 +0000748 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
749
Evan Cheng10043e22007-01-19 07:51:42 +0000750 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000751 setOperationAction(ISD::ROTL, MVT::i32, Expand);
752 for (MVT VT : MVT::vector_valuetypes()) {
753 setOperationAction(ISD::ROTL, VT, Expand);
754 setOperationAction(ISD::ROTR, VT, Expand);
755 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000756 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000757 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000758 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000759 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000760
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000761 // These just redirect to CTTZ and CTLZ on ARM.
762 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
763 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
764
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000765 // @llvm.readcyclecounter requires the Performance Monitors extension.
766 // Default to the 0 expansion on unsupported platforms.
767 // FIXME: Technically there are older ARM CPUs that have
768 // implementation-specific ways of obtaining this information.
769 if (Subtarget->hasPerfMon())
770 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000771
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000772 // Only ARMv6 has BSWAP.
773 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000774 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000775
Bob Wilsone8a549c2012-09-29 21:43:49 +0000776 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
777 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
778 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000779 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
780 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000781 }
Renato Golin87610692013-07-16 09:32:17 +0000782
Chad Rosierad7c9102014-08-23 18:29:43 +0000783 setOperationAction(ISD::SREM, MVT::i32, Expand);
784 setOperationAction(ISD::UREM, MVT::i32, Expand);
785 // Register based DivRem for AEABI (RTABI 4.2)
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +0000786 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000787 setOperationAction(ISD::SREM, MVT::i64, Custom);
788 setOperationAction(ISD::UREM, MVT::i64, Custom);
789
Chad Rosierad7c9102014-08-23 18:29:43 +0000790 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
791 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
792 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
793 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
794 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
795 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
796 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
797 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
798
799 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
800 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
801 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
802 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
803 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
804 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
805 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
806 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
807
808 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
809 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
810 } else {
Renato Golin87610692013-07-16 09:32:17 +0000811 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
812 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
813 }
Bob Wilson7117a912009-03-20 22:42:55 +0000814
Owen Anderson9f944592009-08-11 20:47:22 +0000815 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
816 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000817 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000818 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000819
Evan Cheng74d92c12011-04-08 21:37:21 +0000820 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000821
Evan Cheng10043e22007-01-19 07:51:42 +0000822 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000823 setOperationAction(ISD::VASTART, MVT::Other, Custom);
824 setOperationAction(ISD::VAARG, MVT::Other, Expand);
825 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
826 setOperationAction(ISD::VAEND, MVT::Other, Expand);
827 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
828 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000829
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000830 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
831 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
832 else
833 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
834
Evan Cheng6e809de2010-08-11 06:22:01 +0000835 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000836 // the default expansion. If we are targeting a single threaded system,
837 // then set them all for expand so we can lower them later into their
838 // non-atomic form.
839 if (TM.Options.ThreadModel == ThreadModel::Single)
840 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
841 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000842 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
843 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000844 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000845
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000846 // On v8, we have particularly efficient implementations of atomic fences
847 // if they can be combined with nearby atomic loads and stores.
848 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000849 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000850 setInsertFencesForAtomic(true);
851 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000852 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000853 // If there's anything we can use as a barrier, go through custom lowering
854 // for ATOMIC_FENCE.
855 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
856 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
857
Jim Grosbach6860bb72010-06-18 22:35:32 +0000858 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000859 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000860 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000861 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000862 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000863 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000864 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000865 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000866 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000867 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000868 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000869 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000870 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000871 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
872 // Unordered/Monotonic case.
873 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
874 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000875 }
Evan Cheng10043e22007-01-19 07:51:42 +0000876
Evan Cheng21acf9f2010-11-04 05:19:35 +0000877 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000878
Eli Friedman8cfa7712010-06-26 04:36:50 +0000879 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
880 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000881 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
882 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000883 }
Owen Anderson9f944592009-08-11 20:47:22 +0000884 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000885
Eric Christopher824f42f2015-05-12 01:26:05 +0000886 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000887 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000888 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000889 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000890 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000891 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
892 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000893
894 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000895 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000896 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
897 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
898 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
Tim Northoverf8e47e42015-10-28 22:56:36 +0000899 if (Subtarget->useSjLjEH())
John McCall7d84ece2011-05-29 19:50:32 +0000900 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000901
Owen Anderson9f944592009-08-11 20:47:22 +0000902 setOperationAction(ISD::SETCC, MVT::i32, Expand);
903 setOperationAction(ISD::SETCC, MVT::f32, Expand);
904 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000905 setOperationAction(ISD::SELECT, MVT::i32, Custom);
906 setOperationAction(ISD::SELECT, MVT::f32, Custom);
907 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000908 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
909 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
910 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000911
Owen Anderson9f944592009-08-11 20:47:22 +0000912 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
913 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
914 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
915 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
916 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000917
Dan Gohman482732a2007-10-11 23:21:31 +0000918 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000919 setOperationAction(ISD::FSIN, MVT::f64, Expand);
920 setOperationAction(ISD::FSIN, MVT::f32, Expand);
921 setOperationAction(ISD::FCOS, MVT::f32, Expand);
922 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000923 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
924 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000925 setOperationAction(ISD::FREM, MVT::f64, Expand);
926 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000927 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000928 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000929 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
930 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000931 }
Owen Anderson9f944592009-08-11 20:47:22 +0000932 setOperationAction(ISD::FPOW, MVT::f64, Expand);
933 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000934
Evan Chengd0007f32012-04-10 21:40:28 +0000935 if (!Subtarget->hasVFP4()) {
936 setOperationAction(ISD::FMA, MVT::f64, Expand);
937 setOperationAction(ISD::FMA, MVT::f32, Expand);
938 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000939
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000940 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000941 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000942 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
943 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000944 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
945 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
946 }
947
948 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000949 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000950 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
951 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000952 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000953 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000954
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000955 // Combine sin / cos into one node or libcall if possible.
956 if (Subtarget->hasSinCos()) {
957 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
958 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Tim Northover8b403662015-10-28 22:51:16 +0000959 if (Subtarget->isTargetWatchOS()) {
960 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP);
961 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP);
962 }
963 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000964 // For iOS, we don't want to the normal expansion of a libcall to
965 // sincos. We want to issue a libcall to __sincos_stret.
966 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
967 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
968 }
969 }
Evan Cheng10043e22007-01-19 07:51:42 +0000970
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000971 // FP-ARMv8 implements a lot of rounding-like FP operations.
972 if (Subtarget->hasFPARMv8()) {
973 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
974 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
975 setOperationAction(ISD::FROUND, MVT::f32, Legal);
976 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
977 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
978 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000979 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
980 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +0000981 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
982 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
983 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
984 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
985
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000986 if (!Subtarget->isFPOnlySP()) {
987 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
988 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
989 setOperationAction(ISD::FROUND, MVT::f64, Legal);
990 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
991 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
992 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000993 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
994 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000995 }
996 }
James Molloydb8ee4b2015-08-11 12:06:15 +0000997
James Molloy974838f2015-08-17 19:37:12 +0000998 if (Subtarget->hasNEON()) {
999 // vmin and vmax aren't available in a scalar form, so we use
1000 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +00001001 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
1002 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +00001003 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
1004 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
1005 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
1006 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
1007 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001008
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00001009 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001010 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +00001011 setTargetDAGCombine(ISD::ADD);
1012 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00001013 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +00001014 setTargetDAGCombine(ISD::AND);
1015 setTargetDAGCombine(ISD::OR);
1016 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +00001017
Evan Chengf258a152012-02-23 02:58:19 +00001018 if (Subtarget->hasV6Ops())
1019 setTargetDAGCombine(ISD::SRL);
1020
Evan Cheng10043e22007-01-19 07:51:42 +00001021 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001022
Eric Christopher824f42f2015-05-12 01:26:05 +00001023 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001024 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001025 setSchedulingPreference(Sched::RegPressure);
1026 else
1027 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001028
Evan Cheng3ae2b792011-01-06 06:52:41 +00001029 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001030 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001031 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001032 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001033 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001034 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001035 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001036
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001037 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1038 // are at least 4 bytes aligned.
1039 setMinStackArgumentAlignment(4);
1040
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001041 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001042 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001043
Eli Friedman2518f832011-05-06 20:34:06 +00001044 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001045}
1046
Eric Christopher824f42f2015-05-12 01:26:05 +00001047bool ARMTargetLowering::useSoftFloat() const {
1048 return Subtarget->useSoftFloat();
1049}
1050
Andrew Trick43f25632011-01-19 02:35:27 +00001051// FIXME: It might make sense to define the representative register class as the
1052// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1053// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1054// SPR's representative would be DPR_VFP2. This should work well if register
1055// pressure tracking were modified such that a register use would increment the
1056// pressure of the register class's representative and all of it's super
1057// classes' representatives transitively. We have not implemented this because
1058// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001059// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001060// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001061std::pair<const TargetRegisterClass *, uint8_t>
1062ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1063 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001064 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001065 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001066 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001067 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001068 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001069 // Use DPR as representative register class for all floating point
1070 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1071 // the cost is 1 for both f32 and f64.
1072 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001073 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001074 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001075 // When NEON is used for SP, only half of the register file is available
1076 // because operations that define both SP and DP results will be constrained
1077 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1078 // coalescing by double-counting the SP regs. See the FIXME above.
1079 if (Subtarget->useNEONForSinglePrecisionFP())
1080 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001081 break;
1082 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1083 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001084 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001085 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001086 break;
1087 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001088 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001089 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001090 break;
1091 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001092 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001093 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001094 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001095 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001096 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001097}
1098
Evan Cheng10043e22007-01-19 07:51:42 +00001099const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001100 switch ((ARMISD::NodeType)Opcode) {
1101 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001102 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001103 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001104 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001105 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001106 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001107 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001108 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1109 case ARMISD::tCALL: return "ARMISD::tCALL";
1110 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1111 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001112 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001113 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001114 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001115 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1116 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001117 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001118 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001119 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1120 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001121 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001122 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001123
Evan Cheng10043e22007-01-19 07:51:42 +00001124 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001125
Evan Cheng10043e22007-01-19 07:51:42 +00001126 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1127 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1128 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001129
Evan Chenge8916542011-08-30 01:34:54 +00001130 case ARMISD::ADDC: return "ARMISD::ADDC";
1131 case ARMISD::ADDE: return "ARMISD::ADDE";
1132 case ARMISD::SUBC: return "ARMISD::SUBC";
1133 case ARMISD::SUBE: return "ARMISD::SUBE";
1134
Bob Wilson22806742010-09-22 22:09:21 +00001135 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1136 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001137
Evan Chengec6d7c92009-10-28 06:55:03 +00001138 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001139 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1140 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001141
Dale Johannesend679ff72010-06-03 21:09:53 +00001142 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001143
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001144 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001145
Evan Chengb972e562009-08-07 00:34:42 +00001146 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1147
Bob Wilson7ed59712010-10-30 00:54:37 +00001148 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001149
Evan Cheng8740ee32010-11-03 06:34:55 +00001150 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1151
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001152 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001153 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001154
Bob Wilson2e076c42009-06-22 23:27:02 +00001155 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001156 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001157 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001158 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1159 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001160 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1161 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001162 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1163 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001164 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1165 case ARMISD::VTST: return "ARMISD::VTST";
1166
1167 case ARMISD::VSHL: return "ARMISD::VSHL";
1168 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1169 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001170 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1171 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1172 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1173 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1174 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1175 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1176 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1177 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1178 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1179 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1180 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1181 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001182 case ARMISD::VSLI: return "ARMISD::VSLI";
1183 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001184 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1185 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001186 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001187 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001188 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001189 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001190 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001191 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001192 case ARMISD::VREV64: return "ARMISD::VREV64";
1193 case ARMISD::VREV32: return "ARMISD::VREV32";
1194 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001195 case ARMISD::VZIP: return "ARMISD::VZIP";
1196 case ARMISD::VUZP: return "ARMISD::VUZP";
1197 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001198 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1199 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001200 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1201 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001202 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1203 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001204 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001205 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001206 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1207 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001208 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001209 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001210 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1211 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1212 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001213 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1214 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1215 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1216 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1217 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1218 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1219 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1220 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1221 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1222 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1223 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1224 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1225 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1226 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1227 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1228 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1229 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001230 }
Matthias Braund04893f2015-05-07 21:33:59 +00001231 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001232}
1233
Mehdi Amini44ede332015-07-09 02:09:04 +00001234EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1235 EVT VT) const {
1236 if (!VT.isVector())
1237 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001238 return VT.changeVectorElementTypeToInteger();
1239}
1240
Evan Cheng4cad68e2010-05-15 02:18:07 +00001241/// getRegClassFor - Return the register class that should be used for the
1242/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001243const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001244 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1245 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1246 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001247 if (Subtarget->hasNEON()) {
1248 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001249 return &ARM::QQPRRegClass;
1250 if (VT == MVT::v8i64)
1251 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001252 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001253 return TargetLowering::getRegClassFor(VT);
1254}
1255
John Brawn0dbcd652015-03-18 12:01:59 +00001256// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1257// source/dest is aligned and the copy size is large enough. We therefore want
1258// to align such objects passed to memory intrinsics.
1259bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1260 unsigned &PrefAlign) const {
1261 if (!isa<MemIntrinsic>(CI))
1262 return false;
1263 MinSize = 8;
1264 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1265 // cycle faster than 4-byte aligned LDM.
1266 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1267 return true;
1268}
1269
Eric Christopher84bdfd82010-07-21 22:26:11 +00001270// Create a fast isel object.
1271FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001272ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1273 const TargetLibraryInfo *libInfo) const {
1274 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001275}
1276
Evan Cheng4401f882010-05-20 23:26:43 +00001277Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001278 unsigned NumVals = N->getNumValues();
1279 if (!NumVals)
1280 return Sched::RegPressure;
1281
1282 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001283 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001284 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001285 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001286 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001287 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001288 }
Evan Chengbf914992010-05-28 23:25:23 +00001289
1290 if (!N->isMachineOpcode())
1291 return Sched::RegPressure;
1292
1293 // Load are scheduled for latency even if there instruction itinerary
1294 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001295 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001296 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001297
Evan Cheng6cc775f2011-06-28 19:10:37 +00001298 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001299 return Sched::RegPressure;
1300 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001301 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001302 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001303
Evan Cheng4401f882010-05-20 23:26:43 +00001304 return Sched::RegPressure;
1305}
1306
Evan Cheng10043e22007-01-19 07:51:42 +00001307//===----------------------------------------------------------------------===//
1308// Lowering Code
1309//===----------------------------------------------------------------------===//
1310
Evan Cheng10043e22007-01-19 07:51:42 +00001311/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1312static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1313 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001314 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001315 case ISD::SETNE: return ARMCC::NE;
1316 case ISD::SETEQ: return ARMCC::EQ;
1317 case ISD::SETGT: return ARMCC::GT;
1318 case ISD::SETGE: return ARMCC::GE;
1319 case ISD::SETLT: return ARMCC::LT;
1320 case ISD::SETLE: return ARMCC::LE;
1321 case ISD::SETUGT: return ARMCC::HI;
1322 case ISD::SETUGE: return ARMCC::HS;
1323 case ISD::SETULT: return ARMCC::LO;
1324 case ISD::SETULE: return ARMCC::LS;
1325 }
1326}
1327
Bob Wilsona2e83332009-09-09 23:14:54 +00001328/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1329static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001330 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001331 CondCode2 = ARMCC::AL;
1332 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001333 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001334 case ISD::SETEQ:
1335 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1336 case ISD::SETGT:
1337 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1338 case ISD::SETGE:
1339 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1340 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001341 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001342 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1343 case ISD::SETO: CondCode = ARMCC::VC; break;
1344 case ISD::SETUO: CondCode = ARMCC::VS; break;
1345 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1346 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1347 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1348 case ISD::SETLT:
1349 case ISD::SETULT: CondCode = ARMCC::LT; break;
1350 case ISD::SETLE:
1351 case ISD::SETULE: CondCode = ARMCC::LE; break;
1352 case ISD::SETNE:
1353 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1354 }
Evan Cheng10043e22007-01-19 07:51:42 +00001355}
1356
Bob Wilsona4c22902009-04-17 19:07:39 +00001357//===----------------------------------------------------------------------===//
1358// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001359//===----------------------------------------------------------------------===//
1360
1361#include "ARMGenCallingConv.inc"
1362
Oliver Stannardc24f2172014-05-09 14:01:47 +00001363/// getEffectiveCallingConv - Get the effective calling convention, taking into
1364/// account presence of floating point hardware and calling convention
1365/// limitations, such as support for variadic functions.
1366CallingConv::ID
1367ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1368 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001369 switch (CC) {
1370 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001371 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001372 case CallingConv::ARM_AAPCS:
1373 case CallingConv::ARM_APCS:
1374 case CallingConv::GHC:
1375 return CC;
1376 case CallingConv::ARM_AAPCS_VFP:
1377 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1378 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001379 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001380 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001381 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001382 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1383 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001384 return CallingConv::ARM_AAPCS_VFP;
1385 else
1386 return CallingConv::ARM_AAPCS;
1387 case CallingConv::Fast:
Manman Ren16026052016-01-11 23:50:43 +00001388 case CallingConv::CXX_FAST_TLS:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001389 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001390 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001391 return CallingConv::Fast;
1392 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001393 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001394 return CallingConv::ARM_AAPCS_VFP;
1395 else
1396 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001397 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001398}
1399
1400/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1401/// CallingConvention.
1402CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1403 bool Return,
1404 bool isVarArg) const {
1405 switch (getEffectiveCallingConv(CC, isVarArg)) {
1406 default:
1407 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001408 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001409 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001410 case CallingConv::ARM_AAPCS:
1411 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1412 case CallingConv::ARM_AAPCS_VFP:
1413 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1414 case CallingConv::Fast:
1415 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001416 case CallingConv::GHC:
1417 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001418 }
1419}
1420
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001421/// LowerCallResult - Lower the result values of a call into the
1422/// appropriate copies out of appropriate physical registers.
1423SDValue
1424ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001425 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001426 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001427 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001428 SmallVectorImpl<SDValue> &InVals,
1429 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001430
Bob Wilsona4c22902009-04-17 19:07:39 +00001431 // Assign locations to each value returned by this call.
1432 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001433 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1434 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001435 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001436 CCAssignFnForNode(CallConv, /* Return*/ true,
1437 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001438
1439 // Copy all of the result registers out of their specified physreg.
1440 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1441 CCValAssign VA = RVLocs[i];
1442
Stephen Linb8bd2322013-04-20 05:14:40 +00001443 // Pass 'this' value directly from the argument to return value, to avoid
1444 // reg unit interference
1445 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001446 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1447 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001448 InVals.push_back(ThisVal);
1449 continue;
1450 }
1451
Bob Wilson0041bd32009-04-25 00:33:20 +00001452 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001453 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001454 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001455 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001456 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001457 Chain = Lo.getValue(1);
1458 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001459 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001460 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001461 InFlag);
1462 Chain = Hi.getValue(1);
1463 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001464 if (!Subtarget->isLittle())
1465 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001466 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001467
Owen Anderson9f944592009-08-11 20:47:22 +00001468 if (VA.getLocVT() == MVT::v2f64) {
1469 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1470 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001471 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001472
1473 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001474 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001475 Chain = Lo.getValue(1);
1476 InFlag = Lo.getValue(2);
1477 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001478 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001479 Chain = Hi.getValue(1);
1480 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001481 if (!Subtarget->isLittle())
1482 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001483 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001484 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001485 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001486 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001487 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001488 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1489 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001490 Chain = Val.getValue(1);
1491 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001492 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001493
1494 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001495 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001496 case CCValAssign::Full: break;
1497 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001498 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001499 break;
1500 }
1501
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001502 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001503 }
1504
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001505 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001506}
1507
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001508/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001509SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001510ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1511 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001512 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001513 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001514 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001515 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001516 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001517 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1518 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001519 return DAG.getStore(
1520 Chain, dl, Arg, PtrOff,
1521 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1522 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001523}
1524
Andrew Trickef9de2a2013-05-25 02:42:55 +00001525void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001526 SDValue Chain, SDValue &Arg,
1527 RegsToPassVector &RegsToPass,
1528 CCValAssign &VA, CCValAssign &NextVA,
1529 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001530 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001531 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001532
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001533 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001534 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001535 unsigned id = Subtarget->isLittle() ? 0 : 1;
1536 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001537
1538 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001539 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001540 else {
1541 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001542 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001543 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1544 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001545
Christian Pirkerb5728192014-05-08 14:06:24 +00001546 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001547 dl, DAG, NextVA,
1548 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001549 }
1550}
1551
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001552/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001553/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1554/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001555SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001556ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001557 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001558 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001559 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001560 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1561 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1562 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001563 SDValue Chain = CLI.Chain;
1564 SDValue Callee = CLI.Callee;
1565 bool &isTailCall = CLI.IsTailCall;
1566 CallingConv::ID CallConv = CLI.CallConv;
1567 bool doesNotRet = CLI.DoesNotReturn;
1568 bool isVarArg = CLI.IsVarArg;
1569
Dale Johannesend679ff72010-06-03 21:09:53 +00001570 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001571 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1572 bool isThisReturn = false;
1573 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001574 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001575
Bob Wilson8decdc42011-10-07 17:17:49 +00001576 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001577 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001578 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001579
Dale Johannesend679ff72010-06-03 21:09:53 +00001580 if (isTailCall) {
1581 // Check if it's really possible to do a tail call.
1582 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001583 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001584 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001585 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1586 report_fatal_error("failed to perform tail call elimination on a call "
1587 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001588 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1589 // detected sibcalls.
1590 if (isTailCall) {
1591 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001592 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001593 }
1594 }
Evan Cheng10043e22007-01-19 07:51:42 +00001595
Bob Wilsona4c22902009-04-17 19:07:39 +00001596 // Analyze operands of the call, assigning locations to each operand.
1597 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001598 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1599 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001600 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001601 CCAssignFnForNode(CallConv, /* Return*/ false,
1602 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001603
Bob Wilsona4c22902009-04-17 19:07:39 +00001604 // Get a count of how many bytes are to be pushed on the stack.
1605 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001606
Dale Johannesend679ff72010-06-03 21:09:53 +00001607 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001608 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001609 NumBytes = 0;
1610
Evan Cheng10043e22007-01-19 07:51:42 +00001611 // Adjust the stack pointer for the new arguments...
1612 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001613 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001614 Chain = DAG.getCALLSEQ_START(Chain,
1615 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001616
Mehdi Amini44ede332015-07-09 02:09:04 +00001617 SDValue StackPtr =
1618 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001619
Bob Wilson2e076c42009-06-22 23:27:02 +00001620 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001621 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001622
Bob Wilsona4c22902009-04-17 19:07:39 +00001623 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001624 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001625 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1626 i != e;
1627 ++i, ++realArgIdx) {
1628 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001629 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001630 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001631 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001632
Bob Wilsona4c22902009-04-17 19:07:39 +00001633 // Promote the value if needed.
1634 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001635 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001636 case CCValAssign::Full: break;
1637 case CCValAssign::SExt:
1638 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1639 break;
1640 case CCValAssign::ZExt:
1641 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1642 break;
1643 case CCValAssign::AExt:
1644 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1645 break;
1646 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001647 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001648 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001649 }
1650
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001651 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001652 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001653 if (VA.getLocVT() == MVT::v2f64) {
1654 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001655 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001656 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001657 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001658
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001659 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001660 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1661
1662 VA = ArgLocs[++i]; // skip ahead to next loc
1663 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001664 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001665 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1666 } else {
1667 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001668
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001669 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1670 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001671 }
1672 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001673 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001674 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001675 }
1676 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001677 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1678 assert(VA.getLocVT() == MVT::i32 &&
1679 "unexpected calling convention register assignment");
1680 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001681 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001682 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001683 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001684 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001685 } else if (isByVal) {
1686 assert(VA.isMemLoc());
1687 unsigned offset = 0;
1688
1689 // True if this byval aggregate will be split between registers
1690 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001691 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001692 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001693
1694 if (CurByValIdx < ByValArgsCount) {
1695
1696 unsigned RegBegin, RegEnd;
1697 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1698
Mehdi Amini44ede332015-07-09 02:09:04 +00001699 EVT PtrVT =
1700 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001701 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001702 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001703 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001704 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1705 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1706 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001707 false, false, false,
1708 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001709 MemOpChains.push_back(Load.getValue(1));
1710 RegsToPass.push_back(std::make_pair(j, Load));
1711 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001712
1713 // If parameter size outsides register area, "offset" value
1714 // helps us to calculate stack slot for remained part properly.
1715 offset = RegEnd - RegBegin;
1716
1717 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001718 }
1719
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001720 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001721 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001722 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001723 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001724 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001725 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001726 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001727 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001728 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001729 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1730 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001731
Manman Ren9f911162012-06-01 02:44:42 +00001732 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001733 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001734 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001735 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001736 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001737 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001738 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001739
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001740 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1741 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001742 }
Evan Cheng10043e22007-01-19 07:51:42 +00001743 }
1744
1745 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001746 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001747
1748 // Build a sequence of copy-to-reg nodes chained together with token chain
1749 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001750 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001751 // Tail call byval lowering might overwrite argument registers so in case of
1752 // tail call optimization the copies to registers are lowered later.
1753 if (!isTailCall)
1754 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1755 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1756 RegsToPass[i].second, InFlag);
1757 InFlag = Chain.getValue(1);
1758 }
Evan Cheng10043e22007-01-19 07:51:42 +00001759
Dale Johannesend679ff72010-06-03 21:09:53 +00001760 // For tail calls lower the arguments to the 'real' stack slot.
1761 if (isTailCall) {
1762 // Force all the incoming stack arguments to be loaded from the stack
1763 // before any new outgoing arguments are stored to the stack, because the
1764 // outgoing stack slots may alias the incoming argument stack slots, and
1765 // the alias isn't otherwise explicit. This is slightly more conservative
1766 // than necessary, because it means that each store effectively depends
1767 // on every argument instead of just those arguments it would clobber.
1768
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001769 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001770 InFlag = SDValue();
1771 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1772 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1773 RegsToPass[i].second, InFlag);
1774 InFlag = Chain.getValue(1);
1775 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001776 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001777 }
1778
Bill Wendling24c79f22008-09-16 21:48:12 +00001779 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1780 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1781 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001782 bool isDirect = false;
1783 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001784 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001785 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001786 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001787
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001788 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001789 assert((Subtarget->isTargetWindows() ||
1790 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1791 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001792 // Handle a global address or an external symbol. If it's not one of
1793 // those, the target's already in a register, so we don't need to do
1794 // anything extra.
1795 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001796 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001797 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001798 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001799 ARMConstantPoolValue *CPV =
1800 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1801
Jim Grosbach32bb3622010-04-14 22:28:31 +00001802 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001803 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001804 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001805 Callee = DAG.getLoad(
1806 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1807 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1808 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001809 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1810 const char *Sym = S->getSymbol();
1811
1812 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001813 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001814 ARMConstantPoolValue *CPV =
1815 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1816 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001817 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001818 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001819 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001820 Callee = DAG.getLoad(
1821 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1822 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1823 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001824 }
1825 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001826 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001827 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001828 bool isDef = GV->isStrongDefinitionForLinker();
1829 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001830 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001831 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001832 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001833 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001834 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001835 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001836 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001837 Callee = DAG.getNode(
1838 ARMISD::WrapperPIC, dl, PtrVt,
1839 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1840 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001841 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1842 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001843 } else if (Subtarget->isTargetCOFF()) {
1844 assert(Subtarget->isTargetWindows() &&
1845 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001846 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1847 ? ARMII::MO_DLLIMPORT
1848 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001849 Callee =
1850 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001851 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001852 Callee =
1853 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1854 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001855 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1856 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001857 } else {
1858 // On ELF targets for PIC code, direct calls should go through the PLT
1859 unsigned OpFlags = 0;
1860 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001861 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001862 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001863 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001864 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001865 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001866 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001867 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001868 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001869 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001870 // tBX takes a register source operand.
1871 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001872 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001873 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001874 ARMConstantPoolValue *CPV =
1875 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1876 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001877 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001878 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001879 Callee = DAG.getLoad(
1880 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1881 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1882 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001883 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001884 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001885 } else {
1886 unsigned OpFlags = 0;
1887 // On ELF targets for PIC code, direct calls should go through the PLT
1888 if (Subtarget->isTargetELF() &&
1889 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1890 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001891 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001892 }
Evan Cheng10043e22007-01-19 07:51:42 +00001893 }
1894
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001895 // FIXME: handle tail calls differently.
1896 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001897 if (Subtarget->isThumb()) {
1898 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001899 CallOpc = ARMISD::CALL_NOLINK;
1900 else
1901 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1902 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001903 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001904 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001905 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001906 // Emit regular call when code size is the priority
1907 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001908 // "mov lr, pc; b _foo" to avoid confusing the RSP
1909 CallOpc = ARMISD::CALL_NOLINK;
1910 else
1911 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001912 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001913
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001914 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001915 Ops.push_back(Chain);
1916 Ops.push_back(Callee);
1917
1918 // Add argument registers to the end of the list so that they are known live
1919 // into the call.
1920 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1921 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1922 RegsToPass[i].second.getValueType()));
1923
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001924 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001925 if (!isTailCall) {
1926 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001927 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001928 if (isThisReturn) {
1929 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001930 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001931 if (!Mask) {
1932 // Set isThisReturn to false if the calling convention is not one that
1933 // allows 'returned' to be modeled in this way, so LowerCallResult does
1934 // not try to pass 'this' straight through
1935 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001936 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001937 }
1938 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001939 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001940
Matthias Braunc22630e2013-10-04 16:52:54 +00001941 assert(Mask && "Missing call preserved mask for calling convention");
1942 Ops.push_back(DAG.getRegisterMask(Mask));
1943 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001944
Gabor Greiff304a7a2008-08-28 21:40:38 +00001945 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001946 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001947
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001948 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001949 if (isTailCall) {
1950 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001951 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001952 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001953
Duncan Sands739a0542008-07-02 17:40:58 +00001954 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001955 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001956 InFlag = Chain.getValue(1);
1957
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001958 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1959 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001960 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001961 InFlag = Chain.getValue(1);
1962
Bob Wilsona4c22902009-04-17 19:07:39 +00001963 // Handle result values, copying them out of physregs into vregs that we
1964 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001965 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001966 InVals, isThisReturn,
1967 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001968}
1969
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001970/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001971/// on the stack. Remember the next parameter register to allocate,
1972/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001973/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001974void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1975 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001976 assert((State->getCallOrPrologue() == Prologue ||
1977 State->getCallOrPrologue() == Call) &&
1978 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001979
Tim Northover8cda34f2015-03-11 18:54:22 +00001980 // Byval (as with any stack) slots are always at least 4 byte aligned.
1981 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001982
Tim Northover8cda34f2015-03-11 18:54:22 +00001983 unsigned Reg = State->AllocateReg(GPRArgRegs);
1984 if (!Reg)
1985 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001986
Tim Northover8cda34f2015-03-11 18:54:22 +00001987 unsigned AlignInRegs = Align / 4;
1988 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1989 for (unsigned i = 0; i < Waste; ++i)
1990 Reg = State->AllocateReg(GPRArgRegs);
1991
1992 if (!Reg)
1993 return;
1994
1995 unsigned Excess = 4 * (ARM::R4 - Reg);
1996
1997 // Special case when NSAA != SP and parameter size greater than size of
1998 // all remained GPR regs. In that case we can't split parameter, we must
1999 // send it to stack. We also must set NCRN to R4, so waste all
2000 // remained registers.
2001 const unsigned NSAAOffset = State->getNextStackOffset();
2002 if (NSAAOffset != 0 && Size > Excess) {
2003 while (State->AllocateReg(GPRArgRegs))
2004 ;
2005 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002006 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002007
2008 // First register for byval parameter is the first register that wasn't
2009 // allocated before this method call, so it would be "reg".
2010 // If parameter is small enough to be saved in range [reg, r4), then
2011 // the end (first after last) register would be reg + param-size-in-regs,
2012 // else parameter would be splitted between registers and stack,
2013 // end register would be r4 in this case.
2014 unsigned ByValRegBegin = Reg;
2015 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2016 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2017 // Note, first register is allocated in the beginning of function already,
2018 // allocate remained amount of registers we need.
2019 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2020 State->AllocateReg(GPRArgRegs);
2021 // A byval parameter that is split between registers and memory needs its
2022 // size truncated here.
2023 // In the case where the entire structure fits in registers, we set the
2024 // size in memory to zero.
2025 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002026}
2027
Dale Johannesend679ff72010-06-03 21:09:53 +00002028/// MatchingStackOffset - Return true if the given stack call argument is
2029/// already available in the same position (relatively) of the caller's
2030/// incoming argument stack.
2031static
2032bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2033 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002034 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002035 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2036 int FI = INT_MAX;
2037 if (Arg.getOpcode() == ISD::CopyFromReg) {
2038 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002039 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002040 return false;
2041 MachineInstr *Def = MRI->getVRegDef(VR);
2042 if (!Def)
2043 return false;
2044 if (!Flags.isByVal()) {
2045 if (!TII->isLoadFromStackSlot(Def, FI))
2046 return false;
2047 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002048 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002049 }
2050 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2051 if (Flags.isByVal())
2052 // ByVal argument is passed in as a pointer but it's now being
2053 // dereferenced. e.g.
2054 // define @foo(%struct.X* %A) {
2055 // tail call @bar(%struct.X* byval %A)
2056 // }
2057 return false;
2058 SDValue Ptr = Ld->getBasePtr();
2059 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2060 if (!FINode)
2061 return false;
2062 FI = FINode->getIndex();
2063 } else
2064 return false;
2065
2066 assert(FI != INT_MAX);
2067 if (!MFI->isFixedObjectIndex(FI))
2068 return false;
2069 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2070}
2071
2072/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2073/// for tail call optimization. Targets which want to do tail call
2074/// optimization should implement this function.
2075bool
2076ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2077 CallingConv::ID CalleeCC,
2078 bool isVarArg,
2079 bool isCalleeStructRet,
2080 bool isCallerStructRet,
2081 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002082 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002083 const SmallVectorImpl<ISD::InputArg> &Ins,
2084 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002085 const Function *CallerF = DAG.getMachineFunction().getFunction();
2086 CallingConv::ID CallerCC = CallerF->getCallingConv();
2087 bool CCMatch = CallerCC == CalleeCC;
2088
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002089 assert(Subtarget->supportsTailCall());
2090
Dale Johannesend679ff72010-06-03 21:09:53 +00002091 // Look for obvious safe cases to perform tail call optimization that do not
2092 // require ABI changes. This is what gcc calls sibcall.
2093
Jim Grosbache3864cc2010-06-16 23:45:49 +00002094 // Do not sibcall optimize vararg calls unless the call site is not passing
2095 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002096 if (isVarArg && !Outs.empty())
2097 return false;
2098
Tim Northoverd8407452013-10-01 14:33:28 +00002099 // Exception-handling functions need a special set of instructions to indicate
2100 // a return to the hardware. Tail-calling another function would probably
2101 // break this.
2102 if (CallerF->hasFnAttribute("interrupt"))
2103 return false;
2104
Dale Johannesend679ff72010-06-03 21:09:53 +00002105 // Also avoid sibcall optimization if either caller or callee uses struct
2106 // return semantics.
2107 if (isCalleeStructRet || isCallerStructRet)
2108 return false;
2109
Oliver Stannard12993dd2014-08-18 12:42:15 +00002110 // Externally-defined functions with weak linkage should not be
2111 // tail-called on ARM when the OS does not support dynamic
2112 // pre-emption of symbols, as the AAELF spec requires normal calls
2113 // to undefined weak functions to be replaced with a NOP or jump to the
2114 // next instruction. The behaviour of branch instructions in this
2115 // situation (as used for tail calls) is implementation-defined, so we
2116 // cannot rely on the linker replacing the tail call with a return.
2117 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2118 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002119 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002120 if (GV->hasExternalWeakLinkage() &&
2121 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002122 return false;
2123 }
2124
Dale Johannesend679ff72010-06-03 21:09:53 +00002125 // If the calling conventions do not match, then we'd better make sure the
2126 // results are returned in the same way as what the caller expects.
2127 if (!CCMatch) {
2128 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002129 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2130 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002131 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2132
2133 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002134 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2135 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002136 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2137
2138 if (RVLocs1.size() != RVLocs2.size())
2139 return false;
2140 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2141 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2142 return false;
2143 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2144 return false;
2145 if (RVLocs1[i].isRegLoc()) {
2146 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2147 return false;
2148 } else {
2149 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2150 return false;
2151 }
2152 }
2153 }
2154
Manman Ren7e48b252012-10-12 23:39:43 +00002155 // If Caller's vararg or byval argument has been split between registers and
2156 // stack, do not perform tail call, since part of the argument is in caller's
2157 // local frame.
2158 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2159 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002160 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002161 return false;
2162
Dale Johannesend679ff72010-06-03 21:09:53 +00002163 // If the callee takes no arguments then go on to check the results of the
2164 // call.
2165 if (!Outs.empty()) {
2166 // Check if stack adjustment is needed. For now, do not do this if any
2167 // argument is passed on the stack.
2168 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002169 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2170 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002171 CCInfo.AnalyzeCallOperands(Outs,
2172 CCAssignFnForNode(CalleeCC, false, isVarArg));
2173 if (CCInfo.getNextStackOffset()) {
2174 MachineFunction &MF = DAG.getMachineFunction();
2175
2176 // Check if the arguments are already laid out in the right way as
2177 // the caller's fixed stack objects.
2178 MachineFrameInfo *MFI = MF.getFrameInfo();
2179 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002180 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002181 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2182 i != e;
2183 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002184 CCValAssign &VA = ArgLocs[i];
2185 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002186 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002187 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002188 if (VA.getLocInfo() == CCValAssign::Indirect)
2189 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002190 if (VA.needsCustom()) {
2191 // f64 and vector types are split into multiple registers or
2192 // register/stack-slot combinations. The types will not match
2193 // the registers; give up on memory f64 refs until we figure
2194 // out what to do about this.
2195 if (!VA.isRegLoc())
2196 return false;
2197 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002198 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002199 if (RegVT == MVT::v2f64) {
2200 if (!ArgLocs[++i].isRegLoc())
2201 return false;
2202 if (!ArgLocs[++i].isRegLoc())
2203 return false;
2204 }
2205 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002206 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2207 MFI, MRI, TII))
2208 return false;
2209 }
2210 }
2211 }
2212 }
2213
2214 return true;
2215}
2216
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002217bool
2218ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2219 MachineFunction &MF, bool isVarArg,
2220 const SmallVectorImpl<ISD::OutputArg> &Outs,
2221 LLVMContext &Context) const {
2222 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002223 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002224 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2225 isVarArg));
2226}
2227
Tim Northoverd8407452013-10-01 14:33:28 +00002228static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2229 SDLoc DL, SelectionDAG &DAG) {
2230 const MachineFunction &MF = DAG.getMachineFunction();
2231 const Function *F = MF.getFunction();
2232
2233 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2234
2235 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2236 // version of the "preferred return address". These offsets affect the return
2237 // instruction if this is a return from PL1 without hypervisor extensions.
2238 // IRQ/FIQ: +4 "subs pc, lr, #4"
2239 // SWI: 0 "subs pc, lr, #0"
2240 // ABORT: +4 "subs pc, lr, #4"
2241 // UNDEF: +4/+2 "subs pc, lr, #0"
2242 // UNDEF varies depending on where the exception came from ARM or Thumb
2243 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2244
2245 int64_t LROffset;
2246 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2247 IntKind == "ABORT")
2248 LROffset = 4;
2249 else if (IntKind == "SWI" || IntKind == "UNDEF")
2250 LROffset = 0;
2251 else
2252 report_fatal_error("Unsupported interrupt attribute. If present, value "
2253 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2254
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002255 RetOps.insert(RetOps.begin() + 1,
2256 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002257
Craig Topper48d114b2014-04-26 18:35:24 +00002258 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002259}
2260
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002261SDValue
2262ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002263 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002264 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002265 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002266 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002267
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002268 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002269 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002270
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002271 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002272 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2273 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002274
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002275 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002276 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2277 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002278
Bob Wilsona4c22902009-04-17 19:07:39 +00002279 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002280 SmallVector<SDValue, 4> RetOps;
2281 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002282 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002283
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002284 MachineFunction &MF = DAG.getMachineFunction();
2285 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2286 AFI->setReturnRegsCount(RVLocs.size());
2287
Bob Wilsona4c22902009-04-17 19:07:39 +00002288 // Copy the result values into the output registers.
2289 for (unsigned i = 0, realRVLocIdx = 0;
2290 i != RVLocs.size();
2291 ++i, ++realRVLocIdx) {
2292 CCValAssign &VA = RVLocs[i];
2293 assert(VA.isRegLoc() && "Can only return in registers!");
2294
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002295 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002296
2297 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002298 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002299 case CCValAssign::Full: break;
2300 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002301 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002302 break;
2303 }
2304
Bob Wilsona4c22902009-04-17 19:07:39 +00002305 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002306 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002307 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002308 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002309 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002310 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002311 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002312
Christian Pirkerb5728192014-05-08 14:06:24 +00002313 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2314 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2315 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002316 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002317 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002318 VA = RVLocs[++i]; // skip ahead to next loc
2319 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002320 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2321 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002322 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002323 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002324 VA = RVLocs[++i]; // skip ahead to next loc
2325
2326 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002327 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002328 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002329 }
2330 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2331 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002332 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002333 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002334 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2335 fmrrd.getValue(isLittleEndian ? 0 : 1),
2336 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002337 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002338 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002339 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002340 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2341 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002342 Flag);
2343 } else
2344 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2345
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002346 // Guarantee that all emitted copies are
2347 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002348 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002349 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002350 }
2351
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002352 // Update chain and glue.
2353 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002354 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002355 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002356
Tim Northoverd8407452013-10-01 14:33:28 +00002357 // CPUs which aren't M-class use a special sequence to return from
2358 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2359 // though we use "subs pc, lr, #N").
2360 //
2361 // M-class CPUs actually use a normal return sequence with a special
2362 // (hardware-provided) value in LR, so the normal code path works.
2363 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2364 !Subtarget->isMClass()) {
2365 if (Subtarget->isThumb1Only())
2366 report_fatal_error("interrupt attribute is not supported in Thumb1");
2367 return LowerInterruptReturn(RetOps, dl, DAG);
2368 }
2369
Craig Topper48d114b2014-04-26 18:35:24 +00002370 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002371}
2372
Evan Chengf8bad082012-04-10 01:51:00 +00002373bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002374 if (N->getNumValues() != 1)
2375 return false;
2376 if (!N->hasNUsesOfValue(1, 0))
2377 return false;
2378
Evan Chengf8bad082012-04-10 01:51:00 +00002379 SDValue TCChain = Chain;
2380 SDNode *Copy = *N->use_begin();
2381 if (Copy->getOpcode() == ISD::CopyToReg) {
2382 // If the copy has a glue operand, we conservatively assume it isn't safe to
2383 // perform a tail call.
2384 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2385 return false;
2386 TCChain = Copy->getOperand(0);
2387 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2388 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002389 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002390 SmallPtrSet<SDNode*, 2> Copies;
2391 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002392 UI != UE; ++UI) {
2393 if (UI->getOpcode() != ISD::CopyToReg)
2394 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002395 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002396 }
Evan Chengf8bad082012-04-10 01:51:00 +00002397 if (Copies.size() > 2)
2398 return false;
2399
2400 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2401 UI != UE; ++UI) {
2402 SDValue UseChain = UI->getOperand(0);
2403 if (Copies.count(UseChain.getNode()))
2404 // Second CopyToReg
2405 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002406 else {
2407 // We are at the top of this chain.
2408 // If the copy has a glue operand, we conservatively assume it
2409 // isn't safe to perform a tail call.
2410 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2411 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002412 // First CopyToReg
2413 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002414 }
Evan Chengf8bad082012-04-10 01:51:00 +00002415 }
2416 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002417 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002418 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002419 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002420 Copy = *Copy->use_begin();
2421 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002422 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002423 // If the copy has a glue operand, we conservatively assume it isn't safe to
2424 // perform a tail call.
2425 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2426 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002427 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002428 } else {
2429 return false;
2430 }
2431
Evan Cheng419ea282010-12-01 22:59:46 +00002432 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002433 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2434 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002435 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2436 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002437 return false;
2438 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002439 }
2440
Evan Chengf8bad082012-04-10 01:51:00 +00002441 if (!HasRet)
2442 return false;
2443
2444 Chain = TCChain;
2445 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002446}
2447
Evan Cheng0663f232011-03-21 01:19:09 +00002448bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002449 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002450 return false;
2451
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002452 auto Attr =
2453 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2454 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002455 return false;
2456
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002457 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002458}
2459
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002460// Trying to write a 64 bit value so need to split into two 32 bit values first,
2461// and pass the lower and high parts through.
2462static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2463 SDLoc DL(Op);
2464 SDValue WriteValue = Op->getOperand(2);
2465
2466 // This function is only supposed to be called for i64 type argument.
2467 assert(WriteValue.getValueType() == MVT::i64
2468 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2469
2470 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2471 DAG.getConstant(0, DL, MVT::i32));
2472 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2473 DAG.getConstant(1, DL, MVT::i32));
2474 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2475 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2476}
2477
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002478// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2479// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2480// one of the above mentioned nodes. It has to be wrapped because otherwise
2481// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2482// be used to form addressing mode. These wrapped nodes will be selected
2483// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002484static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002485 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002486 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002487 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002488 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002489 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002490 if (CP->isMachineConstantPoolEntry())
2491 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2492 CP->getAlignment());
2493 else
2494 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2495 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002496 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002497}
2498
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002499unsigned ARMTargetLowering::getJumpTableEncoding() const {
2500 return MachineJumpTableInfo::EK_Inline;
2501}
2502
Dan Gohman21cea8a2010-04-17 15:26:15 +00002503SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2504 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002505 MachineFunction &MF = DAG.getMachineFunction();
2506 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2507 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002508 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002509 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002510 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002511 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2512 SDValue CPAddr;
2513 if (RelocM == Reloc::Static) {
2514 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2515 } else {
2516 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002517 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002518 ARMConstantPoolValue *CPV =
2519 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2520 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002521 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2522 }
2523 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002524 SDValue Result =
2525 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2526 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2527 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002528 if (RelocM == Reloc::Static)
2529 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002530 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002531 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002532}
2533
Tim Northoverbd41cf82016-01-07 09:03:03 +00002534/// \brief Convert a TLS address reference into the correct sequence of loads
2535/// and calls to compute the variable's address for Darwin, and return an
2536/// SDValue containing the final node.
2537
2538/// Darwin only has one TLS scheme which must be capable of dealing with the
2539/// fully general situation, in the worst case. This means:
2540/// + "extern __thread" declaration.
2541/// + Defined in a possibly unknown dynamic library.
2542///
2543/// The general system is that each __thread variable has a [3 x i32] descriptor
2544/// which contains information used by the runtime to calculate the address. The
2545/// only part of this the compiler needs to know about is the first word, which
2546/// contains a function pointer that must be called with the address of the
2547/// entire descriptor in "r0".
2548///
2549/// Since this descriptor may be in a different unit, in general access must
2550/// proceed along the usual ARM rules. A common sequence to produce is:
2551///
2552/// movw rT1, :lower16:_var$non_lazy_ptr
2553/// movt rT1, :upper16:_var$non_lazy_ptr
2554/// ldr r0, [rT1]
2555/// ldr rT2, [r0]
2556/// blx rT2
2557/// [...address now in r0...]
2558SDValue
2559ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2560 SelectionDAG &DAG) const {
2561 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");
2562 SDLoc DL(Op);
2563
2564 // First step is to get the address of the actua global symbol. This is where
2565 // the TLS descriptor lives.
2566 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2567
2568 // The first entry in the descriptor is a function pointer that we must call
2569 // to obtain the address of the variable.
2570 SDValue Chain = DAG.getEntryNode();
2571 SDValue FuncTLVGet =
2572 DAG.getLoad(MVT::i32, DL, Chain, DescAddr,
2573 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2574 false, true, true, 4);
2575 Chain = FuncTLVGet.getValue(1);
2576
2577 MachineFunction &F = DAG.getMachineFunction();
2578 MachineFrameInfo *MFI = F.getFrameInfo();
2579 MFI->setAdjustsStack(true);
2580
2581 // TLS calls preserve all registers except those that absolutely must be
2582 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2583 // silly).
2584 auto TRI =
2585 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo();
2586 auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2587 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2588
2589 // Finally, we can make the call. This is just a degenerate version of a
2590 // normal AArch64 call node: r0 takes the address of the descriptor, and
2591 // returns the address of the variable in this thread.
2592 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2593 Chain =
2594 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2595 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2596 DAG.getRegisterMask(Mask), Chain.getValue(1));
2597 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2598}
2599
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002600// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002601SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002602ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002603 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002604 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002605 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002606 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002607 MachineFunction &MF = DAG.getMachineFunction();
2608 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002609 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002610 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002611 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2612 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002613 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002614 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002615 Argument =
2616 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2617 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2618 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002619 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002620
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002621 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002622 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002623
2624 // call __tls_get_addr.
2625 ArgListTy Args;
2626 ArgListEntry Entry;
2627 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002628 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002629 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002630
Dale Johannesen555a3752009-01-30 23:10:59 +00002631 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002632 TargetLowering::CallLoweringInfo CLI(DAG);
2633 CLI.setDebugLoc(dl).setChain(Chain)
2634 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002635 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2636 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002637
Justin Holewinskiaa583972012-05-25 16:35:28 +00002638 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002639 return CallResult.first;
2640}
2641
2642// Lower ISD::GlobalTLSAddress using the "initial exec" or
2643// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002644SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002645ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002646 SelectionDAG &DAG,
2647 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002648 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002649 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002650 SDValue Offset;
2651 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002652 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002653 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002654 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002655
Hans Wennborgaea41202012-05-04 09:40:39 +00002656 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002657 MachineFunction &MF = DAG.getMachineFunction();
2658 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002659 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002660 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002661 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2662 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002663 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2664 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2665 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002666 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002667 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002668 Offset = DAG.getLoad(
2669 PtrVT, dl, Chain, Offset,
2670 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2671 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002672 Chain = Offset.getValue(1);
2673
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002674 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002675 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002676
Alex Lorenze40c8a22015-08-11 23:09:45 +00002677 Offset = DAG.getLoad(
2678 PtrVT, dl, Chain, Offset,
2679 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2680 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002681 } else {
2682 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002683 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002684 ARMConstantPoolValue *CPV =
2685 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002686 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002687 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002688 Offset = DAG.getLoad(
2689 PtrVT, dl, Chain, Offset,
2690 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2691 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002692 }
2693
2694 // The address of the thread local variable is the add of the thread
2695 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002696 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002697}
2698
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002699SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002700ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Tim Northoverbd41cf82016-01-07 09:03:03 +00002701 if (Subtarget->isTargetDarwin())
2702 return LowerGlobalTLSAddressDarwin(Op, DAG);
2703
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002704 // TODO: implement the "local dynamic" model
Tim Northoverbd41cf82016-01-07 09:03:03 +00002705 assert(Subtarget->isTargetELF() && "Only ELF implemented here");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002706 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002707 if (DAG.getTarget().Options.EmulatedTLS)
2708 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002709
2710 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2711
2712 switch (model) {
2713 case TLSModel::GeneralDynamic:
2714 case TLSModel::LocalDynamic:
2715 return LowerToTLSGeneralDynamicModel(GA, DAG);
2716 case TLSModel::InitialExec:
2717 case TLSModel::LocalExec:
2718 return LowerToTLSExecModels(GA, DAG, model);
2719 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002720 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002721}
2722
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002723SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002724 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002725 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002726 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002727 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002728 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Peter Collingbourne97aae402015-10-26 18:23:16 +00002729 bool UseGOT_PREL =
Peter Collingbourne99fac802015-10-26 20:46:44 +00002730 !(GV->hasHiddenVisibility() || GV->hasLocalLinkage());
Peter Collingbourne97aae402015-10-26 18:23:16 +00002731
2732 MachineFunction &MF = DAG.getMachineFunction();
2733 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2734 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2735 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2736 SDLoc dl(Op);
2737 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2738 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2739 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2740 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2741 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002742 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002743 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002744 SDValue Result = DAG.getLoad(
2745 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2746 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2747 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002748 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002749 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2750 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2751 if (UseGOT_PREL)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002752 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002753 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002754 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002755 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002756 }
2757
2758 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002759 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002760 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002761 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002762 // FIXME: Once remat is capable of dealing with instructions with register
2763 // operands, expand this into two nodes.
2764 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2765 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002766 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002767 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2768 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002769 return DAG.getLoad(
2770 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2771 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2772 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002773 }
2774}
2775
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002776SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002777 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002778 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002779 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002780 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002781 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002782
Eric Christopherc1058df2014-07-04 01:55:26 +00002783 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002784 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002785
Tim Northover72360d22013-12-02 10:35:41 +00002786 // FIXME: Once remat is capable of dealing with instructions with register
2787 // operands, expand this into multiple nodes
2788 unsigned Wrapper =
2789 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002790
Tim Northover72360d22013-12-02 10:35:41 +00002791 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2792 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002793
Evan Cheng1b389522009-09-03 07:04:02 +00002794 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002795 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002796 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2797 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002798 return Result;
2799}
2800
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002801SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2802 SelectionDAG &DAG) const {
2803 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002804 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2805 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002806
2807 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002808 const ARMII::TOF TargetFlags =
2809 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002810 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002811 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002812 SDLoc DL(Op);
2813
2814 ++NumMovwMovt;
2815
2816 // FIXME: Once remat is capable of dealing with instructions with register
2817 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002818 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2819 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002820 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002821 if (GV->hasDLLImportStorageClass())
2822 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002823 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2824 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002825 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002826}
2827
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002828SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002829ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002830 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002831 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002832 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2833 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002834 Op.getOperand(1), Val);
2835}
2836
2837SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002838ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002839 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002840 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002841 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002842}
2843
Matthias Braun3cd00c12015-07-16 22:34:16 +00002844SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2845 SelectionDAG &DAG) const {
2846 SDLoc dl(Op);
2847 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2848 Op.getOperand(0));
2849}
2850
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002851SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002852ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002853 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002854 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002855 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002856 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002857 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002858 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002859 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002860 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00002861 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002862 }
Bob Wilson17f88782009-08-04 00:25:01 +00002863 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002864 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002865 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2866 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002867 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002868 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002869 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002870 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002871 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002872 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2873 SDValue CPAddr;
2874 unsigned PCAdj = (RelocM != Reloc::PIC_)
2875 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002876 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002877 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2878 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002879 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002880 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002881 SDValue Result = DAG.getLoad(
2882 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2883 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2884 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002885
2886 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002887 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002888 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2889 }
2890 return Result;
2891 }
Evan Cheng18381b42011-03-29 23:06:19 +00002892 case Intrinsic::arm_neon_vmulls:
2893 case Intrinsic::arm_neon_vmullu: {
2894 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2895 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002896 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002897 Op.getOperand(1), Op.getOperand(2));
2898 }
James Molloyee868b22015-08-11 12:06:25 +00002899 case Intrinsic::arm_neon_vminnm:
2900 case Intrinsic::arm_neon_vmaxnm: {
2901 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2902 ? ISD::FMINNUM : ISD::FMAXNUM;
2903 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2904 Op.getOperand(1), Op.getOperand(2));
2905 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002906 case Intrinsic::arm_neon_vminu:
2907 case Intrinsic::arm_neon_vmaxu: {
2908 if (Op.getValueType().isFloatingPoint())
2909 return SDValue();
2910 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2911 ? ISD::UMIN : ISD::UMAX;
2912 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2913 Op.getOperand(1), Op.getOperand(2));
2914 }
James Molloyd616c642015-08-11 12:06:28 +00002915 case Intrinsic::arm_neon_vmins:
2916 case Intrinsic::arm_neon_vmaxs: {
2917 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002918 if (!Op.getValueType().isFloatingPoint()) {
2919 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2920 ? ISD::SMIN : ISD::SMAX;
2921 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2922 Op.getOperand(1), Op.getOperand(2));
2923 }
James Molloyd616c642015-08-11 12:06:28 +00002924 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2925 ? ISD::FMINNAN : ISD::FMAXNAN;
2926 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2927 Op.getOperand(1), Op.getOperand(2));
2928 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002929 }
2930}
2931
Eli Friedman30a49e92011-08-03 21:06:02 +00002932static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2933 const ARMSubtarget *Subtarget) {
2934 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002935 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002936 if (!Subtarget->hasDataBarrier()) {
2937 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2938 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2939 // here.
2940 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002941 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002942 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002943 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002944 }
2945
Tim Northover36b24172013-07-03 09:20:36 +00002946 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2947 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002948 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002949 if (Subtarget->isMClass()) {
2950 // Only a full system barrier exists in the M-class architectures.
2951 Domain = ARM_MB::SY;
2952 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002953 // Swift happens to implement ISHST barriers in a way that's compatible with
2954 // Release semantics but weaker than ISH so we'd be fools not to use
2955 // it. Beware: other processors probably don't!
2956 Domain = ARM_MB::ISHST;
2957 }
2958
Joey Gouly926d3f52013-09-05 15:35:24 +00002959 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002960 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2961 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002962}
2963
Evan Cheng8740ee32010-11-03 06:34:55 +00002964static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2965 const ARMSubtarget *Subtarget) {
2966 // ARM pre v5TE and Thumb1 does not have preload instructions.
2967 if (!(Subtarget->isThumb2() ||
2968 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2969 // Just preserve the chain.
2970 return Op.getOperand(0);
2971
Andrew Trickef9de2a2013-05-25 02:42:55 +00002972 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002973 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2974 if (!isRead &&
2975 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2976 // ARMv7 with MP extension has PLDW.
2977 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002978
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002979 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2980 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002981 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002982 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002983 isData = ~isData & 1;
2984 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002985
2986 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002987 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2988 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002989}
2990
Dan Gohman31ae5862010-04-17 14:41:14 +00002991static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2992 MachineFunction &MF = DAG.getMachineFunction();
2993 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2994
Evan Cheng10043e22007-01-19 07:51:42 +00002995 // vastart just stores the address of the VarArgsFrameIndex slot into the
2996 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002997 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002998 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002999 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00003000 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00003001 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
3002 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00003003}
3004
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003005SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00003006ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
3007 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003008 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00003009 MachineFunction &MF = DAG.getMachineFunction();
3010 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3011
Craig Topper760b1342012-02-22 05:59:10 +00003012 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00003013 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00003014 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003015 else
Craig Topperc7242e02012-04-20 07:30:17 +00003016 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003017
3018 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003019 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003020 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003021
3022 SDValue ArgValue2;
3023 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003024 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00003025 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00003026
3027 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003028 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003029 ArgValue2 = DAG.getLoad(
3030 MVT::i32, dl, Root, FIN,
3031 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
3032 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00003033 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00003034 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003035 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003036 }
Christian Pirkerb5728192014-05-08 14:06:24 +00003037 if (!Subtarget->isLittle())
3038 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003039 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00003040}
3041
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003042// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00003043// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003044// byval). Either way, we allocate stack slots adjacent to the data
3045// provided by our caller, and store the unallocated registers there.
3046// If this is a variadic function, the va_list pointer will begin with
3047// these values; otherwise, this reassembles a (byval) structure that
3048// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003049// Return: The frame index registers were stored into.
3050int
3051ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003052 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003053 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003054 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00003055 int ArgOffset,
3056 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003057 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00003058 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003059 // Setup first unallocated register as first byval register;
3060 // eat all remained registers
3061 // (these two actions are performed by HandleByVal method).
3062 // Then, here, we initialize stack frame with
3063 // "store-reg" instructions.
3064 // Case #2. Var-args function, that doesn't contain byval parameters.
3065 // The same: eat all remained unallocated registers,
3066 // initialize stack frame.
3067
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003068 MachineFunction &MF = DAG.getMachineFunction();
3069 MachineFrameInfo *MFI = MF.getFrameInfo();
3070 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003071 unsigned RBegin, REnd;
3072 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3073 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003074 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003075 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003076 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003077 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003078 }
3079
Tim Northover8cda34f2015-03-11 18:54:22 +00003080 if (REnd != RBegin)
3081 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003082
Mehdi Amini44ede332015-07-09 02:09:04 +00003083 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003084 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003085 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003086
Tim Northover8cda34f2015-03-11 18:54:22 +00003087 SmallVector<SDValue, 4> MemOps;
3088 const TargetRegisterClass *RC =
3089 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003090
Tim Northover8cda34f2015-03-11 18:54:22 +00003091 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3092 unsigned VReg = MF.addLiveIn(Reg, RC);
3093 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3094 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003095 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003096 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3097 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003098 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003099 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003100
3101 if (!MemOps.empty())
3102 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3103 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003104}
3105
3106// Setup stack frame, the va_list pointer will start from.
3107void
3108ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003109 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003110 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003111 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003112 bool ForceMutable) const {
3113 MachineFunction &MF = DAG.getMachineFunction();
3114 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3115
3116 // Try to store any remaining integer argument regs
3117 // to their spots on the stack so that they may be loaded by deferencing
3118 // the result of va_next.
3119 // If there is no regs to be stored, just point address after last
3120 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003121 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3122 CCInfo.getInRegsParamsCount(),
3123 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003124 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003125}
3126
Bob Wilson2e076c42009-06-22 23:27:02 +00003127SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003128ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003129 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003130 const SmallVectorImpl<ISD::InputArg>
3131 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003132 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003133 SmallVectorImpl<SDValue> &InVals)
3134 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003135 MachineFunction &MF = DAG.getMachineFunction();
3136 MachineFrameInfo *MFI = MF.getFrameInfo();
3137
Bob Wilsona4c22902009-04-17 19:07:39 +00003138 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3139
3140 // Assign locations to all of the incoming arguments.
3141 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003142 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3143 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003144 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003145 CCAssignFnForNode(CallConv, /* Return*/ false,
3146 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003147
Bob Wilsona4c22902009-04-17 19:07:39 +00003148 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003149 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003150 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3151 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003152
3153 // Initially ArgRegsSaveSize is zero.
3154 // Then we increase this value each time we meet byval parameter.
3155 // We also increase this value in case of varargs function.
3156 AFI->setArgRegsSaveSize(0);
3157
Oliver Stannardd55e1152014-03-05 15:25:27 +00003158 // Calculate the amount of stack space that we need to allocate to store
3159 // byval and variadic arguments that are passed in registers.
3160 // We need to know this before we allocate the first byval or variadic
3161 // argument, as they will be allocated a stack slot below the CFA (Canonical
3162 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003163 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003164 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003165 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3166 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003167
Tim Northover8cda34f2015-03-11 18:54:22 +00003168 CCValAssign &VA = ArgLocs[i];
3169 unsigned Index = VA.getValNo();
3170 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3171 if (!Flags.isByVal())
3172 continue;
3173
3174 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3175 unsigned RBegin, REnd;
3176 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3177 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3178
3179 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003180 }
3181 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003182
3183 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003184 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003185 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3186 if (RegIdx != array_lengthof(GPRArgRegs))
3187 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003188 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003189
3190 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3191 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003192 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003193
Bob Wilsona4c22902009-04-17 19:07:39 +00003194 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3195 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003196 if (Ins[VA.getValNo()].isOrigArg()) {
3197 std::advance(CurOrigArg,
3198 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3199 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3200 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003201 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003202 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003203 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003204
Bob Wilsona4c22902009-04-17 19:07:39 +00003205 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003206 // f64 and vector types are split up into multiple registers or
3207 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003208 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003209 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003210 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003211 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003212 SDValue ArgValue2;
3213 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003214 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003215 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003216 ArgValue2 = DAG.getLoad(
3217 MVT::f64, dl, Chain, FIN,
3218 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3219 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003220 } else {
3221 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3222 Chain, DAG, dl);
3223 }
Owen Anderson9f944592009-08-11 20:47:22 +00003224 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3225 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003226 ArgValue, ArgValue1,
3227 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003228 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003229 ArgValue, ArgValue2,
3230 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003231 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003232 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003233
Bob Wilson2e076c42009-06-22 23:27:02 +00003234 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003235 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003236
Owen Anderson9f944592009-08-11 20:47:22 +00003237 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003238 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003239 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003240 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003241 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003242 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003243 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003244 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3245 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003246 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003247 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003248
3249 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003250 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003251 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003252 }
3253
3254 // If this is an 8 or 16-bit value, it is really passed promoted
3255 // to 32 bits. Insert an assert[sz]ext to capture this, then
3256 // truncate to the right size.
3257 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003258 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003259 case CCValAssign::Full: break;
3260 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003261 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003262 break;
3263 case CCValAssign::SExt:
3264 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3265 DAG.getValueType(VA.getValVT()));
3266 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3267 break;
3268 case CCValAssign::ZExt:
3269 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3270 DAG.getValueType(VA.getValVT()));
3271 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3272 break;
3273 }
3274
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003275 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003276
3277 } else { // VA.isRegLoc()
3278
3279 // sanity check
3280 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003281 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003282
Andrew Trick05938a52015-02-16 18:10:47 +00003283 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003284
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003285 // Some Ins[] entries become multiple ArgLoc[] entries.
3286 // Process them only once.
3287 if (index != lastInsIndex)
3288 {
3289 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003290 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003291 // This can be changed with more analysis.
3292 // In case of tail call optimization mark all arguments mutable.
3293 // Since they could be overwritten by lowering of arguments in case of
3294 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003295 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003296 assert(Ins[index].isOrigArg() &&
3297 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003298 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003299
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003300 int FrameIndex = StoreByValRegs(
3301 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3302 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003303 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003304 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003305 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003306 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003307 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003308 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003309
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003310 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003311 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003312 InVals.push_back(DAG.getLoad(
3313 VA.getValVT(), dl, Chain, FIN,
3314 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3315 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003316 }
3317 lastInsIndex = index;
3318 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003319 }
3320 }
3321
3322 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003323 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003324 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003325 CCInfo.getNextStackOffset(),
3326 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003327
Oliver Stannardb14c6252014-04-02 16:10:33 +00003328 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3329
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003330 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003331}
3332
3333/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003334static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003335 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003336 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003337 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003338 // Maybe this has already been legalized into the constant pool?
3339 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003340 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003341 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003342 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003343 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003344 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003345 } else if (Op->getOpcode() == ISD::BITCAST &&
3346 Op->getValueType(0) == MVT::f64) {
3347 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3348 // created by LowerConstantFP().
3349 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003350 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3351 isNullConstant(BitcastOp->getOperand(0)))
3352 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003353 }
3354 return false;
3355}
3356
Evan Cheng10043e22007-01-19 07:51:42 +00003357/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3358/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003359SDValue
3360ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003361 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003362 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003363 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003364 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003365 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003366 // Constant does not fit, try adjusting it by one?
3367 switch (CC) {
3368 default: break;
3369 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003370 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003371 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003372 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003373 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003374 }
3375 break;
3376 case ISD::SETULT:
3377 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003378 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003379 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003380 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003381 }
3382 break;
3383 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003384 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003385 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003386 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003387 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003388 }
3389 break;
3390 case ISD::SETULE:
3391 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003392 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003393 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003394 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003395 }
3396 break;
3397 }
3398 }
3399 }
3400
3401 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003402 ARMISD::NodeType CompareType;
3403 switch (CondCode) {
3404 default:
3405 CompareType = ARMISD::CMP;
3406 break;
3407 case ARMCC::EQ:
3408 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003409 // Uses only Z Flag
3410 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003411 break;
3412 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003413 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003414 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003415}
3416
3417/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003418SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003419ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003420 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003421 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003422 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003423 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003424 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003425 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003426 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3427 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003428}
3429
Bob Wilson45acbd02011-03-08 01:17:20 +00003430/// duplicateCmp - Glue values can have only one use, so this function
3431/// duplicates a comparison node.
3432SDValue
3433ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3434 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003435 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003436 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3437 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3438
3439 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3440 Cmp = Cmp.getOperand(0);
3441 Opc = Cmp.getOpcode();
3442 if (Opc == ARMISD::CMPFP)
3443 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3444 else {
3445 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3446 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3447 }
3448 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3449}
3450
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003451std::pair<SDValue, SDValue>
3452ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3453 SDValue &ARMcc) const {
3454 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3455
3456 SDValue Value, OverflowCmp;
3457 SDValue LHS = Op.getOperand(0);
3458 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003459 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003460
3461 // FIXME: We are currently always generating CMPs because we don't support
3462 // generating CMN through the backend. This is not as good as the natural
3463 // CMP case because it causes a register dependency and cannot be folded
3464 // later.
3465
3466 switch (Op.getOpcode()) {
3467 default:
3468 llvm_unreachable("Unknown overflow instruction!");
3469 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003470 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3471 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3472 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003473 break;
3474 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003475 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3476 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3477 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003478 break;
3479 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003480 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3481 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3482 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003483 break;
3484 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003485 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3486 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3487 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003488 break;
3489 } // switch (...)
3490
3491 return std::make_pair(Value, OverflowCmp);
3492}
3493
3494
3495SDValue
3496ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3497 // Let legalize expand this if it isn't a legal type yet.
3498 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3499 return SDValue();
3500
3501 SDValue Value, OverflowCmp;
3502 SDValue ARMcc;
3503 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3504 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003505 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003506 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003507 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3508 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003509 EVT VT = Op.getValueType();
3510
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003511 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003512 ARMcc, CCR, OverflowCmp);
3513
3514 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003515 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003516}
3517
3518
Bill Wendling6a981312010-08-11 08:43:16 +00003519SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3520 SDValue Cond = Op.getOperand(0);
3521 SDValue SelectTrue = Op.getOperand(1);
3522 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003523 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003524 unsigned Opc = Cond.getOpcode();
3525
3526 if (Cond.getResNo() == 1 &&
3527 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3528 Opc == ISD::USUBO)) {
3529 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3530 return SDValue();
3531
3532 SDValue Value, OverflowCmp;
3533 SDValue ARMcc;
3534 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3535 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3536 EVT VT = Op.getValueType();
3537
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003538 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003539 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003540 }
Bill Wendling6a981312010-08-11 08:43:16 +00003541
3542 // Convert:
3543 //
3544 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3545 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3546 //
3547 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3548 const ConstantSDNode *CMOVTrue =
3549 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3550 const ConstantSDNode *CMOVFalse =
3551 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3552
3553 if (CMOVTrue && CMOVFalse) {
3554 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3555 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3556
3557 SDValue True;
3558 SDValue False;
3559 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3560 True = SelectTrue;
3561 False = SelectFalse;
3562 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3563 True = SelectFalse;
3564 False = SelectTrue;
3565 }
3566
3567 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003568 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003569 SDValue ARMcc = Cond.getOperand(2);
3570 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003571 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003572 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003573 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003574 }
3575 }
3576 }
3577
Dan Gohmand4a77c42012-02-24 00:09:36 +00003578 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3579 // undefined bits before doing a full-word comparison with zero.
3580 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003581 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003582
Bill Wendling6a981312010-08-11 08:43:16 +00003583 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003584 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003585 SelectTrue, SelectFalse, ISD::SETNE);
3586}
3587
Joey Gouly881eab52013-08-22 15:29:11 +00003588static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3589 bool &swpCmpOps, bool &swpVselOps) {
3590 // Start by selecting the GE condition code for opcodes that return true for
3591 // 'equality'
3592 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3593 CC == ISD::SETULE)
3594 CondCode = ARMCC::GE;
3595
3596 // and GT for opcodes that return false for 'equality'.
3597 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3598 CC == ISD::SETULT)
3599 CondCode = ARMCC::GT;
3600
3601 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3602 // to swap the compare operands.
3603 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3604 CC == ISD::SETULT)
3605 swpCmpOps = true;
3606
3607 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3608 // If we have an unordered opcode, we need to swap the operands to the VSEL
3609 // instruction (effectively negating the condition).
3610 //
3611 // This also has the effect of swapping which one of 'less' or 'greater'
3612 // returns true, so we also swap the compare operands. It also switches
3613 // whether we return true for 'equality', so we compensate by picking the
3614 // opposite condition code to our original choice.
3615 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3616 CC == ISD::SETUGT) {
3617 swpCmpOps = !swpCmpOps;
3618 swpVselOps = !swpVselOps;
3619 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3620 }
3621
3622 // 'ordered' is 'anything but unordered', so use the VS condition code and
3623 // swap the VSEL operands.
3624 if (CC == ISD::SETO) {
3625 CondCode = ARMCC::VS;
3626 swpVselOps = true;
3627 }
3628
3629 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3630 // code and swap the VSEL operands.
3631 if (CC == ISD::SETUNE) {
3632 CondCode = ARMCC::EQ;
3633 swpVselOps = true;
3634 }
3635}
3636
Oliver Stannard51b1d462014-08-21 12:50:31 +00003637SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3638 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3639 SDValue Cmp, SelectionDAG &DAG) const {
3640 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3641 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3642 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3643 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3644 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3645
3646 SDValue TrueLow = TrueVal.getValue(0);
3647 SDValue TrueHigh = TrueVal.getValue(1);
3648 SDValue FalseLow = FalseVal.getValue(0);
3649 SDValue FalseHigh = FalseVal.getValue(1);
3650
3651 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3652 ARMcc, CCR, Cmp);
3653 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3654 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3655
3656 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3657 } else {
3658 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3659 Cmp);
3660 }
3661}
3662
Dan Gohman21cea8a2010-04-17 15:26:15 +00003663SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003664 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003665 SDValue LHS = Op.getOperand(0);
3666 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003667 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003668 SDValue TrueVal = Op.getOperand(2);
3669 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003670 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003671
Oliver Stannard51b1d462014-08-21 12:50:31 +00003672 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3673 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3674 dl);
3675
3676 // If softenSetCCOperands only returned one value, we should compare it to
3677 // zero.
3678 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003679 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003680 CC = ISD::SETNE;
3681 }
3682 }
3683
Owen Anderson9f944592009-08-11 20:47:22 +00003684 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003685 // Try to generate VSEL on ARMv8.
3686 // The VSEL instruction can't use all the usual ARM condition
3687 // codes: it only has two bits to select the condition code, so it's
3688 // constrained to use only GE, GT, VS and EQ.
3689 //
3690 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3691 // swap the operands of the previous compare instruction (effectively
3692 // inverting the compare condition, swapping 'less' and 'greater') and
3693 // sometimes need to swap the operands to the VSEL (which inverts the
3694 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003695 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3696 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003697 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3698 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3699 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003700 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003701 std::swap(TrueVal, FalseVal);
3702 }
3703 }
3704
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003705 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003706 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003707 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003708 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003709 }
3710
3711 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003712 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003713
Scott Douglass7ad77922015-04-08 17:18:28 +00003714 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003715 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3716 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003717 bool swpCmpOps = false;
3718 bool swpVselOps = false;
3719 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3720
3721 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3722 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3723 if (swpCmpOps)
3724 std::swap(LHS, RHS);
3725 if (swpVselOps)
3726 std::swap(TrueVal, FalseVal);
3727 }
3728 }
3729
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003730 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003731 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003732 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003733 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003734 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003735 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003736 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003737 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003738 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003739 }
3740 return Result;
3741}
3742
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003743/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3744/// to morph to an integer compare sequence.
3745static bool canChangeToInt(SDValue Op, bool &SeenZero,
3746 const ARMSubtarget *Subtarget) {
3747 SDNode *N = Op.getNode();
3748 if (!N->hasOneUse())
3749 // Otherwise it requires moving the value from fp to integer registers.
3750 return false;
3751 if (!N->getNumValues())
3752 return false;
3753 EVT VT = Op.getValueType();
3754 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3755 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3756 // vmrs are very slow, e.g. cortex-a8.
3757 return false;
3758
3759 if (isFloatingPointZero(Op)) {
3760 SeenZero = true;
3761 return true;
3762 }
3763 return ISD::isNormalLoad(N);
3764}
3765
3766static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3767 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003768 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003769
3770 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003771 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003772 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003773 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003774 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003775
3776 llvm_unreachable("Unknown VFP cmp argument!");
3777}
3778
3779static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3780 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003781 SDLoc dl(Op);
3782
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003783 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003784 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3785 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003786 return;
3787 }
3788
3789 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3790 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003791 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003792 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003793 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003794 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003795 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003796
3797 EVT PtrType = Ptr.getValueType();
3798 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003799 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3800 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3801 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003802 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003803 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003804 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003805 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003806 return;
3807 }
3808
3809 llvm_unreachable("Unknown VFP cmp argument!");
3810}
3811
3812/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3813/// f32 and even f64 comparisons to integer ones.
3814SDValue
3815ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3816 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003817 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003818 SDValue LHS = Op.getOperand(2);
3819 SDValue RHS = Op.getOperand(3);
3820 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003821 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003822
Evan Chengd12af5d2012-03-01 23:27:13 +00003823 bool LHSSeenZero = false;
3824 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3825 bool RHSSeenZero = false;
3826 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3827 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003828 // If unsafe fp math optimization is enabled and there are no other uses of
3829 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003830 // to an integer comparison.
3831 if (CC == ISD::SETOEQ)
3832 CC = ISD::SETEQ;
3833 else if (CC == ISD::SETUNE)
3834 CC = ISD::SETNE;
3835
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003836 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003837 SDValue ARMcc;
3838 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003839 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3840 bitcastf32Toi32(LHS, DAG), Mask);
3841 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3842 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003843 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3844 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3845 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3846 Chain, Dest, ARMcc, CCR, Cmp);
3847 }
3848
3849 SDValue LHS1, LHS2;
3850 SDValue RHS1, RHS2;
3851 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3852 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003853 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3854 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003855 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003856 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003857 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003858 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003859 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003860 }
3861
3862 return SDValue();
3863}
3864
3865SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3866 SDValue Chain = Op.getOperand(0);
3867 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3868 SDValue LHS = Op.getOperand(2);
3869 SDValue RHS = Op.getOperand(3);
3870 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003871 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003872
Oliver Stannard51b1d462014-08-21 12:50:31 +00003873 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3874 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3875 dl);
3876
3877 // If softenSetCCOperands only returned one value, we should compare it to
3878 // zero.
3879 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003880 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003881 CC = ISD::SETNE;
3882 }
3883 }
3884
Owen Anderson9f944592009-08-11 20:47:22 +00003885 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003886 SDValue ARMcc;
3887 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003888 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003889 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003890 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003891 }
3892
Owen Anderson9f944592009-08-11 20:47:22 +00003893 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003894
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003895 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003896 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3897 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3898 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3899 if (Result.getNode())
3900 return Result;
3901 }
3902
Evan Cheng10043e22007-01-19 07:51:42 +00003903 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003904 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003905
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003906 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003907 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003908 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003909 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003910 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003911 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003912 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003913 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003914 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003915 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003916 }
3917 return Res;
3918}
3919
Dan Gohman21cea8a2010-04-17 15:26:15 +00003920SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003921 SDValue Chain = Op.getOperand(0);
3922 SDValue Table = Op.getOperand(1);
3923 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003924 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003925
Mehdi Amini44ede332015-07-09 02:09:04 +00003926 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003927 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003928 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003929 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003930 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003931 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003932 if (Subtarget->isThumb2()) {
3933 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3934 // which does another jump to the destination. This also makes it easier
3935 // to translate it to TBB / TBH later.
3936 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003937 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003938 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003939 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003940 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003941 Addr =
3942 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
3943 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3944 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003945 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003946 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003947 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003948 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003949 Addr =
3950 DAG.getLoad(PTy, dl, Chain, Addr,
3951 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3952 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003953 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003954 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003955 }
Evan Cheng10043e22007-01-19 07:51:42 +00003956}
3957
Eli Friedman2d4055b2011-11-09 23:36:02 +00003958static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003959 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003960 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003961
James Molloy547d4c02012-02-20 09:24:05 +00003962 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3963 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3964 return Op;
3965 return DAG.UnrollVectorOp(Op.getNode());
3966 }
3967
3968 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3969 "Invalid type for custom lowering!");
3970 if (VT != MVT::v4i16)
3971 return DAG.UnrollVectorOp(Op.getNode());
3972
3973 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3974 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003975}
3976
Oliver Stannard51b1d462014-08-21 12:50:31 +00003977SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003978 EVT VT = Op.getValueType();
3979 if (VT.isVector())
3980 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003981 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3982 RTLIB::Libcall LC;
3983 if (Op.getOpcode() == ISD::FP_TO_SINT)
3984 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3985 Op.getValueType());
3986 else
3987 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3988 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00003989 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00003990 /*isSigned*/ false, SDLoc(Op)).first;
3991 }
3992
James Molloyfa041152015-03-23 16:15:16 +00003993 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003994}
3995
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003996static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3997 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003998 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003999
Eli Friedman2d4055b2011-11-09 23:36:02 +00004000 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4001 if (VT.getVectorElementType() == MVT::f32)
4002 return Op;
4003 return DAG.UnrollVectorOp(Op.getNode());
4004 }
4005
Duncan Sandsa41634e2011-08-12 14:54:45 +00004006 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
4007 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004008 if (VT != MVT::v4f32)
4009 return DAG.UnrollVectorOp(Op.getNode());
4010
4011 unsigned CastOpc;
4012 unsigned Opc;
4013 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00004014 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004015 case ISD::SINT_TO_FP:
4016 CastOpc = ISD::SIGN_EXTEND;
4017 Opc = ISD::SINT_TO_FP;
4018 break;
4019 case ISD::UINT_TO_FP:
4020 CastOpc = ISD::ZERO_EXTEND;
4021 Opc = ISD::UINT_TO_FP;
4022 break;
4023 }
4024
4025 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
4026 return DAG.getNode(Opc, dl, VT, Op);
4027}
4028
Oliver Stannard51b1d462014-08-21 12:50:31 +00004029SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00004030 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004031 if (VT.isVector())
4032 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004033 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4034 RTLIB::Libcall LC;
4035 if (Op.getOpcode() == ISD::SINT_TO_FP)
4036 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4037 Op.getValueType());
4038 else
4039 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4040 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004041 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004042 /*isSigned*/ false, SDLoc(Op)).first;
4043 }
4044
James Molloyfa041152015-03-23 16:15:16 +00004045 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004046}
4047
Evan Cheng25f93642010-07-08 02:08:50 +00004048SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004049 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004050 SDValue Tmp0 = Op.getOperand(0);
4051 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004052 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004053 EVT VT = Op.getValueType();
4054 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004055 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4056 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4057 bool UseNEON = !InGPR && Subtarget->hasNEON();
4058
4059 if (UseNEON) {
4060 // Use VBSL to copy the sign bit.
4061 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4062 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004063 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004064 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4065 if (VT == MVT::f64)
4066 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4067 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004068 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004069 else /*if (VT == MVT::f32)*/
4070 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4071 if (SrcVT == MVT::f32) {
4072 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4073 if (VT == MVT::f64)
4074 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4075 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004076 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004077 } else if (VT == MVT::f32)
4078 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4079 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004080 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004081 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4082 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4083
4084 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004085 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004086 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4087 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4088 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004089
Evan Chengd6b641e2011-02-23 02:24:55 +00004090 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4091 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4092 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004093 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004094 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4095 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004096 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004097 } else {
4098 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4099 }
4100
4101 return Res;
4102 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004103
4104 // Bitcast operand 1 to i32.
4105 if (SrcVT == MVT::f64)
4106 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004107 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004108 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4109
Evan Chengd6b641e2011-02-23 02:24:55 +00004110 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004111 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4112 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004113 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4114 if (VT == MVT::f32) {
4115 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4116 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4117 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4118 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004119 }
4120
Evan Chengd6b641e2011-02-23 02:24:55 +00004121 // f64: Or the high part with signbit and then combine two parts.
4122 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004123 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004124 SDValue Lo = Tmp0.getValue(0);
4125 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4126 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4127 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004128}
4129
Evan Cheng168ced92010-05-22 01:47:14 +00004130SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4131 MachineFunction &MF = DAG.getMachineFunction();
4132 MachineFrameInfo *MFI = MF.getFrameInfo();
4133 MFI->setReturnAddressIsTaken(true);
4134
Bill Wendling908bf812014-01-06 00:43:20 +00004135 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004136 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004137
Evan Cheng168ced92010-05-22 01:47:14 +00004138 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004139 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004140 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4141 if (Depth) {
4142 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004143 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004144 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4145 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004146 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004147 }
4148
4149 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004150 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004151 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4152}
4153
Dan Gohman21cea8a2010-04-17 15:26:15 +00004154SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004155 const ARMBaseRegisterInfo &ARI =
4156 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4157 MachineFunction &MF = DAG.getMachineFunction();
4158 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004159 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004160
Owen Anderson53aa7a92009-08-10 22:56:29 +00004161 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004162 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004163 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004164 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004165 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4166 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004167 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4168 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004169 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004170 return FrameAddr;
4171}
4172
Renato Golinc7aea402014-05-06 16:51:25 +00004173// FIXME? Maybe this could be a TableGen attribute on some registers and
4174// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004175unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4176 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004177 unsigned Reg = StringSwitch<unsigned>(RegName)
4178 .Case("sp", ARM::SP)
4179 .Default(0);
4180 if (Reg)
4181 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004182 report_fatal_error(Twine("Invalid register name \""
4183 + StringRef(RegName) + "\"."));
4184}
4185
4186// Result is 64 bit value so split into two 32 bit values and return as a
4187// pair of values.
4188static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4189 SelectionDAG &DAG) {
4190 SDLoc DL(N);
4191
4192 // This function is only supposed to be called for i64 type destination.
4193 assert(N->getValueType(0) == MVT::i64
4194 && "ExpandREAD_REGISTER called for non-i64 type result.");
4195
4196 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4197 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4198 N->getOperand(0),
4199 N->getOperand(1));
4200
4201 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4202 Read.getValue(1)));
4203 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004204}
4205
Quentin Colombet901f0362015-12-04 01:53:14 +00004206/// \p BC is a bitcast that is about to be turned into a VMOVDRR.
4207/// When \p DstVT, the destination type of \p BC, is on the vector
4208/// register bank and the source of bitcast, \p Op, operates on the same bank,
4209/// it might be possible to combine them, such that everything stays on the
4210/// vector register bank.
4211/// \p return The node that would replace \p BT, if the combine
4212/// is possible.
4213static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
4214 SelectionDAG &DAG) {
4215 SDValue Op = BC->getOperand(0);
4216 EVT DstVT = BC->getValueType(0);
4217
4218 // The only vector instruction that can produce a scalar (remember,
4219 // since the bitcast was about to be turned into VMOVDRR, the source
4220 // type is i64) from a vector is EXTRACT_VECTOR_ELT.
4221 // Moreover, we can do this combine only if there is one use.
4222 // Finally, if the destination type is not a vector, there is not
4223 // much point on forcing everything on the vector bank.
4224 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4225 !Op.hasOneUse())
4226 return SDValue();
4227
4228 // If the index is not constant, we will introduce an additional
4229 // multiply that will stick.
4230 // Give up in that case.
4231 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4232 if (!Index)
4233 return SDValue();
4234 unsigned DstNumElt = DstVT.getVectorNumElements();
4235
4236 // Compute the new index.
4237 const APInt &APIntIndex = Index->getAPIntValue();
4238 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
4239 NewIndex *= APIntIndex;
4240 // Check if the new constant index fits into i32.
4241 if (NewIndex.getBitWidth() > 32)
4242 return SDValue();
4243
4244 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
4245 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
4246 SDLoc dl(Op);
4247 SDValue ExtractSrc = Op.getOperand(0);
4248 EVT VecVT = EVT::getVectorVT(
4249 *DAG.getContext(), DstVT.getScalarType(),
4250 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
4251 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
4252 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
4253 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
4254}
4255
Wesley Peck527da1b2010-11-23 03:31:01 +00004256/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004257/// expand a bit convert where either the source or destination type is i64 to
4258/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4259/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4260/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004261static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004262 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004263 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004264 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004265
Bob Wilson59b70ea2010-04-17 05:30:19 +00004266 // This function is only supposed to be called for i64 types, either as the
4267 // source or destination of the bit convert.
4268 EVT SrcVT = Op.getValueType();
4269 EVT DstVT = N->getValueType(0);
4270 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004271 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004272
Bob Wilson59b70ea2010-04-17 05:30:19 +00004273 // Turn i64->f64 into VMOVDRR.
4274 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Quentin Colombet901f0362015-12-04 01:53:14 +00004275 // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
4276 // if we can combine the bitcast with its source.
4277 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
4278 return Val;
4279
Owen Anderson9f944592009-08-11 20:47:22 +00004280 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004281 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004282 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004283 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004284 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004285 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004286 }
Bob Wilson7117a912009-03-20 22:42:55 +00004287
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004288 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004289 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004290 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004291 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004292 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004293 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4294 DAG.getVTList(MVT::i32, MVT::i32),
4295 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4296 else
4297 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4298 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004299 // Merge the pieces into a single i64 value.
4300 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4301 }
Bob Wilson7117a912009-03-20 22:42:55 +00004302
Bob Wilson59b70ea2010-04-17 05:30:19 +00004303 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004304}
4305
Bob Wilson2e076c42009-06-22 23:27:02 +00004306/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004307/// Zero vectors are used to represent vector negation and in those cases
4308/// will be implemented with the NEON VNEG instruction. However, VNEG does
4309/// not support i64 elements, so sometimes the zero vectors will need to be
4310/// explicitly constructed. Regardless, use a canonical VMOV to create the
4311/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004312static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004313 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004314 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004315 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004316 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4317 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004318 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004319}
4320
Jim Grosbach624fcb22009-10-31 21:00:56 +00004321/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4322/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004323SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4324 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004325 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4326 EVT VT = Op.getValueType();
4327 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004328 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004329 SDValue ShOpLo = Op.getOperand(0);
4330 SDValue ShOpHi = Op.getOperand(1);
4331 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004332 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004333 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004334
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004335 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4336
Jim Grosbach624fcb22009-10-31 21:00:56 +00004337 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004338 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004339 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4340 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004341 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004342 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4343 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004344 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004345
4346 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004347 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4348 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004349 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004350 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004351 CCR, Cmp);
4352
4353 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004354 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004355}
4356
Jim Grosbach5d994042009-10-31 19:38:01 +00004357/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4358/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004359SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4360 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004361 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4362 EVT VT = Op.getValueType();
4363 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004364 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004365 SDValue ShOpLo = Op.getOperand(0);
4366 SDValue ShOpHi = Op.getOperand(1);
4367 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004368 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004369
4370 assert(Op.getOpcode() == ISD::SHL_PARTS);
4371 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004372 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004373 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4374 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004375 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004376 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4377 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4378
4379 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4380 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004381 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4382 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004383 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004384 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004385 CCR, Cmp);
4386
4387 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004388 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004389}
4390
Jim Grosbach535d3b42010-09-08 03:54:02 +00004391SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004392 SelectionDAG &DAG) const {
4393 // The rounding mode is in bits 23:22 of the FPSCR.
4394 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4395 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4396 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004397 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004398 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004399 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004400 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004401 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004402 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004403 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004404 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004405 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004406 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004407}
4408
Jim Grosbach8546ec92010-01-18 19:58:49 +00004409static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4410 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004411 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004412 EVT VT = N->getValueType(0);
4413 if (VT.isVector()) {
4414 assert(ST->hasNEON());
4415
4416 // Compute the least significant set bit: LSB = X & -X
4417 SDValue X = N->getOperand(0);
4418 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4419 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4420
4421 EVT ElemTy = VT.getVectorElementType();
4422
4423 if (ElemTy == MVT::i8) {
4424 // Compute with: cttz(x) = ctpop(lsb - 1)
4425 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4426 DAG.getTargetConstant(1, dl, ElemTy));
4427 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4428 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4429 }
4430
4431 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4432 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4433 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4434 unsigned NumBits = ElemTy.getSizeInBits();
4435 SDValue WidthMinus1 =
4436 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4437 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4438 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4439 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4440 }
4441
4442 // Compute with: cttz(x) = ctpop(lsb - 1)
4443
4444 // Since we can only compute the number of bits in a byte with vcnt.8, we
4445 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4446 // and i64.
4447
4448 // Compute LSB - 1.
4449 SDValue Bits;
4450 if (ElemTy == MVT::i64) {
4451 // Load constant 0xffff'ffff'ffff'ffff to register.
4452 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4453 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4454 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4455 } else {
4456 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4457 DAG.getTargetConstant(1, dl, ElemTy));
4458 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4459 }
4460
4461 // Count #bits with vcnt.8.
4462 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4463 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4464 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4465
4466 // Gather the #bits with vpaddl (pairwise add.)
4467 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4468 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4469 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4470 Cnt8);
4471 if (ElemTy == MVT::i16)
4472 return Cnt16;
4473
4474 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4475 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4476 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4477 Cnt16);
4478 if (ElemTy == MVT::i32)
4479 return Cnt32;
4480
4481 assert(ElemTy == MVT::i64);
4482 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4483 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4484 Cnt32);
4485 return Cnt64;
4486 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004487
4488 if (!ST->hasV6T2Ops())
4489 return SDValue();
4490
James Molloyb5640982015-11-13 16:05:22 +00004491 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00004492 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4493}
4494
Evan Chengb4eae132012-12-04 22:41:50 +00004495/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4496/// for each 16-bit element from operand, repeated. The basic idea is to
4497/// leverage vcnt to get the 8-bit counts, gather and add the results.
4498///
4499/// Trace for v4i16:
4500/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4501/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4502/// 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 +00004503/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004504/// [b0 b1 b2 b3 b4 b5 b6 b7]
4505/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4506/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4507/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4508static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4509 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004510 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004511
4512 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4513 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4514 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4515 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4516 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4517 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4518}
4519
4520/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4521/// bit-count for each 16-bit element from the operand. We need slightly
4522/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4523/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004524///
Evan Chengb4eae132012-12-04 22:41:50 +00004525/// Trace for v4i16:
4526/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4527/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4528/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4529/// v4i16:Extracted = [k0 k1 k2 k3 ]
4530static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4531 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004532 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004533
4534 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4535 if (VT.is64BitVector()) {
4536 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4537 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004538 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004539 } else {
4540 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004541 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004542 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4543 }
4544}
4545
4546/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4547/// bit-count for each 32-bit element from the operand. The idea here is
4548/// to split the vector into 16-bit elements, leverage the 16-bit count
4549/// routine, and then combine the results.
4550///
4551/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4552/// input = [v0 v1 ] (vi: 32-bit elements)
4553/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4554/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004555/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004556/// [k0 k1 k2 k3 ]
4557/// N1 =+[k1 k0 k3 k2 ]
4558/// [k0 k2 k1 k3 ]
4559/// N2 =+[k1 k3 k0 k2 ]
4560/// [k0 k2 k1 k3 ]
4561/// Extended =+[k1 k3 k0 k2 ]
4562/// [k0 k2 ]
4563/// Extracted=+[k1 k3 ]
4564///
4565static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4566 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004567 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004568
4569 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4570
4571 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4572 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4573 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4574 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4575 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4576
4577 if (VT.is64BitVector()) {
4578 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4579 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004580 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004581 } else {
4582 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004583 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004584 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4585 }
4586}
4587
4588static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4589 const ARMSubtarget *ST) {
4590 EVT VT = N->getValueType(0);
4591
4592 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004593 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4594 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004595 "Unexpected type for custom ctpop lowering");
4596
4597 if (VT.getVectorElementType() == MVT::i32)
4598 return lowerCTPOP32BitElements(N, DAG);
4599 else
4600 return lowerCTPOP16BitElements(N, DAG);
4601}
4602
Bob Wilson2e076c42009-06-22 23:27:02 +00004603static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4604 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004605 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004606 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004607
Bob Wilson7d471332010-11-18 21:16:28 +00004608 if (!VT.isVector())
4609 return SDValue();
4610
Bob Wilson2e076c42009-06-22 23:27:02 +00004611 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004612 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004613
Bob Wilson7d471332010-11-18 21:16:28 +00004614 // Left shifts translate directly to the vshiftu intrinsic.
4615 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004616 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004617 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4618 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004619 N->getOperand(0), N->getOperand(1));
4620
4621 assert((N->getOpcode() == ISD::SRA ||
4622 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4623
4624 // NEON uses the same intrinsics for both left and right shifts. For
4625 // right shifts, the shift amounts are negative, so negate the vector of
4626 // shift amounts.
4627 EVT ShiftVT = N->getOperand(1).getValueType();
4628 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4629 getZeroVector(ShiftVT, DAG, dl),
4630 N->getOperand(1));
4631 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4632 Intrinsic::arm_neon_vshifts :
4633 Intrinsic::arm_neon_vshiftu);
4634 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004635 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004636 N->getOperand(0), NegatedCount);
4637}
4638
4639static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4640 const ARMSubtarget *ST) {
4641 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004642 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004643
Eli Friedman682d8c12009-08-22 03:13:10 +00004644 // We can get here for a node like i32 = ISD::SHL i32, i64
4645 if (VT != MVT::i64)
4646 return SDValue();
4647
4648 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004649 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004650
Chris Lattnerf81d5882007-11-24 07:07:01 +00004651 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00004652 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00004653 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004654
Chris Lattnerf81d5882007-11-24 07:07:01 +00004655 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004656 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004657
Chris Lattnerf81d5882007-11-24 07:07:01 +00004658 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004659 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004660 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004661 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004662 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004663
Chris Lattnerf81d5882007-11-24 07:07:01 +00004664 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4665 // captures the result into a carry flag.
4666 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004667 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004668
Chris Lattnerf81d5882007-11-24 07:07:01 +00004669 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004670 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004671
Chris Lattnerf81d5882007-11-24 07:07:01 +00004672 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004673 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004674}
4675
Bob Wilson2e076c42009-06-22 23:27:02 +00004676static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4677 SDValue TmpOp0, TmpOp1;
4678 bool Invert = false;
4679 bool Swap = false;
4680 unsigned Opc = 0;
4681
4682 SDValue Op0 = Op.getOperand(0);
4683 SDValue Op1 = Op.getOperand(1);
4684 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004685 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004686 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004687 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004688 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004689
James Molloybf170092015-08-20 16:33:44 +00004690 if (CmpVT.getVectorElementType() == MVT::i64)
4691 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4692 // but it's possible that our operands are 64-bit but our result is 32-bit.
4693 // Bail in this case.
4694 return SDValue();
4695
Oliver Stannard51b1d462014-08-21 12:50:31 +00004696 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004697 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004698 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004699 case ISD::SETUNE:
4700 case ISD::SETNE: Invert = true; // Fallthrough
4701 case ISD::SETOEQ:
4702 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4703 case ISD::SETOLT:
4704 case ISD::SETLT: Swap = true; // Fallthrough
4705 case ISD::SETOGT:
4706 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4707 case ISD::SETOLE:
4708 case ISD::SETLE: Swap = true; // Fallthrough
4709 case ISD::SETOGE:
4710 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4711 case ISD::SETUGE: Swap = true; // Fallthrough
4712 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4713 case ISD::SETUGT: Swap = true; // Fallthrough
4714 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4715 case ISD::SETUEQ: Invert = true; // Fallthrough
4716 case ISD::SETONE:
4717 // Expand this to (OLT | OGT).
4718 TmpOp0 = Op0;
4719 TmpOp1 = Op1;
4720 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004721 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4722 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004723 break;
4724 case ISD::SETUO: Invert = true; // Fallthrough
4725 case ISD::SETO:
4726 // Expand this to (OLT | OGE).
4727 TmpOp0 = Op0;
4728 TmpOp1 = Op1;
4729 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004730 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4731 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004732 break;
4733 }
4734 } else {
4735 // Integer comparisons.
4736 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004737 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004738 case ISD::SETNE: Invert = true;
4739 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4740 case ISD::SETLT: Swap = true;
4741 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4742 case ISD::SETLE: Swap = true;
4743 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4744 case ISD::SETULT: Swap = true;
4745 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4746 case ISD::SETULE: Swap = true;
4747 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4748 }
4749
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004750 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004751 if (Opc == ARMISD::VCEQ) {
4752
4753 SDValue AndOp;
4754 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4755 AndOp = Op0;
4756 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4757 AndOp = Op1;
4758
4759 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004760 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004761 AndOp = AndOp.getOperand(0);
4762
4763 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4764 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004765 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4766 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004767 Invert = !Invert;
4768 }
4769 }
4770 }
4771
4772 if (Swap)
4773 std::swap(Op0, Op1);
4774
Owen Andersonc7baee32010-11-08 23:21:22 +00004775 // If one of the operands is a constant vector zero, attempt to fold the
4776 // comparison to a specialized compare-against-zero form.
4777 SDValue SingleOp;
4778 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4779 SingleOp = Op0;
4780 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4781 if (Opc == ARMISD::VCGE)
4782 Opc = ARMISD::VCLEZ;
4783 else if (Opc == ARMISD::VCGT)
4784 Opc = ARMISD::VCLTZ;
4785 SingleOp = Op1;
4786 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004787
Owen Andersonc7baee32010-11-08 23:21:22 +00004788 SDValue Result;
4789 if (SingleOp.getNode()) {
4790 switch (Opc) {
4791 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004792 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004793 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004794 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004795 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004796 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004797 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004798 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004799 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004800 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004801 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004802 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004803 }
4804 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004805 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004806 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004807
Tim Northover45aa89c2015-02-08 00:50:47 +00004808 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4809
Bob Wilson2e076c42009-06-22 23:27:02 +00004810 if (Invert)
4811 Result = DAG.getNOT(dl, Result, VT);
4812
4813 return Result;
4814}
4815
Bob Wilson5b2b5042010-06-14 22:19:57 +00004816/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4817/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004818/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004819static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4820 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004821 SDLoc dl, EVT &VT, bool is128Bits,
4822 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004823 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004824
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004825 // SplatBitSize is set to the smallest size that splats the vector, so a
4826 // zero vector will always have SplatBitSize == 8. However, NEON modified
4827 // immediate instructions others than VMOV do not support the 8-bit encoding
4828 // of a zero vector, and the default encoding of zero is supposed to be the
4829 // 32-bit version.
4830 if (SplatBits == 0)
4831 SplatBitSize = 32;
4832
Bob Wilson2e076c42009-06-22 23:27:02 +00004833 switch (SplatBitSize) {
4834 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004835 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004836 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004837 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004838 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004839 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004840 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004841 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004842 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004843
4844 case 16:
4845 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004846 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004847 if ((SplatBits & ~0xff) == 0) {
4848 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004849 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004850 Imm = SplatBits;
4851 break;
4852 }
4853 if ((SplatBits & ~0xff00) == 0) {
4854 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004855 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004856 Imm = SplatBits >> 8;
4857 break;
4858 }
4859 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004860
4861 case 32:
4862 // NEON's 32-bit VMOV supports splat values where:
4863 // * only one byte is nonzero, or
4864 // * the least significant byte is 0xff and the second byte is nonzero, or
4865 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004866 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004867 if ((SplatBits & ~0xff) == 0) {
4868 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004869 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004870 Imm = SplatBits;
4871 break;
4872 }
4873 if ((SplatBits & ~0xff00) == 0) {
4874 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004875 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004876 Imm = SplatBits >> 8;
4877 break;
4878 }
4879 if ((SplatBits & ~0xff0000) == 0) {
4880 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004881 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004882 Imm = SplatBits >> 16;
4883 break;
4884 }
4885 if ((SplatBits & ~0xff000000) == 0) {
4886 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004887 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004888 Imm = SplatBits >> 24;
4889 break;
4890 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004891
Owen Andersona4076922010-11-05 21:57:54 +00004892 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4893 if (type == OtherModImm) return SDValue();
4894
Bob Wilson2e076c42009-06-22 23:27:02 +00004895 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004896 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4897 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004898 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004899 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004900 break;
4901 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004902
4903 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004904 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4905 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004906 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004907 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004908 break;
4909 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004910
4911 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4912 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4913 // VMOV.I32. A (very) minor optimization would be to replicate the value
4914 // and fall through here to test for a valid 64-bit splat. But, then the
4915 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004916 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004917
4918 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004919 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004920 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004921 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004922 uint64_t BitMask = 0xff;
4923 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004924 unsigned ImmMask = 1;
4925 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004926 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004927 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004928 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004929 Imm |= ImmMask;
4930 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004931 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004932 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004933 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004934 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004935 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004936
Mehdi Aminiffc14022015-07-08 01:00:38 +00004937 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00004938 // swap higher and lower 32 bit word
4939 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4940
Bob Wilson6eae5202010-06-11 21:34:50 +00004941 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004942 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004943 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004944 break;
4945 }
4946
Bob Wilson6eae5202010-06-11 21:34:50 +00004947 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004948 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004949 }
4950
Bob Wilsona3f19012010-07-13 21:16:48 +00004951 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004952 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004953}
4954
Lang Hames591cdaf2012-03-29 21:56:11 +00004955SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4956 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004957 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004958 return SDValue();
4959
Tim Northoverf79c3a52013-08-20 08:57:11 +00004960 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004961 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004962
Oliver Stannard51b1d462014-08-21 12:50:31 +00004963 // Use the default (constant pool) lowering for double constants when we have
4964 // an SP-only FPU
4965 if (IsDouble && Subtarget->isFPOnlySP())
4966 return SDValue();
4967
Lang Hames591cdaf2012-03-29 21:56:11 +00004968 // Try splatting with a VMOV.f32...
4969 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004970 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4971
Lang Hames591cdaf2012-03-29 21:56:11 +00004972 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004973 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4974 // We have code in place to select a valid ConstantFP already, no need to
4975 // do any mangling.
4976 return Op;
4977 }
4978
4979 // It's a float and we are trying to use NEON operations where
4980 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004981 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004982 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004983 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4984 NewVal);
4985 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004986 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004987 }
4988
Tim Northoverf79c3a52013-08-20 08:57:11 +00004989 // The rest of our options are NEON only, make sure that's allowed before
4990 // proceeding..
4991 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4992 return SDValue();
4993
Lang Hames591cdaf2012-03-29 21:56:11 +00004994 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004995 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4996
4997 // It wouldn't really be worth bothering for doubles except for one very
4998 // important value, which does happen to match: 0.0. So make sure we don't do
4999 // anything stupid.
5000 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5001 return SDValue();
5002
5003 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005004 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5005 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005006 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005007 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005008 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5009 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005010 if (IsDouble)
5011 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5012
5013 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005014 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5015 VecConstant);
5016 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005017 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005018 }
5019
5020 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005021 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00005022 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005023 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005024 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005025 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005026
5027 if (IsDouble)
5028 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5029
5030 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005031 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5032 VecConstant);
5033 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005034 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005035 }
5036
5037 return SDValue();
5038}
5039
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005040// check if an VEXT instruction can handle the shuffle mask when the
5041// vector sources of the shuffle are the same.
5042static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5043 unsigned NumElts = VT.getVectorNumElements();
5044
5045 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5046 if (M[0] < 0)
5047 return false;
5048
5049 Imm = M[0];
5050
5051 // If this is a VEXT shuffle, the immediate value is the index of the first
5052 // element. The other shuffle indices must be the successive elements after
5053 // the first one.
5054 unsigned ExpectedElt = Imm;
5055 for (unsigned i = 1; i < NumElts; ++i) {
5056 // Increment the expected index. If it wraps around, just follow it
5057 // back to index zero and keep going.
5058 ++ExpectedElt;
5059 if (ExpectedElt == NumElts)
5060 ExpectedElt = 0;
5061
5062 if (M[i] < 0) continue; // ignore UNDEF indices
5063 if (ExpectedElt != static_cast<unsigned>(M[i]))
5064 return false;
5065 }
5066
5067 return true;
5068}
5069
Lang Hames591cdaf2012-03-29 21:56:11 +00005070
Benjamin Kramer339ced42012-01-15 13:16:05 +00005071static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005072 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005073 unsigned NumElts = VT.getVectorNumElements();
5074 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00005075
5076 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5077 if (M[0] < 0)
5078 return false;
5079
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005080 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00005081
5082 // If this is a VEXT shuffle, the immediate value is the index of the first
5083 // element. The other shuffle indices must be the successive elements after
5084 // the first one.
5085 unsigned ExpectedElt = Imm;
5086 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005087 // Increment the expected index. If it wraps around, it may still be
5088 // a VEXT but the source vectors must be swapped.
5089 ExpectedElt += 1;
5090 if (ExpectedElt == NumElts * 2) {
5091 ExpectedElt = 0;
5092 ReverseVEXT = true;
5093 }
5094
Bob Wilson411dfad2010-08-17 05:54:34 +00005095 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005096 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005097 return false;
5098 }
5099
5100 // Adjust the index value if the source operands will be swapped.
5101 if (ReverseVEXT)
5102 Imm -= NumElts;
5103
Bob Wilson32cd8552009-08-19 17:03:43 +00005104 return true;
5105}
5106
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005107/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5108/// instruction with the specified blocksize. (The order of the elements
5109/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005110static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005111 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5112 "Only possible block sizes for VREV are: 16, 32, 64");
5113
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005114 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005115 if (EltSz == 64)
5116 return false;
5117
5118 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005119 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005120 // If the first shuffle index is UNDEF, be optimistic.
5121 if (M[0] < 0)
5122 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005123
5124 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5125 return false;
5126
5127 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005128 if (M[i] < 0) continue; // ignore UNDEF indices
5129 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005130 return false;
5131 }
5132
5133 return true;
5134}
5135
Benjamin Kramer339ced42012-01-15 13:16:05 +00005136static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005137 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5138 // range, then 0 is placed into the resulting vector. So pretty much any mask
5139 // of 8 elements can work here.
5140 return VT == MVT::v8i8 && M.size() == 8;
5141}
5142
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005143// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5144// checking that pairs of elements in the shuffle mask represent the same index
5145// in each vector, incrementing the expected index by 2 at each step.
5146// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5147// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5148// v2={e,f,g,h}
5149// WhichResult gives the offset for each element in the mask based on which
5150// of the two results it belongs to.
5151//
5152// The transpose can be represented either as:
5153// result1 = shufflevector v1, v2, result1_shuffle_mask
5154// result2 = shufflevector v1, v2, result2_shuffle_mask
5155// where v1/v2 and the shuffle masks have the same number of elements
5156// (here WhichResult (see below) indicates which result is being checked)
5157//
5158// or as:
5159// results = shufflevector v1, v2, shuffle_mask
5160// where both results are returned in one vector and the shuffle mask has twice
5161// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5162// want to check the low half and high half of the shuffle mask as if it were
5163// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005164static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005165 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5166 if (EltSz == 64)
5167 return false;
5168
Bob Wilsona7062312009-08-21 20:54:19 +00005169 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005170 if (M.size() != NumElts && M.size() != NumElts*2)
5171 return false;
5172
James Molloy8c995a92015-09-10 08:42:28 +00005173 // If the mask is twice as long as the input vector then we need to check the
5174 // upper and lower parts of the mask with a matching value for WhichResult
5175 // FIXME: A mask with only even values will be rejected in case the first
5176 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5177 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005178 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005179 if (M.size() == NumElts * 2)
5180 WhichResult = i / NumElts;
5181 else
5182 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005183 for (unsigned j = 0; j < NumElts; j += 2) {
5184 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5185 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5186 return false;
5187 }
Bob Wilsona7062312009-08-21 20:54:19 +00005188 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005189
5190 if (M.size() == NumElts*2)
5191 WhichResult = 0;
5192
Bob Wilsona7062312009-08-21 20:54:19 +00005193 return true;
5194}
5195
Bob Wilson0bbd3072009-12-03 06:40:55 +00005196/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5197/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5198/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005199static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005200 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5201 if (EltSz == 64)
5202 return false;
5203
5204 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005205 if (M.size() != NumElts && M.size() != NumElts*2)
5206 return false;
5207
5208 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005209 if (M.size() == NumElts * 2)
5210 WhichResult = i / NumElts;
5211 else
5212 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005213 for (unsigned j = 0; j < NumElts; j += 2) {
5214 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5215 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5216 return false;
5217 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005218 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005219
5220 if (M.size() == NumElts*2)
5221 WhichResult = 0;
5222
Bob Wilson0bbd3072009-12-03 06:40:55 +00005223 return true;
5224}
5225
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005226// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5227// that the mask elements are either all even and in steps of size 2 or all odd
5228// and in steps of size 2.
5229// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5230// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5231// v2={e,f,g,h}
5232// Requires similar checks to that of isVTRNMask with
5233// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005234static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005235 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5236 if (EltSz == 64)
5237 return false;
5238
Bob Wilsona7062312009-08-21 20:54:19 +00005239 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005240 if (M.size() != NumElts && M.size() != NumElts*2)
5241 return false;
5242
5243 for (unsigned i = 0; i < M.size(); i += NumElts) {
5244 WhichResult = M[i] == 0 ? 0 : 1;
5245 for (unsigned j = 0; j < NumElts; ++j) {
5246 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5247 return false;
5248 }
Bob Wilsona7062312009-08-21 20:54:19 +00005249 }
5250
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005251 if (M.size() == NumElts*2)
5252 WhichResult = 0;
5253
Bob Wilsona7062312009-08-21 20:54:19 +00005254 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005255 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005256 return false;
5257
5258 return true;
5259}
5260
Bob Wilson0bbd3072009-12-03 06:40:55 +00005261/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5262/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5263/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005264static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005265 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5266 if (EltSz == 64)
5267 return false;
5268
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005269 unsigned NumElts = VT.getVectorNumElements();
5270 if (M.size() != NumElts && M.size() != NumElts*2)
5271 return false;
5272
5273 unsigned Half = NumElts / 2;
5274 for (unsigned i = 0; i < M.size(); i += NumElts) {
5275 WhichResult = M[i] == 0 ? 0 : 1;
5276 for (unsigned j = 0; j < NumElts; j += Half) {
5277 unsigned Idx = WhichResult;
5278 for (unsigned k = 0; k < Half; ++k) {
5279 int MIdx = M[i + j + k];
5280 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5281 return false;
5282 Idx += 2;
5283 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005284 }
5285 }
5286
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005287 if (M.size() == NumElts*2)
5288 WhichResult = 0;
5289
Bob Wilson0bbd3072009-12-03 06:40:55 +00005290 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5291 if (VT.is64BitVector() && EltSz == 32)
5292 return false;
5293
5294 return true;
5295}
5296
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005297// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5298// that pairs of elements of the shufflemask represent the same index in each
5299// vector incrementing sequentially through the vectors.
5300// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5301// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5302// v2={e,f,g,h}
5303// Requires similar checks to that of isVTRNMask with respect the how results
5304// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005305static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005306 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5307 if (EltSz == 64)
5308 return false;
5309
Bob Wilsona7062312009-08-21 20:54:19 +00005310 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005311 if (M.size() != NumElts && M.size() != NumElts*2)
5312 return false;
5313
5314 for (unsigned i = 0; i < M.size(); i += NumElts) {
5315 WhichResult = M[i] == 0 ? 0 : 1;
5316 unsigned Idx = WhichResult * NumElts / 2;
5317 for (unsigned j = 0; j < NumElts; j += 2) {
5318 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5319 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5320 return false;
5321 Idx += 1;
5322 }
Bob Wilsona7062312009-08-21 20:54:19 +00005323 }
5324
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005325 if (M.size() == NumElts*2)
5326 WhichResult = 0;
5327
Bob Wilsona7062312009-08-21 20:54:19 +00005328 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005329 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005330 return false;
5331
5332 return true;
5333}
5334
Bob Wilson0bbd3072009-12-03 06:40:55 +00005335/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5336/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5337/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005338static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005339 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5340 if (EltSz == 64)
5341 return false;
5342
5343 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005344 if (M.size() != NumElts && M.size() != NumElts*2)
5345 return false;
5346
5347 for (unsigned i = 0; i < M.size(); i += NumElts) {
5348 WhichResult = M[i] == 0 ? 0 : 1;
5349 unsigned Idx = WhichResult * NumElts / 2;
5350 for (unsigned j = 0; j < NumElts; j += 2) {
5351 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5352 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5353 return false;
5354 Idx += 1;
5355 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005356 }
5357
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005358 if (M.size() == NumElts*2)
5359 WhichResult = 0;
5360
Bob Wilson0bbd3072009-12-03 06:40:55 +00005361 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5362 if (VT.is64BitVector() && EltSz == 32)
5363 return false;
5364
5365 return true;
5366}
5367
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005368/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5369/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5370static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5371 unsigned &WhichResult,
5372 bool &isV_UNDEF) {
5373 isV_UNDEF = false;
5374 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5375 return ARMISD::VTRN;
5376 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5377 return ARMISD::VUZP;
5378 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5379 return ARMISD::VZIP;
5380
5381 isV_UNDEF = true;
5382 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5383 return ARMISD::VTRN;
5384 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5385 return ARMISD::VUZP;
5386 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5387 return ARMISD::VZIP;
5388
5389 return 0;
5390}
5391
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005392/// \return true if this is a reverse operation on an vector.
5393static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5394 unsigned NumElts = VT.getVectorNumElements();
5395 // Make sure the mask has the right size.
5396 if (NumElts != M.size())
5397 return false;
5398
5399 // Look for <15, ..., 3, -1, 1, 0>.
5400 for (unsigned i = 0; i != NumElts; ++i)
5401 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5402 return false;
5403
5404 return true;
5405}
5406
Dale Johannesen2bff5052010-07-29 20:10:08 +00005407// If N is an integer constant that can be moved into a register in one
5408// instruction, return an SDValue of such a constant (will become a MOV
5409// instruction). Otherwise return null.
5410static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005411 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005412 uint64_t Val;
5413 if (!isa<ConstantSDNode>(N))
5414 return SDValue();
5415 Val = cast<ConstantSDNode>(N)->getZExtValue();
5416
5417 if (ST->isThumb1Only()) {
5418 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005419 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005420 } else {
5421 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005422 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005423 }
5424 return SDValue();
5425}
5426
Bob Wilson2e076c42009-06-22 23:27:02 +00005427// If this is a case we can't handle, return null and let the default
5428// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005429SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5430 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005431 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005432 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005433 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005434
5435 APInt SplatBits, SplatUndef;
5436 unsigned SplatBitSize;
5437 bool HasAnyUndefs;
5438 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005439 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005440 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005441 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005442 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005443 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005444 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005445 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005446 if (Val.getNode()) {
5447 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005448 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005449 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005450
5451 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005452 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005453 Val = isNEONModifiedImm(NegatedImm,
5454 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005455 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005456 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005457 if (Val.getNode()) {
5458 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005459 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005460 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005461
5462 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005463 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005464 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005465 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005466 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005467 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5468 }
5469 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005470 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005471 }
5472
Bob Wilson91fdf682010-05-22 00:23:12 +00005473 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005474 //
5475 // As an optimisation, even if more than one value is used it may be more
5476 // profitable to splat with one value then change some lanes.
5477 //
5478 // Heuristically we decide to do this if the vector has a "dominant" value,
5479 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005480 unsigned NumElts = VT.getVectorNumElements();
5481 bool isOnlyLowElement = true;
5482 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005483 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005484 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005485
5486 // Map of the number of times a particular SDValue appears in the
5487 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005488 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005489 SDValue Value;
5490 for (unsigned i = 0; i < NumElts; ++i) {
5491 SDValue V = Op.getOperand(i);
5492 if (V.getOpcode() == ISD::UNDEF)
5493 continue;
5494 if (i > 0)
5495 isOnlyLowElement = false;
5496 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5497 isConstant = false;
5498
James Molloy49bdbce2012-09-06 09:55:02 +00005499 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005500 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005501
James Molloy49bdbce2012-09-06 09:55:02 +00005502 // Is this value dominant? (takes up more than half of the lanes)
5503 if (++Count > (NumElts / 2)) {
5504 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005505 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005506 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005507 }
James Molloy49bdbce2012-09-06 09:55:02 +00005508 if (ValueCounts.size() != 1)
5509 usesOnlyOneValue = false;
5510 if (!Value.getNode() && ValueCounts.size() > 0)
5511 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005512
James Molloy49bdbce2012-09-06 09:55:02 +00005513 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005514 return DAG.getUNDEF(VT);
5515
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005516 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5517 // Keep going if we are hitting this case.
5518 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005519 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5520
Dale Johannesen2bff5052010-07-29 20:10:08 +00005521 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5522
Dale Johannesen710a2d92010-10-19 20:00:17 +00005523 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5524 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005525 if (hasDominantValue && EltSize <= 32) {
5526 if (!isConstant) {
5527 SDValue N;
5528
5529 // If we are VDUPing a value that comes directly from a vector, that will
5530 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005531 // just use VDUPLANE. We can only do this if the lane being extracted
5532 // is at a constant index, as the VDUP from lane instructions only have
5533 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005534 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005535 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00005536 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005537 // We need to create a new undef vector to use for the VDUPLANE if the
5538 // size of the vector from which we get the value is different than the
5539 // size of the vector that we need to create. We will insert the element
5540 // such that the register coalescer will remove unnecessary copies.
5541 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00005542 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5543 VT.getVectorNumElements();
5544 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5545 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005546 Value, DAG.getConstant(index, dl, MVT::i32)),
5547 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005548 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005549 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005550 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005551 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005552 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5553
5554 if (!usesOnlyOneValue) {
5555 // The dominant value was splatted as 'N', but we now have to insert
5556 // all differing elements.
5557 for (unsigned I = 0; I < NumElts; ++I) {
5558 if (Op.getOperand(I) == Value)
5559 continue;
5560 SmallVector<SDValue, 3> Ops;
5561 Ops.push_back(N);
5562 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005563 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005564 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005565 }
5566 }
5567 return N;
5568 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005569 if (VT.getVectorElementType().isFloatingPoint()) {
5570 SmallVector<SDValue, 8> Ops;
5571 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005572 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005573 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005574 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005575 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005576 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5577 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005578 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005579 }
James Molloy49bdbce2012-09-06 09:55:02 +00005580 if (usesOnlyOneValue) {
5581 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5582 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005583 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005584 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005585 }
5586
5587 // If all elements are constants and the case above didn't get hit, fall back
5588 // to the default expansion, which will generate a load from the constant
5589 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005590 if (isConstant)
5591 return SDValue();
5592
Bob Wilson6f2b8962011-01-07 21:37:30 +00005593 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5594 if (NumElts >= 4) {
5595 SDValue shuffle = ReconstructShuffle(Op, DAG);
5596 if (shuffle != SDValue())
5597 return shuffle;
5598 }
5599
Bob Wilson91fdf682010-05-22 00:23:12 +00005600 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005601 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5602 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005603 if (EltSize >= 32) {
5604 // Do the expansion with floating-point types, since that is what the VFP
5605 // registers are defined to use, and since i64 is not legal.
5606 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5607 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005608 SmallVector<SDValue, 8> Ops;
5609 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005610 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005611 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005612 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005613 }
5614
Jim Grosbach24e102a2013-07-08 18:18:52 +00005615 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5616 // know the default expansion would otherwise fall back on something even
5617 // worse. For a vector with one or two non-undef values, that's
5618 // scalar_to_vector for the elements followed by a shuffle (provided the
5619 // shuffle is valid for the target) and materialization element by element
5620 // on the stack followed by a load for everything else.
5621 if (!isConstant && !usesOnlyOneValue) {
5622 SDValue Vec = DAG.getUNDEF(VT);
5623 for (unsigned i = 0 ; i < NumElts; ++i) {
5624 SDValue V = Op.getOperand(i);
5625 if (V.getOpcode() == ISD::UNDEF)
5626 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005627 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005628 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5629 }
5630 return Vec;
5631 }
5632
Bob Wilson2e076c42009-06-22 23:27:02 +00005633 return SDValue();
5634}
5635
Bob Wilson6f2b8962011-01-07 21:37:30 +00005636// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005637// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005638SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5639 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005640 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005641 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005642 EVT VT = Op.getValueType();
5643 unsigned NumElts = VT.getVectorNumElements();
5644
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005645 struct ShuffleSourceInfo {
5646 SDValue Vec;
5647 unsigned MinElt;
5648 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005649
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005650 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5651 // be compatible with the shuffle we intend to construct. As a result
5652 // ShuffleVec will be some sliding window into the original Vec.
5653 SDValue ShuffleVec;
5654
5655 // Code should guarantee that element i in Vec starts at element "WindowBase
5656 // + i * WindowScale in ShuffleVec".
5657 int WindowBase;
5658 int WindowScale;
5659
5660 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5661 ShuffleSourceInfo(SDValue Vec)
5662 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5663 WindowScale(1) {}
5664 };
5665
5666 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5667 // node.
5668 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005669 for (unsigned i = 0; i < NumElts; ++i) {
5670 SDValue V = Op.getOperand(i);
5671 if (V.getOpcode() == ISD::UNDEF)
5672 continue;
5673 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5674 // A shuffle can only come from building a vector from various
5675 // elements of other vectors.
5676 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005677 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5678 // Furthermore, shuffles require a constant mask, whereas extractelts
5679 // accept variable indices.
5680 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005681 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005682
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005683 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005684 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005685 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5686 if (Source == Sources.end())
5687 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005688
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005689 // Update the minimum and maximum lane number seen.
5690 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5691 Source->MinElt = std::min(Source->MinElt, EltNo);
5692 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005693 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005694
Bob Wilson6f2b8962011-01-07 21:37:30 +00005695 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005696 // are involved.
5697 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005698 return SDValue();
5699
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005700 // Find out the smallest element size among result and two sources, and use
5701 // it as element size to build the shuffle_vector.
5702 EVT SmallestEltTy = VT.getVectorElementType();
5703 for (auto &Source : Sources) {
5704 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5705 if (SrcEltTy.bitsLT(SmallestEltTy))
5706 SmallestEltTy = SrcEltTy;
5707 }
5708 unsigned ResMultiplier =
5709 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5710 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5711 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005712
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005713 // If the source vector is too wide or too narrow, we may nevertheless be able
5714 // to construct a compatible shuffle either by concatenating it with UNDEF or
5715 // extracting a suitable range of elements.
5716 for (auto &Src : Sources) {
5717 EVT SrcVT = Src.ShuffleVec.getValueType();
5718
5719 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005720 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005721
5722 // This stage of the search produces a source with the same element type as
5723 // the original, but with a total width matching the BUILD_VECTOR output.
5724 EVT EltVT = SrcVT.getVectorElementType();
5725 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5726 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5727
5728 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5729 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5730 return SDValue();
5731 // We can pad out the smaller vector for free, so if it's part of a
5732 // shuffle...
5733 Src.ShuffleVec =
5734 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5735 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5736 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005737 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005738
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005739 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5740 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005741
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005742 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005743 // Span too large for a VEXT to cope
5744 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005745 }
5746
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005747 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005748 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005749 Src.ShuffleVec =
5750 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5751 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5752 Src.WindowBase = -NumSrcElts;
5753 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005754 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005755 Src.ShuffleVec =
5756 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5757 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005758 } else {
5759 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005760 SDValue VEXTSrc1 =
5761 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5762 DAG.getConstant(0, dl, MVT::i32));
5763 SDValue VEXTSrc2 =
5764 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5765 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005766
5767 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5768 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005769 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005770 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005771 }
5772 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005773
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005774 // Another possible incompatibility occurs from the vector element types. We
5775 // can fix this by bitcasting the source vectors to the same type we intend
5776 // for the shuffle.
5777 for (auto &Src : Sources) {
5778 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5779 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005780 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005781 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5782 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5783 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5784 Src.WindowBase *= Src.WindowScale;
5785 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005786
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005787 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005788 DEBUG(
5789 for (auto Src : Sources)
5790 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5791 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005792
5793 // The stars all align, our next step is to produce the mask for the shuffle.
5794 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5795 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5796 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5797 SDValue Entry = Op.getOperand(i);
5798 if (Entry.getOpcode() == ISD::UNDEF)
5799 continue;
5800
5801 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5802 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5803
5804 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5805 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5806 // segment.
5807 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5808 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5809 VT.getVectorElementType().getSizeInBits());
5810 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5811
5812 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5813 // starting at the appropriate offset.
5814 int *LaneMask = &Mask[i * ResMultiplier];
5815
5816 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5817 ExtractBase += NumElts * (Src - Sources.begin());
5818 for (int j = 0; j < LanesDefined; ++j)
5819 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005820 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005821
Bob Wilson6f2b8962011-01-07 21:37:30 +00005822 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005823 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5824 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005825
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005826 // We can't handle more than two sources. This should have already
5827 // been checked before this point.
5828 assert(Sources.size() <= 2 && "Too many sources!");
5829
5830 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5831 for (unsigned i = 0; i < Sources.size(); ++i)
5832 ShuffleOps[i] = Sources[i].ShuffleVec;
5833
5834 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5835 ShuffleOps[1], &Mask[0]);
5836 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005837}
5838
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005839/// isShuffleMaskLegal - Targets can use this to indicate that they only
5840/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5841/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5842/// are assumed to be legal.
5843bool
5844ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5845 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005846 if (VT.getVectorNumElements() == 4 &&
5847 (VT.is128BitVector() || VT.is64BitVector())) {
5848 unsigned PFIndexes[4];
5849 for (unsigned i = 0; i != 4; ++i) {
5850 if (M[i] < 0)
5851 PFIndexes[i] = 8;
5852 else
5853 PFIndexes[i] = M[i];
5854 }
5855
5856 // Compute the index in the perfect shuffle table.
5857 unsigned PFTableIndex =
5858 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5859 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5860 unsigned Cost = (PFEntry >> 30);
5861
5862 if (Cost <= 4)
5863 return true;
5864 }
5865
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005866 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005867 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005868
Bob Wilson846bd792010-06-07 23:53:38 +00005869 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5870 return (EltSize >= 32 ||
5871 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005872 isVREVMask(M, VT, 64) ||
5873 isVREVMask(M, VT, 32) ||
5874 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005875 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005876 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005877 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005878 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005879}
5880
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005881/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5882/// the specified operations to build the shuffle.
5883static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5884 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005885 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005886 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5887 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5888 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5889
5890 enum {
5891 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5892 OP_VREV,
5893 OP_VDUP0,
5894 OP_VDUP1,
5895 OP_VDUP2,
5896 OP_VDUP3,
5897 OP_VEXT1,
5898 OP_VEXT2,
5899 OP_VEXT3,
5900 OP_VUZPL, // VUZP, left result
5901 OP_VUZPR, // VUZP, right result
5902 OP_VZIPL, // VZIP, left result
5903 OP_VZIPR, // VZIP, right result
5904 OP_VTRNL, // VTRN, left result
5905 OP_VTRNR // VTRN, right result
5906 };
5907
5908 if (OpNum == OP_COPY) {
5909 if (LHSID == (1*9+2)*9+3) return LHS;
5910 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5911 return RHS;
5912 }
5913
5914 SDValue OpLHS, OpRHS;
5915 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5916 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5917 EVT VT = OpLHS.getValueType();
5918
5919 switch (OpNum) {
5920 default: llvm_unreachable("Unknown shuffle opcode!");
5921 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005922 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005923 if (VT.getVectorElementType() == MVT::i32 ||
5924 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005925 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5926 // vrev <4 x i16> -> VREV32
5927 if (VT.getVectorElementType() == MVT::i16)
5928 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5929 // vrev <4 x i8> -> VREV16
5930 assert(VT.getVectorElementType() == MVT::i8);
5931 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005932 case OP_VDUP0:
5933 case OP_VDUP1:
5934 case OP_VDUP2:
5935 case OP_VDUP3:
5936 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005937 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005938 case OP_VEXT1:
5939 case OP_VEXT2:
5940 case OP_VEXT3:
5941 return DAG.getNode(ARMISD::VEXT, dl, VT,
5942 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005943 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005944 case OP_VUZPL:
5945 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005946 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005947 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5948 case OP_VZIPL:
5949 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005950 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005951 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5952 case OP_VTRNL:
5953 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005954 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5955 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005956 }
5957}
5958
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005959static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005960 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005961 SelectionDAG &DAG) {
5962 // Check to see if we can use the VTBL instruction.
5963 SDValue V1 = Op.getOperand(0);
5964 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005965 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005966
5967 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005968 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005969 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005970 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005971
5972 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5973 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005974 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005975
Owen Anderson77aa2662011-04-05 21:48:57 +00005976 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005977 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005978}
5979
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005980static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5981 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005982 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005983 SDValue OpLHS = Op.getOperand(0);
5984 EVT VT = OpLHS.getValueType();
5985
5986 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5987 "Expect an v8i16/v16i8 type");
5988 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5989 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5990 // extract the first 8 bytes into the top double word and the last 8 bytes
5991 // into the bottom double word. The v8i16 case is similar.
5992 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5993 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005994 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005995}
5996
Bob Wilson2e076c42009-06-22 23:27:02 +00005997static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005998 SDValue V1 = Op.getOperand(0);
5999 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006000 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00006001 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006002 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00006003
Bob Wilsonc6800b52009-08-13 02:13:04 +00006004 // Convert shuffles that are directly supported on NEON to target-specific
6005 // DAG nodes, instead of keeping them as shuffles and matching them again
6006 // during code selection. This is more efficient and avoids the possibility
6007 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00006008 // FIXME: floating-point vectors should be canonicalized to integer vectors
6009 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006010 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006011
Bob Wilson846bd792010-06-07 23:53:38 +00006012 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6013 if (EltSize <= 32) {
6014 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
6015 int Lane = SVN->getSplatIndex();
6016 // If this is undef splat, generate it via "just" vdup, if possible.
6017 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00006018
Dan Gohman198b7ff2011-11-03 21:49:52 +00006019 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00006020 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6021 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6022 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006023 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6024 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6025 // reaches it).
6026 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6027 !isa<ConstantSDNode>(V1.getOperand(0))) {
6028 bool IsScalarToVector = true;
6029 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
6030 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
6031 IsScalarToVector = false;
6032 break;
6033 }
6034 if (IsScalarToVector)
6035 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6036 }
Bob Wilson846bd792010-06-07 23:53:38 +00006037 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006038 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00006039 }
Bob Wilson846bd792010-06-07 23:53:38 +00006040
6041 bool ReverseVEXT;
6042 unsigned Imm;
6043 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
6044 if (ReverseVEXT)
6045 std::swap(V1, V2);
6046 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006047 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00006048 }
6049
6050 if (isVREVMask(ShuffleMask, VT, 64))
6051 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
6052 if (isVREVMask(ShuffleMask, VT, 32))
6053 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
6054 if (isVREVMask(ShuffleMask, VT, 16))
6055 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
6056
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006057 if (V2->getOpcode() == ISD::UNDEF &&
6058 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
6059 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006060 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006061 }
6062
Bob Wilson846bd792010-06-07 23:53:38 +00006063 // Check for Neon shuffles that modify both input vectors in place.
6064 // If both results are used, i.e., if there are two shuffles with the same
6065 // source operands and with masks corresponding to both results of one of
6066 // these operations, DAG memoization will ensure that a single node is
6067 // used for both shuffles.
6068 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006069 bool isV_UNDEF;
6070 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6071 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
6072 if (isV_UNDEF)
6073 V2 = V1;
6074 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
6075 .getValue(WhichResult);
6076 }
Bob Wilson846bd792010-06-07 23:53:38 +00006077
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006078 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
6079 // shuffles that produce a result larger than their operands with:
6080 // shuffle(concat(v1, undef), concat(v2, undef))
6081 // ->
6082 // shuffle(concat(v1, v2), undef)
6083 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
6084 //
6085 // This is useful in the general case, but there are special cases where
6086 // native shuffles produce larger results: the two-result ops.
6087 //
6088 // Look through the concat when lowering them:
6089 // shuffle(concat(v1, v2), undef)
6090 // ->
6091 // concat(VZIP(v1, v2):0, :1)
6092 //
6093 if (V1->getOpcode() == ISD::CONCAT_VECTORS &&
6094 V2->getOpcode() == ISD::UNDEF) {
6095 SDValue SubV1 = V1->getOperand(0);
6096 SDValue SubV2 = V1->getOperand(1);
6097 EVT SubVT = SubV1.getValueType();
6098
6099 // We expect these to have been canonicalized to -1.
6100 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
6101 return i < (int)VT.getVectorNumElements();
6102 }) && "Unexpected shuffle index into UNDEF operand!");
6103
6104 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6105 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
6106 if (isV_UNDEF)
6107 SubV2 = SubV1;
6108 assert((WhichResult == 0) &&
6109 "In-place shuffle of concat can only have one result!");
6110 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6111 SubV1, SubV2);
6112 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6113 Res.getValue(1));
6114 }
6115 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006116 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006117
Bob Wilsona7062312009-08-21 20:54:19 +00006118 // If the shuffle is not directly supported and it has 4 elements, use
6119 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006120 unsigned NumElts = VT.getVectorNumElements();
6121 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006122 unsigned PFIndexes[4];
6123 for (unsigned i = 0; i != 4; ++i) {
6124 if (ShuffleMask[i] < 0)
6125 PFIndexes[i] = 8;
6126 else
6127 PFIndexes[i] = ShuffleMask[i];
6128 }
6129
6130 // Compute the index in the perfect shuffle table.
6131 unsigned PFTableIndex =
6132 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006133 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6134 unsigned Cost = (PFEntry >> 30);
6135
6136 if (Cost <= 4)
6137 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6138 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006139
Bob Wilsond8a9a042010-06-04 00:04:02 +00006140 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006141 if (EltSize >= 32) {
6142 // Do the expansion with floating-point types, since that is what the VFP
6143 // registers are defined to use, and since i64 is not legal.
6144 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6145 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006146 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6147 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006148 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006149 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006150 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006151 Ops.push_back(DAG.getUNDEF(EltVT));
6152 else
6153 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6154 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6155 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006156 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006157 }
Craig Topper48d114b2014-04-26 18:35:24 +00006158 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006159 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006160 }
6161
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006162 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6163 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6164
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006165 if (VT == MVT::v8i8) {
6166 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
6167 if (NewOp.getNode())
6168 return NewOp;
6169 }
6170
Bob Wilson6f34e272009-08-14 05:16:33 +00006171 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006172}
6173
Eli Friedmana5e244c2011-10-24 23:08:52 +00006174static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6175 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6176 SDValue Lane = Op.getOperand(2);
6177 if (!isa<ConstantSDNode>(Lane))
6178 return SDValue();
6179
6180 return Op;
6181}
6182
Bob Wilson2e076c42009-06-22 23:27:02 +00006183static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006184 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006185 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006186 if (!isa<ConstantSDNode>(Lane))
6187 return SDValue();
6188
6189 SDValue Vec = Op.getOperand(0);
6190 if (Op.getValueType() == MVT::i32 &&
6191 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006192 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006193 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6194 }
6195
6196 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006197}
6198
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006199static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6200 // The only time a CONCAT_VECTORS operation can have legal types is when
6201 // two 64-bit vectors are concatenated to a 128-bit vector.
6202 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6203 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006204 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006205 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006206 SDValue Op0 = Op.getOperand(0);
6207 SDValue Op1 = Op.getOperand(1);
6208 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006209 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006210 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006211 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006212 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006213 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006214 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006215 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006216 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006217}
6218
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006219/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6220/// element has been zero/sign-extended, depending on the isSigned parameter,
6221/// from an integer type half its size.
6222static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6223 bool isSigned) {
6224 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6225 EVT VT = N->getValueType(0);
6226 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6227 SDNode *BVN = N->getOperand(0).getNode();
6228 if (BVN->getValueType(0) != MVT::v4i32 ||
6229 BVN->getOpcode() != ISD::BUILD_VECTOR)
6230 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006231 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006232 unsigned HiElt = 1 - LoElt;
6233 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6234 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6235 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6236 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6237 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6238 return false;
6239 if (isSigned) {
6240 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6241 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6242 return true;
6243 } else {
6244 if (Hi0->isNullValue() && Hi1->isNullValue())
6245 return true;
6246 }
6247 return false;
6248 }
6249
6250 if (N->getOpcode() != ISD::BUILD_VECTOR)
6251 return false;
6252
6253 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6254 SDNode *Elt = N->getOperand(i).getNode();
6255 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6256 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6257 unsigned HalfSize = EltSize / 2;
6258 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006259 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006260 return false;
6261 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006262 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006263 return false;
6264 }
6265 continue;
6266 }
6267 return false;
6268 }
6269
6270 return true;
6271}
6272
6273/// isSignExtended - Check if a node is a vector value that is sign-extended
6274/// or a constant BUILD_VECTOR with sign-extended elements.
6275static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6276 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6277 return true;
6278 if (isExtendedBUILD_VECTOR(N, DAG, true))
6279 return true;
6280 return false;
6281}
6282
6283/// isZeroExtended - Check if a node is a vector value that is zero-extended
6284/// or a constant BUILD_VECTOR with zero-extended elements.
6285static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6286 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6287 return true;
6288 if (isExtendedBUILD_VECTOR(N, DAG, false))
6289 return true;
6290 return false;
6291}
6292
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006293static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6294 if (OrigVT.getSizeInBits() >= 64)
6295 return OrigVT;
6296
6297 assert(OrigVT.isSimple() && "Expecting a simple value type");
6298
6299 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6300 switch (OrigSimpleTy) {
6301 default: llvm_unreachable("Unexpected Vector Type");
6302 case MVT::v2i8:
6303 case MVT::v2i16:
6304 return MVT::v2i32;
6305 case MVT::v4i8:
6306 return MVT::v4i16;
6307 }
6308}
6309
Sebastian Popa204f722012-11-30 19:08:04 +00006310/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6311/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6312/// We insert the required extension here to get the vector to fill a D register.
6313static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6314 const EVT &OrigTy,
6315 const EVT &ExtTy,
6316 unsigned ExtOpcode) {
6317 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6318 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6319 // 64-bits we need to insert a new extension so that it will be 64-bits.
6320 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6321 if (OrigTy.getSizeInBits() >= 64)
6322 return N;
6323
6324 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006325 EVT NewVT = getExtensionTo64Bits(OrigTy);
6326
Andrew Trickef9de2a2013-05-25 02:42:55 +00006327 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006328}
6329
6330/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6331/// does not do any sign/zero extension. If the original vector is less
6332/// than 64 bits, an appropriate extension will be added after the load to
6333/// reach a total size of 64 bits. We have to add the extension separately
6334/// because ARM does not have a sign/zero extending load for vectors.
6335static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006336 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6337
6338 // The load already has the right type.
6339 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006340 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006341 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6342 LD->isNonTemporal(), LD->isInvariant(),
6343 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006344
6345 // We need to create a zextload/sextload. We cannot just create a load
6346 // followed by a zext/zext node because LowerMUL is also run during normal
6347 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006348 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006349 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006350 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006351 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006352}
6353
6354/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6355/// extending load, or BUILD_VECTOR with extended elements, return the
6356/// unextended value. The unextended vector should be 64 bits so that it can
6357/// be used as an operand to a VMULL instruction. If the original vector size
6358/// before extension is less than 64 bits we add a an extension to resize
6359/// the vector to 64 bits.
6360static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006361 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006362 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6363 N->getOperand(0)->getValueType(0),
6364 N->getValueType(0),
6365 N->getOpcode());
6366
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006367 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006368 return SkipLoadExtensionForVMULL(LD, DAG);
6369
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006370 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6371 // have been legalized as a BITCAST from v4i32.
6372 if (N->getOpcode() == ISD::BITCAST) {
6373 SDNode *BVN = N->getOperand(0).getNode();
6374 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6375 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006376 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006377 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006378 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6379 }
6380 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6381 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6382 EVT VT = N->getValueType(0);
6383 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6384 unsigned NumElts = VT.getVectorNumElements();
6385 MVT TruncVT = MVT::getIntegerVT(EltSize);
6386 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006387 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006388 for (unsigned i = 0; i != NumElts; ++i) {
6389 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6390 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006391 // Element types smaller than 32 bits are not legal, so use i32 elements.
6392 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006393 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006394 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006395 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006396 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006397}
6398
Evan Chenge2086e72011-03-29 01:56:09 +00006399static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6400 unsigned Opcode = N->getOpcode();
6401 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6402 SDNode *N0 = N->getOperand(0).getNode();
6403 SDNode *N1 = N->getOperand(1).getNode();
6404 return N0->hasOneUse() && N1->hasOneUse() &&
6405 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6406 }
6407 return false;
6408}
6409
6410static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6411 unsigned Opcode = N->getOpcode();
6412 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6413 SDNode *N0 = N->getOperand(0).getNode();
6414 SDNode *N1 = N->getOperand(1).getNode();
6415 return N0->hasOneUse() && N1->hasOneUse() &&
6416 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6417 }
6418 return false;
6419}
6420
Bob Wilson38ab35a2010-09-01 23:50:19 +00006421static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6422 // Multiplications are only custom-lowered for 128-bit vectors so that
6423 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6424 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006425 assert(VT.is128BitVector() && VT.isInteger() &&
6426 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006427 SDNode *N0 = Op.getOperand(0).getNode();
6428 SDNode *N1 = Op.getOperand(1).getNode();
6429 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006430 bool isMLA = false;
6431 bool isN0SExt = isSignExtended(N0, DAG);
6432 bool isN1SExt = isSignExtended(N1, DAG);
6433 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006434 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006435 else {
6436 bool isN0ZExt = isZeroExtended(N0, DAG);
6437 bool isN1ZExt = isZeroExtended(N1, DAG);
6438 if (isN0ZExt && isN1ZExt)
6439 NewOpc = ARMISD::VMULLu;
6440 else if (isN1SExt || isN1ZExt) {
6441 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6442 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6443 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6444 NewOpc = ARMISD::VMULLs;
6445 isMLA = true;
6446 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6447 NewOpc = ARMISD::VMULLu;
6448 isMLA = true;
6449 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6450 std::swap(N0, N1);
6451 NewOpc = ARMISD::VMULLu;
6452 isMLA = true;
6453 }
6454 }
6455
6456 if (!NewOpc) {
6457 if (VT == MVT::v2i64)
6458 // Fall through to expand this. It is not legal.
6459 return SDValue();
6460 else
6461 // Other vector multiplications are legal.
6462 return Op;
6463 }
6464 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006465
6466 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006467 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006468 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006469 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006470 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006471 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006472 assert(Op0.getValueType().is64BitVector() &&
6473 Op1.getValueType().is64BitVector() &&
6474 "unexpected types for extended operands to VMULL");
6475 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6476 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006477
Evan Chenge2086e72011-03-29 01:56:09 +00006478 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6479 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6480 // vmull q0, d4, d6
6481 // vmlal q0, d5, d6
6482 // is faster than
6483 // vaddl q0, d4, d5
6484 // vmovl q1, d6
6485 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006486 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6487 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006488 EVT Op1VT = Op1.getValueType();
6489 return DAG.getNode(N0->getOpcode(), DL, VT,
6490 DAG.getNode(NewOpc, DL, VT,
6491 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6492 DAG.getNode(NewOpc, DL, VT,
6493 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006494}
6495
Owen Anderson77aa2662011-04-05 21:48:57 +00006496static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006497LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006498 // TODO: Should this propagate fast-math-flags?
6499
Nate Begemanfa62d502011-02-11 20:53:29 +00006500 // Convert to float
6501 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6502 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6503 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6504 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6505 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6506 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6507 // Get reciprocal estimate.
6508 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006509 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006510 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6511 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006512 // Because char has a smaller range than uchar, we can actually get away
6513 // without any newton steps. This requires that we use a weird bias
6514 // of 0xb000, however (again, this has been exhaustively tested).
6515 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6516 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6517 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006518 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006519 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6520 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6521 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6522 // Convert back to short.
6523 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6524 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6525 return X;
6526}
6527
Owen Anderson77aa2662011-04-05 21:48:57 +00006528static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006529LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006530 // TODO: Should this propagate fast-math-flags?
6531
Nate Begemanfa62d502011-02-11 20:53:29 +00006532 SDValue N2;
6533 // Convert to float.
6534 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6535 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6536 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6537 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6538 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6539 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006540
Nate Begemanfa62d502011-02-11 20:53:29 +00006541 // Use reciprocal estimate and one refinement step.
6542 // float4 recip = vrecpeq_f32(yf);
6543 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006544 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006545 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6546 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006547 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006548 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006549 N1, N2);
6550 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6551 // Because short has a smaller range than ushort, we can actually get away
6552 // with only a single newton step. This requires that we use a weird bias
6553 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006554 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006555 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6556 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006557 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006558 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6559 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6560 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6561 // Convert back to integer and return.
6562 // return vmovn_s32(vcvt_s32_f32(result));
6563 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6564 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6565 return N0;
6566}
6567
6568static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6569 EVT VT = Op.getValueType();
6570 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6571 "unexpected type for custom-lowering ISD::SDIV");
6572
Andrew Trickef9de2a2013-05-25 02:42:55 +00006573 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006574 SDValue N0 = Op.getOperand(0);
6575 SDValue N1 = Op.getOperand(1);
6576 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006577
Nate Begemanfa62d502011-02-11 20:53:29 +00006578 if (VT == MVT::v8i8) {
6579 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6580 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006581
Nate Begemanfa62d502011-02-11 20:53:29 +00006582 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006583 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006584 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006585 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006586 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006587 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006588 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006589 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006590
6591 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6592 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6593
6594 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6595 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006596
Nate Begemanfa62d502011-02-11 20:53:29 +00006597 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6598 return N0;
6599 }
6600 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6601}
6602
6603static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006604 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006605 EVT VT = Op.getValueType();
6606 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6607 "unexpected type for custom-lowering ISD::UDIV");
6608
Andrew Trickef9de2a2013-05-25 02:42:55 +00006609 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006610 SDValue N0 = Op.getOperand(0);
6611 SDValue N1 = Op.getOperand(1);
6612 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006613
Nate Begemanfa62d502011-02-11 20:53:29 +00006614 if (VT == MVT::v8i8) {
6615 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6616 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006617
Nate Begemanfa62d502011-02-11 20:53:29 +00006618 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006619 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006620 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006621 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006622 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006623 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006624 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006625 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006626
Nate Begemanfa62d502011-02-11 20:53:29 +00006627 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6628 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006629
Nate Begemanfa62d502011-02-11 20:53:29 +00006630 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6631 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006632
6633 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006634 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6635 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006636 N0);
6637 return N0;
6638 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006639
Nate Begemanfa62d502011-02-11 20:53:29 +00006640 // v4i16 sdiv ... Convert to float.
6641 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6642 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6643 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6644 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6645 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006646 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006647
6648 // Use reciprocal estimate and two refinement steps.
6649 // float4 recip = vrecpeq_f32(yf);
6650 // recip *= vrecpsq_f32(yf, recip);
6651 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006652 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006653 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6654 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006655 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006656 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006657 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006658 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006659 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006660 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006661 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006662 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6663 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6664 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6665 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006666 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006667 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6668 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006669 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006670 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6671 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6672 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6673 // Convert back to integer and return.
6674 // return vmovn_u32(vcvt_s32_f32(result));
6675 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6676 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6677 return N0;
6678}
6679
Evan Chenge8916542011-08-30 01:34:54 +00006680static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6681 EVT VT = Op.getNode()->getValueType(0);
6682 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6683
6684 unsigned Opc;
6685 bool ExtraOp = false;
6686 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006687 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006688 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6689 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6690 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6691 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6692 }
6693
6694 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006695 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006696 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006697 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006698 Op.getOperand(1), Op.getOperand(2));
6699}
6700
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006701SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6702 assert(Subtarget->isTargetDarwin());
6703
6704 // For iOS, we want to call an alternative entry point: __sincos_stret,
6705 // return values are passed via sret.
6706 SDLoc dl(Op);
6707 SDValue Arg = Op.getOperand(0);
6708 EVT ArgVT = Arg.getValueType();
6709 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006710 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006711
6712 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00006713 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006714
6715 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00006716 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00006717 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006718
6719 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00006720 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
6721 SDValue SRet;
6722 if (ShouldUseSRet) {
6723 // Create stack object for sret.
6724 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6725 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
6726 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6727 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
6728
6729 ArgListEntry Entry;
6730 Entry.Node = SRet;
6731 Entry.Ty = RetTy->getPointerTo();
6732 Entry.isSExt = false;
6733 Entry.isZExt = false;
6734 Entry.isSRet = true;
6735 Args.push_back(Entry);
6736 RetTy = Type::getVoidTy(*DAG.getContext());
6737 }
6738
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006739 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006740 Entry.Node = Arg;
6741 Entry.Ty = ArgTy;
6742 Entry.isSExt = false;
6743 Entry.isZExt = false;
6744 Args.push_back(Entry);
6745
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006746 const char *LibcallName =
6747 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00006748 RTLIB::Libcall LC =
6749 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
6750 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00006751 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006752
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006753 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00006754 CLI.setDebugLoc(dl)
6755 .setChain(DAG.getEntryNode())
6756 .setCallee(CC, RetTy, Callee, std::move(Args), 0)
6757 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006758 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6759
Tim Northover8b403662015-10-28 22:51:16 +00006760 if (!ShouldUseSRet)
6761 return CallResult.first;
6762
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006763 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6764 MachinePointerInfo(), false, false, false, 0);
6765
6766 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006767 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006768 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006769 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6770 MachinePointerInfo(), false, false, false, 0);
6771
6772 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6773 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6774 LoadSin.getValue(0), LoadCos.getValue(0));
6775}
6776
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006777SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00006778 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006779 SDValue &Chain) const {
6780 EVT VT = Op.getValueType();
6781 assert((VT == MVT::i32 || VT == MVT::i64) &&
6782 "unexpected type for custom lowering DIV");
6783 SDLoc dl(Op);
6784
6785 const auto &DL = DAG.getDataLayout();
6786 const auto &TLI = DAG.getTargetLoweringInfo();
6787
6788 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00006789 if (Signed)
6790 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6791 else
6792 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006793
6794 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6795
6796 ARMTargetLowering::ArgListTy Args;
6797
6798 for (auto AI : {1, 0}) {
6799 ArgListEntry Arg;
6800 Arg.Node = Op.getOperand(AI);
6801 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
6802 Args.push_back(Arg);
6803 }
6804
6805 CallLoweringInfo CLI(DAG);
6806 CLI.setDebugLoc(dl)
6807 .setChain(Chain)
6808 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
6809 ES, std::move(Args), 0);
6810
6811 return LowerCallTo(CLI).first;
6812}
6813
Martell Maloned1229242015-11-26 15:34:03 +00006814SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
6815 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006816 assert(Op.getValueType() == MVT::i32 &&
6817 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006818 SDLoc dl(Op);
6819
6820 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
6821 DAG.getEntryNode(), Op.getOperand(1));
6822
Martell Maloned1229242015-11-26 15:34:03 +00006823 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006824}
6825
6826void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00006827 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006828 SmallVectorImpl<SDValue> &Results) const {
6829 const auto &DL = DAG.getDataLayout();
6830 const auto &TLI = DAG.getTargetLoweringInfo();
6831
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006832 assert(Op.getValueType() == MVT::i64 &&
6833 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006834 SDLoc dl(Op);
6835
6836 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6837 DAG.getConstant(0, dl, MVT::i32));
6838 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6839 DAG.getConstant(1, dl, MVT::i32));
6840 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
6841
6842 SDValue DBZCHK =
6843 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
6844
Martell Maloned1229242015-11-26 15:34:03 +00006845 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006846
6847 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
6848 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
6849 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
6850 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
6851
6852 Results.push_back(Lower);
6853 Results.push_back(Upper);
6854}
6855
Eli Friedman10f9ce22011-09-15 22:26:18 +00006856static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006857 // Monotonic load/store is legal for all targets
6858 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6859 return Op;
6860
Alp Tokercb402912014-01-24 17:20:08 +00006861 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006862 // dmb or equivalent available.
6863 return SDValue();
6864}
6865
Tim Northoverbc933082013-05-23 19:11:20 +00006866static void ReplaceREADCYCLECOUNTER(SDNode *N,
6867 SmallVectorImpl<SDValue> &Results,
6868 SelectionDAG &DAG,
6869 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006870 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006871 // Under Power Management extensions, the cycle-count is:
6872 // mrc p15, #0, <Rt>, c9, c13, #0
6873 SDValue Ops[] = { N->getOperand(0), // Chain
6874 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6875 DAG.getConstant(15, DL, MVT::i32),
6876 DAG.getConstant(0, DL, MVT::i32),
6877 DAG.getConstant(9, DL, MVT::i32),
6878 DAG.getConstant(13, DL, MVT::i32),
6879 DAG.getConstant(0, DL, MVT::i32)
6880 };
Tim Northoverbc933082013-05-23 19:11:20 +00006881
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006882 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6883 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6884 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6885 DAG.getConstant(0, DL, MVT::i32)));
6886 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006887}
6888
Dan Gohman21cea8a2010-04-17 15:26:15 +00006889SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006890 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006891 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006892 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006893 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006894 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006895 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006896 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6897 default: llvm_unreachable("unknown object format");
6898 case Triple::COFF:
6899 return LowerGlobalAddressWindows(Op, DAG);
6900 case Triple::ELF:
6901 return LowerGlobalAddressELF(Op, DAG);
6902 case Triple::MachO:
6903 return LowerGlobalAddressDarwin(Op, DAG);
6904 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006905 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006906 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006907 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6908 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006909 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006910 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006911 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006912 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006913 case ISD::SINT_TO_FP:
6914 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6915 case ISD::FP_TO_SINT:
6916 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006917 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006918 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006919 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006920 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006921 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006922 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006923 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6924 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006925 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006926 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006927 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006928 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00006929 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
6930 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006931 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006932 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006933 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00006934 case ISD::CTTZ:
6935 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006936 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006937 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006938 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006939 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006940 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006941 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006942 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006943 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006944 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006945 case ISD::MUL: return LowerMUL(Op, DAG);
Martell Malonea6b867e2015-11-23 13:11:39 +00006946 case ISD::SDIV: return LowerSDIV(Op, DAG);
Martell Maloned1229242015-11-26 15:34:03 +00006947 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006948 case ISD::ADDC:
6949 case ISD::ADDE:
6950 case ISD::SUBC:
6951 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006952 case ISD::SADDO:
6953 case ISD::UADDO:
6954 case ISD::SSUBO:
6955 case ISD::USUBO:
6956 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006957 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006958 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006959 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006960 case ISD::SDIVREM:
6961 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006962 case ISD::DYNAMIC_STACKALLOC:
6963 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6964 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6965 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006966 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6967 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006968 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00006969 }
Evan Cheng10043e22007-01-19 07:51:42 +00006970}
6971
Duncan Sands6ed40142008-12-01 11:39:25 +00006972/// ReplaceNodeResults - Replace the results of node with an illegal result
6973/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006974void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006975 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006976 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006977 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006978 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006979 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006980 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006981 case ISD::READ_REGISTER:
6982 ExpandREAD_REGISTER(N, Results, DAG);
6983 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00006984 case ISD::BITCAST:
6985 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006986 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006987 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006988 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006989 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006990 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00006991 case ISD::SREM:
6992 case ISD::UREM:
6993 Res = LowerREM(N, DAG);
6994 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006995 case ISD::READCYCLECOUNTER:
6996 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6997 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006998 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00006999 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007000 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00007001 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
7002 Results);
Duncan Sands6ed40142008-12-01 11:39:25 +00007003 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00007004 if (Res.getNode())
7005 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00007006}
Chris Lattnerf81d5882007-11-24 07:07:01 +00007007
Evan Cheng10043e22007-01-19 07:51:42 +00007008//===----------------------------------------------------------------------===//
7009// ARM Scheduler Hooks
7010//===----------------------------------------------------------------------===//
7011
Bill Wendling030b58e2011-10-06 22:18:16 +00007012/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
7013/// registers the function context.
7014void ARMTargetLowering::
7015SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
7016 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007017 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00007018 DebugLoc dl = MI->getDebugLoc();
7019 MachineFunction *MF = MBB->getParent();
7020 MachineRegisterInfo *MRI = &MF->getRegInfo();
7021 MachineConstantPool *MCP = MF->getConstantPool();
7022 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
7023 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00007024
Bill Wendling374ee192011-10-03 21:25:38 +00007025 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007026 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00007027
Bill Wendling374ee192011-10-03 21:25:38 +00007028 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007029 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00007030 ARMConstantPoolValue *CPV =
7031 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
7032 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
7033
Craig Topper61e88f42014-11-21 05:58:21 +00007034 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
7035 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00007036
Bill Wendling030b58e2011-10-06 22:18:16 +00007037 // Grab constant pool and fixed stack memory operands.
7038 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007039 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
7040 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007041
7042 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007043 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
7044 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007045
7046 // Load the address of the dispatch MBB into the jump buffer.
7047 if (isThumb2) {
7048 // Incoming value: jbuf
7049 // ldr.n r5, LCPI1_1
7050 // orr r5, r5, #1
7051 // add r5, pc
7052 // str r5, [$jbuf, #+4] ; &jbuf[1]
7053 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7054 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
7055 .addConstantPoolIndex(CPI)
7056 .addMemOperand(CPMMO));
7057 // Set the low bit because of thumb mode.
7058 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7059 AddDefaultCC(
7060 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
7061 .addReg(NewVReg1, RegState::Kill)
7062 .addImm(0x01)));
7063 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7064 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
7065 .addReg(NewVReg2, RegState::Kill)
7066 .addImm(PCLabelId);
7067 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
7068 .addReg(NewVReg3, RegState::Kill)
7069 .addFrameIndex(FI)
7070 .addImm(36) // &jbuf[1] :: pc
7071 .addMemOperand(FIMMOSt));
7072 } else if (isThumb) {
7073 // Incoming value: jbuf
7074 // ldr.n r1, LCPI1_4
7075 // add r1, pc
7076 // mov r2, #1
7077 // orrs r1, r2
7078 // add r2, $jbuf, #+4 ; &jbuf[1]
7079 // str r1, [r2]
7080 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7081 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
7082 .addConstantPoolIndex(CPI)
7083 .addMemOperand(CPMMO));
7084 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7085 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
7086 .addReg(NewVReg1, RegState::Kill)
7087 .addImm(PCLabelId);
7088 // Set the low bit because of thumb mode.
7089 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7090 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
7091 .addReg(ARM::CPSR, RegState::Define)
7092 .addImm(1));
7093 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7094 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
7095 .addReg(ARM::CPSR, RegState::Define)
7096 .addReg(NewVReg2, RegState::Kill)
7097 .addReg(NewVReg3, RegState::Kill));
7098 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00007099 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
7100 .addFrameIndex(FI)
7101 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00007102 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
7103 .addReg(NewVReg4, RegState::Kill)
7104 .addReg(NewVReg5, RegState::Kill)
7105 .addImm(0)
7106 .addMemOperand(FIMMOSt));
7107 } else {
7108 // Incoming value: jbuf
7109 // ldr r1, LCPI1_1
7110 // add r1, pc, r1
7111 // str r1, [$jbuf, #+4] ; &jbuf[1]
7112 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7113 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
7114 .addConstantPoolIndex(CPI)
7115 .addImm(0)
7116 .addMemOperand(CPMMO));
7117 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7118 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7119 .addReg(NewVReg1, RegState::Kill)
7120 .addImm(PCLabelId));
7121 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7122 .addReg(NewVReg2, RegState::Kill)
7123 .addFrameIndex(FI)
7124 .addImm(36) // &jbuf[1] :: pc
7125 .addMemOperand(FIMMOSt));
7126 }
7127}
7128
Matthias Brauneec4efc2015-04-28 00:37:05 +00007129void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
7130 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007131 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007132 DebugLoc dl = MI->getDebugLoc();
7133 MachineFunction *MF = MBB->getParent();
7134 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007135 MachineFrameInfo *MFI = MF->getFrameInfo();
7136 int FI = MFI->getFunctionContextIndex();
7137
Craig Topper61e88f42014-11-21 05:58:21 +00007138 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7139 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007140
Bill Wendling362c1b02011-10-06 21:29:56 +00007141 // Get a mapping of the call site numbers to all of the landing pads they're
7142 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007143 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7144 unsigned MaxCSNum = 0;
7145 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007146 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7147 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007148 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007149
7150 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7151 // pad.
7152 for (MachineBasicBlock::iterator
7153 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7154 if (!II->isEHLabel()) continue;
7155
7156 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007157 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007158
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007159 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7160 for (SmallVectorImpl<unsigned>::iterator
7161 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7162 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007163 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007164 MaxCSNum = std::max(MaxCSNum, *CSI);
7165 }
Bill Wendling202803e2011-10-05 00:02:33 +00007166 break;
7167 }
7168 }
7169
7170 // Get an ordered list of the machine basic blocks for the jump table.
7171 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00007172 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007173 LPadList.reserve(CallSiteNumToLPad.size());
7174 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7175 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7176 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007177 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007178 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007179 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7180 }
Bill Wendling202803e2011-10-05 00:02:33 +00007181 }
7182
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007183 assert(!LPadList.empty() &&
7184 "No landing pad destinations for the dispatch jump table!");
7185
Bill Wendling362c1b02011-10-06 21:29:56 +00007186 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007187 MachineJumpTableInfo *JTI =
7188 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7189 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00007190 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00007191
Bill Wendling362c1b02011-10-06 21:29:56 +00007192 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007193
7194 // Shove the dispatch's address into the return slot in the function context.
7195 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007196 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007197
Bill Wendling324be982011-10-05 00:39:32 +00007198 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007199 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007200 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007201 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007202 else
7203 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7204
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007205 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007206 DispatchBB->addSuccessor(TrapBB);
7207
7208 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7209 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007210
Bill Wendling510fbcd2011-10-17 21:32:56 +00007211 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007212 MF->insert(MF->end(), DispatchBB);
7213 MF->insert(MF->end(), DispContBB);
7214 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007215
Bill Wendling030b58e2011-10-06 22:18:16 +00007216 // Insert code into the entry block that creates and registers the function
7217 // context.
7218 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7219
Alex Lorenze40c8a22015-08-11 23:09:45 +00007220 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7221 MachinePointerInfo::getFixedStack(*MF, FI),
7222 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007223
Chad Rosier1ec8e402012-11-06 23:05:24 +00007224 MachineInstrBuilder MIB;
7225 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7226
7227 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7228 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7229
7230 // Add a register mask with no preserved registers. This results in all
7231 // registers being marked as clobbered.
7232 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007233
Bill Wendling85833f72011-10-18 22:49:07 +00007234 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007235 if (Subtarget->isThumb2()) {
7236 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7237 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7238 .addFrameIndex(FI)
7239 .addImm(4)
7240 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007241
Bill Wendling85833f72011-10-18 22:49:07 +00007242 if (NumLPads < 256) {
7243 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7244 .addReg(NewVReg1)
7245 .addImm(LPadList.size()));
7246 } else {
7247 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7248 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007249 .addImm(NumLPads & 0xFFFF));
7250
7251 unsigned VReg2 = VReg1;
7252 if ((NumLPads & 0xFFFF0000) != 0) {
7253 VReg2 = MRI->createVirtualRegister(TRC);
7254 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7255 .addReg(VReg1)
7256 .addImm(NumLPads >> 16));
7257 }
7258
Bill Wendling85833f72011-10-18 22:49:07 +00007259 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7260 .addReg(NewVReg1)
7261 .addReg(VReg2));
7262 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007263
Bill Wendling5626c662011-10-06 22:53:00 +00007264 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7265 .addMBB(TrapBB)
7266 .addImm(ARMCC::HI)
7267 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007268
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007269 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7270 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007271 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007272
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007273 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007274 AddDefaultCC(
7275 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007276 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7277 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007278 .addReg(NewVReg1)
7279 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7280
7281 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007282 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007283 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007284 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007285 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007286 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7287 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7288 .addFrameIndex(FI)
7289 .addImm(1)
7290 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007291
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007292 if (NumLPads < 256) {
7293 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7294 .addReg(NewVReg1)
7295 .addImm(NumLPads));
7296 } else {
7297 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007298 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7299 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7300
7301 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007302 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007303 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007304 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007305 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007306
7307 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7308 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7309 .addReg(VReg1, RegState::Define)
7310 .addConstantPoolIndex(Idx));
7311 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7312 .addReg(NewVReg1)
7313 .addReg(VReg1));
7314 }
7315
Bill Wendlingb3d46782011-10-06 23:37:36 +00007316 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7317 .addMBB(TrapBB)
7318 .addImm(ARMCC::HI)
7319 .addReg(ARM::CPSR);
7320
7321 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7322 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7323 .addReg(ARM::CPSR, RegState::Define)
7324 .addReg(NewVReg1)
7325 .addImm(2));
7326
7327 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007328 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007329 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007330
7331 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7332 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7333 .addReg(ARM::CPSR, RegState::Define)
7334 .addReg(NewVReg2, RegState::Kill)
7335 .addReg(NewVReg3));
7336
Alex Lorenze40c8a22015-08-11 23:09:45 +00007337 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7338 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007339
7340 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7341 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7342 .addReg(NewVReg4, RegState::Kill)
7343 .addImm(0)
7344 .addMemOperand(JTMMOLd));
7345
Chad Rosier96603432013-03-01 18:30:38 +00007346 unsigned NewVReg6 = NewVReg5;
7347 if (RelocM == Reloc::PIC_) {
7348 NewVReg6 = MRI->createVirtualRegister(TRC);
7349 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7350 .addReg(ARM::CPSR, RegState::Define)
7351 .addReg(NewVReg5, RegState::Kill)
7352 .addReg(NewVReg3));
7353 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007354
7355 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7356 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007357 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007358 } else {
7359 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7360 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7361 .addFrameIndex(FI)
7362 .addImm(4)
7363 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007364
Bill Wendling4969dcd2011-10-18 22:52:20 +00007365 if (NumLPads < 256) {
7366 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7367 .addReg(NewVReg1)
7368 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007369 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007370 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7371 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007372 .addImm(NumLPads & 0xFFFF));
7373
7374 unsigned VReg2 = VReg1;
7375 if ((NumLPads & 0xFFFF0000) != 0) {
7376 VReg2 = MRI->createVirtualRegister(TRC);
7377 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7378 .addReg(VReg1)
7379 .addImm(NumLPads >> 16));
7380 }
7381
Bill Wendling4969dcd2011-10-18 22:52:20 +00007382 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7383 .addReg(NewVReg1)
7384 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007385 } else {
7386 MachineConstantPool *ConstantPool = MF->getConstantPool();
7387 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7388 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7389
7390 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007391 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007392 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007393 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007394 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7395
7396 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7397 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7398 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007399 .addConstantPoolIndex(Idx)
7400 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007401 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7402 .addReg(NewVReg1)
7403 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007404 }
7405
Bill Wendling5626c662011-10-06 22:53:00 +00007406 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7407 .addMBB(TrapBB)
7408 .addImm(ARMCC::HI)
7409 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007410
Bill Wendling973c8172011-10-18 22:11:18 +00007411 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007412 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007413 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007414 .addReg(NewVReg1)
7415 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007416 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7417 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007418 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007419
Alex Lorenze40c8a22015-08-11 23:09:45 +00007420 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7421 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007422 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007423 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007424 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7425 .addReg(NewVReg3, RegState::Kill)
7426 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007427 .addImm(0)
7428 .addMemOperand(JTMMOLd));
7429
Chad Rosier96603432013-03-01 18:30:38 +00007430 if (RelocM == Reloc::PIC_) {
7431 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7432 .addReg(NewVReg5, RegState::Kill)
7433 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007434 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007435 } else {
7436 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7437 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007438 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007439 }
Bill Wendling5626c662011-10-06 22:53:00 +00007440 }
Bill Wendling202803e2011-10-05 00:02:33 +00007441
Bill Wendling324be982011-10-05 00:39:32 +00007442 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007443 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007444 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007445 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7446 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007447 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007448 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007449 }
7450
Bill Wendling26d27802011-10-17 05:25:09 +00007451 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007452 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007453 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007454 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007455
7456 // Remove the landing pad successor from the invoke block and replace it
7457 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007458 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7459 BB->succ_end());
7460 while (!Successors.empty()) {
7461 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007462 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007463 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007464 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007465 }
7466 }
7467
Cong Houd97c1002015-12-01 05:29:22 +00007468 BB->addSuccessor(DispatchBB, BranchProbability::getZero());
Cong Houc1069892015-12-13 09:26:17 +00007469 BB->normalizeSuccProbs();
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007470
7471 // Find the invoke call and mark all of the callee-saved registers as
7472 // 'implicit defined' so that they're spilled. This prevents code from
7473 // moving instructions to before the EH block, where they will never be
7474 // executed.
7475 for (MachineBasicBlock::reverse_iterator
7476 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007477 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007478
7479 DenseMap<unsigned, bool> DefRegs;
7480 for (MachineInstr::mop_iterator
7481 OI = II->operands_begin(), OE = II->operands_end();
7482 OI != OE; ++OI) {
7483 if (!OI->isReg()) continue;
7484 DefRegs[OI->getReg()] = true;
7485 }
7486
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007487 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007488
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007489 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007490 unsigned Reg = SavedRegs[i];
7491 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007492 !ARM::tGPRRegClass.contains(Reg) &&
7493 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007494 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007495 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007496 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007497 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007498 continue;
7499 if (!DefRegs[Reg])
7500 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007501 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007502
7503 break;
7504 }
Bill Wendling883ec972011-10-07 23:18:02 +00007505 }
Bill Wendling324be982011-10-05 00:39:32 +00007506
Bill Wendling617075f2011-10-18 18:30:49 +00007507 // Mark all former landing pads as non-landing pads. The dispatch is the only
7508 // landing pad now.
7509 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7510 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007511 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007512
Bill Wendling324be982011-10-05 00:39:32 +00007513 // The instruction is gone now.
7514 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007515}
7516
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007517static
7518MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7519 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7520 E = MBB->succ_end(); I != E; ++I)
7521 if (*I != Succ)
7522 return *I;
7523 llvm_unreachable("Expecting a BB with two successors!");
7524}
7525
Manman Renb504f492013-10-29 22:27:32 +00007526/// Return the load opcode for a given load size. If load size >= 8,
7527/// neon opcode will be returned.
7528static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7529 if (LdSize >= 8)
7530 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7531 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7532 if (IsThumb1)
7533 return LdSize == 4 ? ARM::tLDRi
7534 : LdSize == 2 ? ARM::tLDRHi
7535 : LdSize == 1 ? ARM::tLDRBi : 0;
7536 if (IsThumb2)
7537 return LdSize == 4 ? ARM::t2LDR_POST
7538 : LdSize == 2 ? ARM::t2LDRH_POST
7539 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7540 return LdSize == 4 ? ARM::LDR_POST_IMM
7541 : LdSize == 2 ? ARM::LDRH_POST
7542 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7543}
7544
7545/// Return the store opcode for a given store size. If store size >= 8,
7546/// neon opcode will be returned.
7547static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7548 if (StSize >= 8)
7549 return StSize == 16 ? ARM::VST1q32wb_fixed
7550 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7551 if (IsThumb1)
7552 return StSize == 4 ? ARM::tSTRi
7553 : StSize == 2 ? ARM::tSTRHi
7554 : StSize == 1 ? ARM::tSTRBi : 0;
7555 if (IsThumb2)
7556 return StSize == 4 ? ARM::t2STR_POST
7557 : StSize == 2 ? ARM::t2STRH_POST
7558 : StSize == 1 ? ARM::t2STRB_POST : 0;
7559 return StSize == 4 ? ARM::STR_POST_IMM
7560 : StSize == 2 ? ARM::STRH_POST
7561 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7562}
7563
7564/// Emit a post-increment load operation with given size. The instructions
7565/// will be added to BB at Pos.
7566static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7567 const TargetInstrInfo *TII, DebugLoc dl,
7568 unsigned LdSize, unsigned Data, unsigned AddrIn,
7569 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7570 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7571 assert(LdOpc != 0 && "Should have a load opcode");
7572 if (LdSize >= 8) {
7573 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7574 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7575 .addImm(0));
7576 } else if (IsThumb1) {
7577 // load + update AddrIn
7578 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7579 .addReg(AddrIn).addImm(0));
7580 MachineInstrBuilder MIB =
7581 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7582 MIB = AddDefaultT1CC(MIB);
7583 MIB.addReg(AddrIn).addImm(LdSize);
7584 AddDefaultPred(MIB);
7585 } else if (IsThumb2) {
7586 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7587 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7588 .addImm(LdSize));
7589 } else { // arm
7590 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7591 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7592 .addReg(0).addImm(LdSize));
7593 }
7594}
7595
7596/// Emit a post-increment store operation with given size. The instructions
7597/// will be added to BB at Pos.
7598static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7599 const TargetInstrInfo *TII, DebugLoc dl,
7600 unsigned StSize, unsigned Data, unsigned AddrIn,
7601 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7602 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7603 assert(StOpc != 0 && "Should have a store opcode");
7604 if (StSize >= 8) {
7605 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7606 .addReg(AddrIn).addImm(0).addReg(Data));
7607 } else if (IsThumb1) {
7608 // store + update AddrIn
7609 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7610 .addReg(AddrIn).addImm(0));
7611 MachineInstrBuilder MIB =
7612 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7613 MIB = AddDefaultT1CC(MIB);
7614 MIB.addReg(AddrIn).addImm(StSize);
7615 AddDefaultPred(MIB);
7616 } else if (IsThumb2) {
7617 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7618 .addReg(Data).addReg(AddrIn).addImm(StSize));
7619 } else { // arm
7620 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7621 .addReg(Data).addReg(AddrIn).addReg(0)
7622 .addImm(StSize));
7623 }
7624}
7625
David Peixottoc32e24a2013-10-17 19:49:22 +00007626MachineBasicBlock *
7627ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7628 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007629 // This pseudo instruction has 3 operands: dst, src, size
7630 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7631 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007632 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007633 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007634 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00007635
7636 unsigned dest = MI->getOperand(0).getReg();
7637 unsigned src = MI->getOperand(1).getReg();
7638 unsigned SizeVal = MI->getOperand(2).getImm();
7639 unsigned Align = MI->getOperand(3).getImm();
7640 DebugLoc dl = MI->getDebugLoc();
7641
Manman Rene8735522012-06-01 19:33:18 +00007642 MachineFunction *MF = BB->getParent();
7643 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007644 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007645 const TargetRegisterClass *TRC = nullptr;
7646 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007647
7648 bool IsThumb1 = Subtarget->isThumb1Only();
7649 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007650
7651 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007652 UnitSize = 1;
7653 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007654 UnitSize = 2;
7655 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007656 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007657 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007658 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007659 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007660 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007661 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007662 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007663 }
7664 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007665 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007666 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007667 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007668
David Peixottob0653e532013-10-24 16:39:36 +00007669 // Select the correct opcode and register class for unit size load/store
7670 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007671 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007672 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007673 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7674 : UnitSize == 8 ? &ARM::DPRRegClass
7675 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007676
Manman Rene8735522012-06-01 19:33:18 +00007677 unsigned BytesLeft = SizeVal % UnitSize;
7678 unsigned LoopSize = SizeVal - BytesLeft;
7679
7680 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7681 // Use LDR and STR to copy.
7682 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7683 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7684 unsigned srcIn = src;
7685 unsigned destIn = dest;
7686 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007687 unsigned srcOut = MRI.createVirtualRegister(TRC);
7688 unsigned destOut = MRI.createVirtualRegister(TRC);
7689 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007690 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7691 IsThumb1, IsThumb2);
7692 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7693 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007694 srcIn = srcOut;
7695 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007696 }
7697
7698 // Handle the leftover bytes with LDRB and STRB.
7699 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7700 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007701 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007702 unsigned srcOut = MRI.createVirtualRegister(TRC);
7703 unsigned destOut = MRI.createVirtualRegister(TRC);
7704 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007705 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7706 IsThumb1, IsThumb2);
7707 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7708 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007709 srcIn = srcOut;
7710 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007711 }
7712 MI->eraseFromParent(); // The instruction is gone now.
7713 return BB;
7714 }
7715
7716 // Expand the pseudo op to a loop.
7717 // thisMBB:
7718 // ...
7719 // movw varEnd, # --> with thumb2
7720 // movt varEnd, #
7721 // ldrcp varEnd, idx --> without thumb2
7722 // fallthrough --> loopMBB
7723 // loopMBB:
7724 // PHI varPhi, varEnd, varLoop
7725 // PHI srcPhi, src, srcLoop
7726 // PHI destPhi, dst, destLoop
7727 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7728 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7729 // subs varLoop, varPhi, #UnitSize
7730 // bne loopMBB
7731 // fallthrough --> exitMBB
7732 // exitMBB:
7733 // epilogue to handle left-over bytes
7734 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7735 // [destOut] = STRB_POST(scratch, destLoop, 1)
7736 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7737 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7738 MF->insert(It, loopMBB);
7739 MF->insert(It, exitMBB);
7740
7741 // Transfer the remainder of BB and its successor edges to exitMBB.
7742 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007743 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007744 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7745
7746 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007747 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007748 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007749 unsigned Vtmp = varEnd;
7750 if ((LoopSize & 0xFFFF0000) != 0)
7751 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007752 AddDefaultPred(BuildMI(BB, dl,
7753 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7754 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007755
7756 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007757 AddDefaultPred(BuildMI(BB, dl,
7758 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7759 varEnd)
7760 .addReg(Vtmp)
7761 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007762 } else {
7763 MachineConstantPool *ConstantPool = MF->getConstantPool();
7764 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7765 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7766
7767 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007768 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007769 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007770 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007771 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7772
7773 if (IsThumb1)
7774 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7775 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7776 else
7777 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7778 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7779 }
Manman Rene8735522012-06-01 19:33:18 +00007780 BB->addSuccessor(loopMBB);
7781
7782 // Generate the loop body:
7783 // varPhi = PHI(varLoop, varEnd)
7784 // srcPhi = PHI(srcLoop, src)
7785 // destPhi = PHI(destLoop, dst)
7786 MachineBasicBlock *entryBB = BB;
7787 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007788 unsigned varLoop = MRI.createVirtualRegister(TRC);
7789 unsigned varPhi = MRI.createVirtualRegister(TRC);
7790 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7791 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7792 unsigned destLoop = MRI.createVirtualRegister(TRC);
7793 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007794
7795 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7796 .addReg(varLoop).addMBB(loopMBB)
7797 .addReg(varEnd).addMBB(entryBB);
7798 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7799 .addReg(srcLoop).addMBB(loopMBB)
7800 .addReg(src).addMBB(entryBB);
7801 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7802 .addReg(destLoop).addMBB(loopMBB)
7803 .addReg(dest).addMBB(entryBB);
7804
7805 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7806 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007807 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007808 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7809 IsThumb1, IsThumb2);
7810 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7811 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007812
7813 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007814 if (IsThumb1) {
7815 MachineInstrBuilder MIB =
7816 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7817 MIB = AddDefaultT1CC(MIB);
7818 MIB.addReg(varPhi).addImm(UnitSize);
7819 AddDefaultPred(MIB);
7820 } else {
7821 MachineInstrBuilder MIB =
7822 BuildMI(*BB, BB->end(), dl,
7823 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7824 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7825 MIB->getOperand(5).setReg(ARM::CPSR);
7826 MIB->getOperand(5).setIsDef(true);
7827 }
7828 BuildMI(*BB, BB->end(), dl,
7829 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7830 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007831
7832 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7833 BB->addSuccessor(loopMBB);
7834 BB->addSuccessor(exitMBB);
7835
7836 // Add epilogue to handle BytesLeft.
7837 BB = exitMBB;
7838 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007839
7840 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7841 // [destOut] = STRB_POST(scratch, destLoop, 1)
7842 unsigned srcIn = srcLoop;
7843 unsigned destIn = destLoop;
7844 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007845 unsigned srcOut = MRI.createVirtualRegister(TRC);
7846 unsigned destOut = MRI.createVirtualRegister(TRC);
7847 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007848 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7849 IsThumb1, IsThumb2);
7850 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7851 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007852 srcIn = srcOut;
7853 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007854 }
7855
7856 MI->eraseFromParent(); // The instruction is gone now.
7857 return BB;
7858}
7859
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007860MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007861ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7862 MachineBasicBlock *MBB) const {
7863 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007864 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007865 DebugLoc DL = MI->getDebugLoc();
7866
7867 assert(Subtarget->isTargetWindows() &&
7868 "__chkstk is only supported on Windows");
7869 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7870
7871 // __chkstk takes the number of words to allocate on the stack in R4, and
7872 // returns the stack adjustment in number of bytes in R4. This will not
7873 // clober any other registers (other than the obvious lr).
7874 //
7875 // Although, technically, IP should be considered a register which may be
7876 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7877 // thumb-2 environment, so there is no interworking required. As a result, we
7878 // do not expect a veneer to be emitted by the linker, clobbering IP.
7879 //
Alp Toker1d099d92014-06-19 19:41:26 +00007880 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007881 // required, again, ensuring that IP is not clobbered.
7882 //
7883 // Finally, although some linkers may theoretically provide a trampoline for
7884 // out of range calls (which is quite common due to a 32M range limitation of
7885 // branches for Thumb), we can generate the long-call version via
7886 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7887 // IP.
7888
7889 switch (TM.getCodeModel()) {
7890 case CodeModel::Small:
7891 case CodeModel::Medium:
7892 case CodeModel::Default:
7893 case CodeModel::Kernel:
7894 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7895 .addImm((unsigned)ARMCC::AL).addReg(0)
7896 .addExternalSymbol("__chkstk")
7897 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7898 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7899 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7900 break;
7901 case CodeModel::Large:
7902 case CodeModel::JITDefault: {
7903 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7904 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7905
7906 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7907 .addExternalSymbol("__chkstk");
7908 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7909 .addImm((unsigned)ARMCC::AL).addReg(0)
7910 .addReg(Reg, RegState::Kill)
7911 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7912 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7913 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7914 break;
7915 }
7916 }
7917
7918 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7919 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007920 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007921
7922 MI->eraseFromParent();
7923 return MBB;
7924}
7925
7926MachineBasicBlock *
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007927ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI,
7928 MachineBasicBlock *MBB) const {
7929 DebugLoc DL = MI->getDebugLoc();
7930 MachineFunction *MF = MBB->getParent();
7931 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
7932
7933 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
7934 MF->push_back(ContBB);
7935 ContBB->splice(ContBB->begin(), MBB,
7936 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
7937 MBB->addSuccessor(ContBB);
7938
7939 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
7940 MF->push_back(TrapBB);
7941 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
7942 MBB->addSuccessor(TrapBB);
7943
7944 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
7945 .addReg(MI->getOperand(0).getReg())
7946 .addMBB(TrapBB);
7947
7948 MI->eraseFromParent();
7949 return ContBB;
7950}
7951
7952MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007953ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007954 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007955 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007956 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007957 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007958 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007959 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007960 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007961 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007962 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007963 // The Thumb2 pre-indexed stores have the same MI operands, they just
7964 // define them differently in the .td files from the isel patterns, so
7965 // they need pseudos.
7966 case ARM::t2STR_preidx:
7967 MI->setDesc(TII->get(ARM::t2STR_PRE));
7968 return BB;
7969 case ARM::t2STRB_preidx:
7970 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7971 return BB;
7972 case ARM::t2STRH_preidx:
7973 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7974 return BB;
7975
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007976 case ARM::STRi_preidx:
7977 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007978 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007979 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7980 // Decode the offset.
7981 unsigned Offset = MI->getOperand(4).getImm();
7982 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7983 Offset = ARM_AM::getAM2Offset(Offset);
7984 if (isSub)
7985 Offset = -Offset;
7986
Jim Grosbachf402f692011-08-12 21:02:34 +00007987 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007988 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007989 .addOperand(MI->getOperand(0)) // Rn_wb
7990 .addOperand(MI->getOperand(1)) // Rt
7991 .addOperand(MI->getOperand(2)) // Rn
7992 .addImm(Offset) // offset (skip GPR==zero_reg)
7993 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007994 .addOperand(MI->getOperand(6))
7995 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007996 MI->eraseFromParent();
7997 return BB;
7998 }
7999 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008000 case ARM::STRBr_preidx:
8001 case ARM::STRH_preidx: {
8002 unsigned NewOpc;
8003 switch (MI->getOpcode()) {
8004 default: llvm_unreachable("unexpected opcode!");
8005 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
8006 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
8007 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
8008 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008009 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
8010 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
8011 MIB.addOperand(MI->getOperand(i));
8012 MI->eraseFromParent();
8013 return BB;
8014 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00008015
Evan Chengbb2af352009-08-12 05:17:19 +00008016 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00008017 // To "insert" a SELECT_CC instruction, we actually have to insert the
8018 // diamond control-flow pattern. The incoming instruction knows the
8019 // destination vreg to set, the condition code register to branch on, the
8020 // true/false values to select between, and a branch opcode to use.
8021 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008022 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00008023
8024 // thisMBB:
8025 // ...
8026 // TrueVal = ...
8027 // cmpTY ccX, r1, r2
8028 // bCC copy1MBB
8029 // fallthrough --> copy0MBB
8030 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00008031 MachineFunction *F = BB->getParent();
8032 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8033 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00008034 F->insert(It, copy0MBB);
8035 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008036
8037 // Transfer the remainder of BB and its successor edges to sinkMBB.
8038 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008039 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008040 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8041
Dan Gohmanf4f04102010-07-06 15:49:48 +00008042 BB->addSuccessor(copy0MBB);
8043 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00008044
Dan Gohman34396292010-07-06 20:24:04 +00008045 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
8046 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
8047
Evan Cheng10043e22007-01-19 07:51:42 +00008048 // copy0MBB:
8049 // %FalseValue = ...
8050 // # fallthrough to sinkMBB
8051 BB = copy0MBB;
8052
8053 // Update machine-CFG edges
8054 BB->addSuccessor(sinkMBB);
8055
8056 // sinkMBB:
8057 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8058 // ...
8059 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008060 BuildMI(*BB, BB->begin(), dl,
8061 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00008062 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8063 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8064
Dan Gohman34396292010-07-06 20:24:04 +00008065 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00008066 return BB;
8067 }
Evan Chengb972e562009-08-07 00:34:42 +00008068
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008069 case ARM::BCCi64:
8070 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00008071 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008072 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00008073
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008074 // Compare both parts that make up the double comparison separately for
8075 // equality.
8076 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
8077
8078 unsigned LHS1 = MI->getOperand(1).getReg();
8079 unsigned LHS2 = MI->getOperand(2).getReg();
8080 if (RHSisZero) {
8081 AddDefaultPred(BuildMI(BB, dl,
8082 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8083 .addReg(LHS1).addImm(0));
8084 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8085 .addReg(LHS2).addImm(0)
8086 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8087 } else {
8088 unsigned RHS1 = MI->getOperand(3).getReg();
8089 unsigned RHS2 = MI->getOperand(4).getReg();
8090 AddDefaultPred(BuildMI(BB, dl,
8091 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8092 .addReg(LHS1).addReg(RHS1));
8093 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8094 .addReg(LHS2).addReg(RHS2)
8095 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8096 }
8097
8098 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
8099 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
8100 if (MI->getOperand(0).getImm() == ARMCC::NE)
8101 std::swap(destMBB, exitMBB);
8102
8103 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
8104 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00008105 if (isThumb2)
8106 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
8107 else
8108 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008109
8110 MI->eraseFromParent(); // The pseudo instruction is gone now.
8111 return BB;
8112 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008113
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008114 case ARM::Int_eh_sjlj_setjmp:
8115 case ARM::Int_eh_sjlj_setjmp_nofp:
8116 case ARM::tInt_eh_sjlj_setjmp:
8117 case ARM::t2Int_eh_sjlj_setjmp:
8118 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00008119 return BB;
8120
8121 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008122 EmitSjLjDispatchBlock(MI, BB);
8123 return BB;
8124
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008125 case ARM::ABS:
8126 case ARM::t2ABS: {
8127 // To insert an ABS instruction, we have to insert the
8128 // diamond control-flow pattern. The incoming instruction knows the
8129 // source vreg to test against 0, the destination vreg to set,
8130 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008131 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008132 // It transforms
8133 // V1 = ABS V0
8134 // into
8135 // V2 = MOVS V0
8136 // BCC (branch to SinkBB if V0 >= 0)
8137 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008138 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008139 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008140 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008141 MachineFunction *Fn = BB->getParent();
8142 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8143 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8144 Fn->insert(BBI, RSBBB);
8145 Fn->insert(BBI, SinkBB);
8146
8147 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
8148 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00008149 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008150 bool isThumb2 = Subtarget->isThumb2();
8151 MachineRegisterInfo &MRI = Fn->getRegInfo();
8152 // In Thumb mode S must not be specified if source register is the SP or
8153 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008154 unsigned NewRsbDstReg =
8155 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008156
8157 // Transfer the remainder of BB and its successor edges to sinkMBB.
8158 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008159 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008160 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8161
8162 BB->addSuccessor(RSBBB);
8163 BB->addSuccessor(SinkBB);
8164
8165 // fall through to SinkMBB
8166 RSBBB->addSuccessor(SinkBB);
8167
Manman Rene0763c72012-06-15 21:32:12 +00008168 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008169 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008170 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8171 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008172
8173 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008174 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008175 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8176 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8177
8178 // insert rsbri in RSBBB
8179 // Note: BCC and rsbri will be converted into predicated rsbmi
8180 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008181 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008182 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008183 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008184 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8185
Andrew Trick3f07c422011-10-18 18:40:53 +00008186 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008187 // reuse ABSDstReg to not change uses of ABS instruction
8188 BuildMI(*SinkBB, SinkBB->begin(), dl,
8189 TII->get(ARM::PHI), ABSDstReg)
8190 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008191 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008192
8193 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00008194 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008195
8196 // return last added BB
8197 return SinkBB;
8198 }
Manman Rene8735522012-06-01 19:33:18 +00008199 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008200 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008201 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008202 case ARM::WIN__CHKSTK:
8203 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008204 case ARM::WIN__DBZCHK:
8205 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008206 }
8207}
8208
Scott Douglass953f9082015-10-05 14:49:54 +00008209/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8210/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8211/// instead of as a custom inserter because we need the use list from the SDNode.
8212static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
8213 MachineInstr *MI, const SDNode *Node) {
8214 bool isThumb1 = Subtarget->isThumb1Only();
8215
8216 DebugLoc DL = MI->getDebugLoc();
8217 MachineFunction *MF = MI->getParent()->getParent();
8218 MachineRegisterInfo &MRI = MF->getRegInfo();
8219 MachineInstrBuilder MIB(*MF, MI);
8220
8221 // If the new dst/src is unused mark it as dead.
8222 if (!Node->hasAnyUseOfValue(0)) {
8223 MI->getOperand(0).setIsDead(true);
8224 }
8225 if (!Node->hasAnyUseOfValue(1)) {
8226 MI->getOperand(1).setIsDead(true);
8227 }
8228
8229 // The MEMCPY both defines and kills the scratch registers.
8230 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
8231 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8232 : &ARM::GPRRegClass);
8233 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8234 }
8235}
8236
Evan Chenge6fba772011-08-30 19:09:48 +00008237void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
8238 SDNode *Node) const {
Scott Douglass953f9082015-10-05 14:49:54 +00008239 if (MI->getOpcode() == ARM::MEMCPY) {
8240 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8241 return;
8242 }
8243
Evan Cheng7f8e5632011-12-07 07:15:52 +00008244 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008245 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8246 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8247 // operand is still set to noreg. If needed, set the optional operand's
8248 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008249 //
Andrew Trick88b24502011-10-18 19:18:52 +00008250 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008251
Andrew Trick924123a2011-09-21 02:20:46 +00008252 // Rename pseudo opcodes.
8253 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
8254 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008255 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008256 MCID = &TII->get(NewOpc);
8257
8258 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
8259 "converted opcode should be the same except for cc_out");
8260
8261 MI->setDesc(*MCID);
8262
8263 // Add the optional cc_out operand
8264 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008265 }
Andrew Trick88b24502011-10-18 19:18:52 +00008266 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008267
8268 // Any ARM instruction that sets the 's' bit should specify an optional
8269 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008270 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008271 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008272 return;
8273 }
Andrew Trick924123a2011-09-21 02:20:46 +00008274 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8275 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008276 bool definesCPSR = false;
8277 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008278 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008279 i != e; ++i) {
8280 const MachineOperand &MO = MI->getOperand(i);
8281 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8282 definesCPSR = true;
8283 if (MO.isDead())
8284 deadCPSR = true;
8285 MI->RemoveOperand(i);
8286 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008287 }
8288 }
Andrew Trick8586e622011-09-20 03:17:40 +00008289 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008290 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008291 return;
8292 }
8293 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008294 if (deadCPSR) {
8295 assert(!MI->getOperand(ccOutIdx).getReg() &&
8296 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008297 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008298 }
Andrew Trick8586e622011-09-20 03:17:40 +00008299
Andrew Trick924123a2011-09-21 02:20:46 +00008300 // If this instruction was defined with an optional CPSR def and its dag node
8301 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008302 MachineOperand &MO = MI->getOperand(ccOutIdx);
8303 MO.setReg(ARM::CPSR);
8304 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008305}
8306
Evan Cheng10043e22007-01-19 07:51:42 +00008307//===----------------------------------------------------------------------===//
8308// ARM Optimization Hooks
8309//===----------------------------------------------------------------------===//
8310
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008311// Helper function that checks if N is a null or all ones constant.
8312static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00008313 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008314}
8315
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008316// Return true if N is conditionally 0 or all ones.
8317// Detects these expressions where cc is an i1 value:
8318//
8319// (select cc 0, y) [AllOnes=0]
8320// (select cc y, 0) [AllOnes=0]
8321// (zext cc) [AllOnes=0]
8322// (sext cc) [AllOnes=0/1]
8323// (select cc -1, y) [AllOnes=1]
8324// (select cc y, -1) [AllOnes=1]
8325//
8326// Invert is set when N is the null/all ones constant when CC is false.
8327// OtherOp is set to the alternative value of N.
8328static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8329 SDValue &CC, bool &Invert,
8330 SDValue &OtherOp,
8331 SelectionDAG &DAG) {
8332 switch (N->getOpcode()) {
8333 default: return false;
8334 case ISD::SELECT: {
8335 CC = N->getOperand(0);
8336 SDValue N1 = N->getOperand(1);
8337 SDValue N2 = N->getOperand(2);
8338 if (isZeroOrAllOnes(N1, AllOnes)) {
8339 Invert = false;
8340 OtherOp = N2;
8341 return true;
8342 }
8343 if (isZeroOrAllOnes(N2, AllOnes)) {
8344 Invert = true;
8345 OtherOp = N1;
8346 return true;
8347 }
8348 return false;
8349 }
8350 case ISD::ZERO_EXTEND:
8351 // (zext cc) can never be the all ones value.
8352 if (AllOnes)
8353 return false;
8354 // Fall through.
8355 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008356 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008357 EVT VT = N->getValueType(0);
8358 CC = N->getOperand(0);
8359 if (CC.getValueType() != MVT::i1)
8360 return false;
8361 Invert = !AllOnes;
8362 if (AllOnes)
8363 // When looking for an AllOnes constant, N is an sext, and the 'other'
8364 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008365 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008366 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8367 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008368 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008369 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008370 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8371 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008372 return true;
8373 }
8374 }
8375}
8376
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008377// Combine a constant select operand into its use:
8378//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008379// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8380// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8381// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8382// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8383// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008384//
8385// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008386// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008387//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008388// Also recognize sext/zext from i1:
8389//
8390// (add (zext cc), x) -> (select cc (add x, 1), x)
8391// (add (sext cc), x) -> (select cc (add x, -1), x)
8392//
8393// These transformations eventually create predicated instructions.
8394//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008395// @param N The node to transform.
8396// @param Slct The N operand that is a select.
8397// @param OtherOp The other N operand (x above).
8398// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008399// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008400// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008401static
8402SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008403 TargetLowering::DAGCombinerInfo &DCI,
8404 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008405 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008406 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008407 SDValue NonConstantVal;
8408 SDValue CCOp;
8409 bool SwapSelectOps;
8410 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8411 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008412 return SDValue();
8413
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008414 // Slct is now know to be the desired identity constant when CC is true.
8415 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008416 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008417 OtherOp, NonConstantVal);
8418 // Unless SwapSelectOps says CC should be false.
8419 if (SwapSelectOps)
8420 std::swap(TrueVal, FalseVal);
8421
Andrew Trickef9de2a2013-05-25 02:42:55 +00008422 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008423 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008424}
8425
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008426// Attempt combineSelectAndUse on each operand of a commutative operator N.
8427static
8428SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8429 TargetLowering::DAGCombinerInfo &DCI) {
8430 SDValue N0 = N->getOperand(0);
8431 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008432 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008433 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
8434 if (Result.getNode())
8435 return Result;
8436 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008437 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008438 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
8439 if (Result.getNode())
8440 return Result;
8441 }
8442 return SDValue();
8443}
8444
Eric Christopher1b8b94192011-06-29 21:10:36 +00008445// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008446// (only after legalization).
8447static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8448 TargetLowering::DAGCombinerInfo &DCI,
8449 const ARMSubtarget *Subtarget) {
8450
8451 // Only perform optimization if after legalize, and if NEON is available. We
8452 // also expected both operands to be BUILD_VECTORs.
8453 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8454 || N0.getOpcode() != ISD::BUILD_VECTOR
8455 || N1.getOpcode() != ISD::BUILD_VECTOR)
8456 return SDValue();
8457
8458 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8459 EVT VT = N->getValueType(0);
8460 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8461 return SDValue();
8462
8463 // Check that the vector operands are of the right form.
8464 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8465 // operands, where N is the size of the formed vector.
8466 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8467 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008468
8469 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008470 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008471 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008472 SDValue Vec = N0->getOperand(0)->getOperand(0);
8473 SDNode *V = Vec.getNode();
8474 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008475
Eric Christopher1b8b94192011-06-29 21:10:36 +00008476 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008477 // check to see if each of their operands are an EXTRACT_VECTOR with
8478 // the same vector and appropriate index.
8479 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8480 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8481 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008482
Tanya Lattnere9e67052011-06-14 23:48:48 +00008483 SDValue ExtVec0 = N0->getOperand(i);
8484 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008485
Tanya Lattnere9e67052011-06-14 23:48:48 +00008486 // First operand is the vector, verify its the same.
8487 if (V != ExtVec0->getOperand(0).getNode() ||
8488 V != ExtVec1->getOperand(0).getNode())
8489 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008490
Tanya Lattnere9e67052011-06-14 23:48:48 +00008491 // Second is the constant, verify its correct.
8492 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8493 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008494
Tanya Lattnere9e67052011-06-14 23:48:48 +00008495 // For the constant, we want to see all the even or all the odd.
8496 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8497 || C1->getZExtValue() != nextIndex+1)
8498 return SDValue();
8499
8500 // Increment index.
8501 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008502 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008503 return SDValue();
8504 }
8505
8506 // Create VPADDL node.
8507 SelectionDAG &DAG = DCI.DAG;
8508 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008509
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008510 SDLoc dl(N);
8511
Tanya Lattnere9e67052011-06-14 23:48:48 +00008512 // Build operand list.
8513 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008514 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008515 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008516
8517 // Input is the vector.
8518 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008519
Tanya Lattnere9e67052011-06-14 23:48:48 +00008520 // Get widened type and narrowed type.
8521 MVT widenType;
8522 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00008523
Silviu Barangaa3106e62014-04-03 10:44:27 +00008524 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8525 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008526 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8527 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8528 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8529 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008530 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008531 }
8532
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008533 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008534 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008535 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008536}
8537
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008538static SDValue findMUL_LOHI(SDValue V) {
8539 if (V->getOpcode() == ISD::UMUL_LOHI ||
8540 V->getOpcode() == ISD::SMUL_LOHI)
8541 return V;
8542 return SDValue();
8543}
8544
8545static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8546 TargetLowering::DAGCombinerInfo &DCI,
8547 const ARMSubtarget *Subtarget) {
8548
8549 if (Subtarget->isThumb1Only()) return SDValue();
8550
8551 // Only perform the checks after legalize when the pattern is available.
8552 if (DCI.isBeforeLegalize()) return SDValue();
8553
8554 // Look for multiply add opportunities.
8555 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8556 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8557 // a glue link from the first add to the second add.
8558 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8559 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008560 // UMUL_LOHI
8561 // / :lo \ :hi
8562 // / \ [no multiline comment]
8563 // loAdd -> ADDE |
8564 // \ :glue /
8565 // \ /
8566 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008567 //
8568 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8569 SDValue AddcOp0 = AddcNode->getOperand(0);
8570 SDValue AddcOp1 = AddcNode->getOperand(1);
8571
8572 // Check if the two operands are from the same mul_lohi node.
8573 if (AddcOp0.getNode() == AddcOp1.getNode())
8574 return SDValue();
8575
8576 assert(AddcNode->getNumValues() == 2 &&
8577 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008578 "Expect ADDC with two result values. First: i32");
8579
8580 // Check that we have a glued ADDC node.
8581 if (AddcNode->getValueType(1) != MVT::Glue)
8582 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008583
8584 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8585 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8586 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8587 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8588 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8589 return SDValue();
8590
8591 // Look for the glued ADDE.
8592 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008593 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008594 return SDValue();
8595
8596 // Make sure it is really an ADDE.
8597 if (AddeNode->getOpcode() != ISD::ADDE)
8598 return SDValue();
8599
8600 assert(AddeNode->getNumOperands() == 3 &&
8601 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8602 "ADDE node has the wrong inputs");
8603
8604 // Check for the triangle shape.
8605 SDValue AddeOp0 = AddeNode->getOperand(0);
8606 SDValue AddeOp1 = AddeNode->getOperand(1);
8607
8608 // Make sure that the ADDE operands are not coming from the same node.
8609 if (AddeOp0.getNode() == AddeOp1.getNode())
8610 return SDValue();
8611
8612 // Find the MUL_LOHI node walking up ADDE's operands.
8613 bool IsLeftOperandMUL = false;
8614 SDValue MULOp = findMUL_LOHI(AddeOp0);
8615 if (MULOp == SDValue())
8616 MULOp = findMUL_LOHI(AddeOp1);
8617 else
8618 IsLeftOperandMUL = true;
8619 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008620 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008621
8622 // Figure out the right opcode.
8623 unsigned Opc = MULOp->getOpcode();
8624 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8625
8626 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008627 SDValue* HiAdd = nullptr;
8628 SDValue* LoMul = nullptr;
8629 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008630
Jyoti Allurf1d70502015-01-23 09:10:03 +00008631 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8632 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8633 return SDValue();
8634
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008635 if (IsLeftOperandMUL)
8636 HiAdd = &AddeOp1;
8637 else
8638 HiAdd = &AddeOp0;
8639
8640
Jyoti Allurf1d70502015-01-23 09:10:03 +00008641 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8642 // whose low result is fed to the ADDC we are checking.
8643
8644 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008645 LoMul = &AddcOp0;
8646 LowAdd = &AddcOp1;
8647 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008648 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008649 LoMul = &AddcOp1;
8650 LowAdd = &AddcOp0;
8651 }
8652
Craig Topper062a2ba2014-04-25 05:30:21 +00008653 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008654 return SDValue();
8655
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008656 // Create the merged node.
8657 SelectionDAG &DAG = DCI.DAG;
8658
8659 // Build operand list.
8660 SmallVector<SDValue, 8> Ops;
8661 Ops.push_back(LoMul->getOperand(0));
8662 Ops.push_back(LoMul->getOperand(1));
8663 Ops.push_back(*LowAdd);
8664 Ops.push_back(*HiAdd);
8665
Andrew Trickef9de2a2013-05-25 02:42:55 +00008666 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008667 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008668
8669 // Replace the ADDs' nodes uses by the MLA node's values.
8670 SDValue HiMLALResult(MLALNode.getNode(), 1);
8671 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8672
8673 SDValue LoMLALResult(MLALNode.getNode(), 0);
8674 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8675
8676 // Return original node to notify the driver to stop replacing.
8677 SDValue resNode(AddcNode, 0);
8678 return resNode;
8679}
8680
8681/// PerformADDCCombine - Target-specific dag combine transform from
8682/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8683static SDValue PerformADDCCombine(SDNode *N,
8684 TargetLowering::DAGCombinerInfo &DCI,
8685 const ARMSubtarget *Subtarget) {
8686
8687 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8688
8689}
8690
Bob Wilson728eb292010-07-29 20:34:14 +00008691/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8692/// operands N0 and N1. This is a helper for PerformADDCombine that is
8693/// called with the default operands, and if that fails, with commuted
8694/// operands.
8695static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008696 TargetLowering::DAGCombinerInfo &DCI,
8697 const ARMSubtarget *Subtarget){
8698
8699 // Attempt to create vpaddl for this add.
8700 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8701 if (Result.getNode())
8702 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008703
Chris Lattner4147f082009-03-12 06:52:53 +00008704 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008705 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008706 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8707 if (Result.getNode()) return Result;
8708 }
Chris Lattner4147f082009-03-12 06:52:53 +00008709 return SDValue();
8710}
8711
Bob Wilson728eb292010-07-29 20:34:14 +00008712/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8713///
8714static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008715 TargetLowering::DAGCombinerInfo &DCI,
8716 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008717 SDValue N0 = N->getOperand(0);
8718 SDValue N1 = N->getOperand(1);
8719
8720 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008721 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008722 if (Result.getNode())
8723 return Result;
8724
8725 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008726 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008727}
8728
Chris Lattner4147f082009-03-12 06:52:53 +00008729/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008730///
Chris Lattner4147f082009-03-12 06:52:53 +00008731static SDValue PerformSUBCombine(SDNode *N,
8732 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008733 SDValue N0 = N->getOperand(0);
8734 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008735
Chris Lattner4147f082009-03-12 06:52:53 +00008736 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008737 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008738 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8739 if (Result.getNode()) return Result;
8740 }
Bob Wilson7117a912009-03-20 22:42:55 +00008741
Chris Lattner4147f082009-03-12 06:52:53 +00008742 return SDValue();
8743}
8744
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008745/// PerformVMULCombine
8746/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8747/// special multiplier accumulator forwarding.
8748/// vmul d3, d0, d2
8749/// vmla d3, d1, d2
8750/// is faster than
8751/// vadd d3, d0, d1
8752/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008753// However, for (A + B) * (A + B),
8754// vadd d2, d0, d1
8755// vmul d3, d0, d2
8756// vmla d3, d1, d2
8757// is slower than
8758// vadd d2, d0, d1
8759// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008760static SDValue PerformVMULCombine(SDNode *N,
8761 TargetLowering::DAGCombinerInfo &DCI,
8762 const ARMSubtarget *Subtarget) {
8763 if (!Subtarget->hasVMLxForwarding())
8764 return SDValue();
8765
8766 SelectionDAG &DAG = DCI.DAG;
8767 SDValue N0 = N->getOperand(0);
8768 SDValue N1 = N->getOperand(1);
8769 unsigned Opcode = N0.getOpcode();
8770 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8771 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008772 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008773 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8774 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8775 return SDValue();
8776 std::swap(N0, N1);
8777 }
8778
Weiming Zhao2052f482013-09-25 23:12:06 +00008779 if (N0 == N1)
8780 return SDValue();
8781
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008782 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008783 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008784 SDValue N00 = N0->getOperand(0);
8785 SDValue N01 = N0->getOperand(1);
8786 return DAG.getNode(Opcode, DL, VT,
8787 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8788 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8789}
8790
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008791static SDValue PerformMULCombine(SDNode *N,
8792 TargetLowering::DAGCombinerInfo &DCI,
8793 const ARMSubtarget *Subtarget) {
8794 SelectionDAG &DAG = DCI.DAG;
8795
8796 if (Subtarget->isThumb1Only())
8797 return SDValue();
8798
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008799 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8800 return SDValue();
8801
8802 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008803 if (VT.is64BitVector() || VT.is128BitVector())
8804 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008805 if (VT != MVT::i32)
8806 return SDValue();
8807
8808 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8809 if (!C)
8810 return SDValue();
8811
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008812 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008813 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008814
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008815 ShiftAmt = ShiftAmt & (32 - 1);
8816 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008817 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008818
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008819 SDValue Res;
8820 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008821
8822 if (MulAmt >= 0) {
8823 if (isPowerOf2_32(MulAmt - 1)) {
8824 // (mul x, 2^N + 1) => (add (shl x, N), x)
8825 Res = DAG.getNode(ISD::ADD, DL, VT,
8826 V,
8827 DAG.getNode(ISD::SHL, DL, VT,
8828 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008829 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008830 MVT::i32)));
8831 } else if (isPowerOf2_32(MulAmt + 1)) {
8832 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8833 Res = DAG.getNode(ISD::SUB, DL, VT,
8834 DAG.getNode(ISD::SHL, DL, VT,
8835 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008836 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008837 MVT::i32)),
8838 V);
8839 } else
8840 return SDValue();
8841 } else {
8842 uint64_t MulAmtAbs = -MulAmt;
8843 if (isPowerOf2_32(MulAmtAbs + 1)) {
8844 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8845 Res = DAG.getNode(ISD::SUB, DL, VT,
8846 V,
8847 DAG.getNode(ISD::SHL, DL, VT,
8848 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008849 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008850 MVT::i32)));
8851 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8852 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8853 Res = DAG.getNode(ISD::ADD, DL, VT,
8854 V,
8855 DAG.getNode(ISD::SHL, DL, VT,
8856 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008857 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008858 MVT::i32)));
8859 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008860 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008861
8862 } else
8863 return SDValue();
8864 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008865
8866 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008867 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008868 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008869
8870 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008871 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008872 return SDValue();
8873}
8874
Owen Anderson30c48922010-11-05 19:27:46 +00008875static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008876 TargetLowering::DAGCombinerInfo &DCI,
8877 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008878
Owen Anderson30c48922010-11-05 19:27:46 +00008879 // Attempt to use immediate-form VBIC
8880 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008881 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008882 EVT VT = N->getValueType(0);
8883 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008884
Tanya Lattner266792a2011-04-07 15:24:20 +00008885 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8886 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008887
Owen Anderson30c48922010-11-05 19:27:46 +00008888 APInt SplatBits, SplatUndef;
8889 unsigned SplatBitSize;
8890 bool HasAnyUndefs;
8891 if (BVN &&
8892 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8893 if (SplatBitSize <= 64) {
8894 EVT VbicVT;
8895 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8896 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008897 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008898 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008899 if (Val.getNode()) {
8900 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008901 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008902 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008903 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008904 }
8905 }
8906 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008907
Evan Chenge87681c2012-02-23 01:19:06 +00008908 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008909 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8910 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8911 if (Result.getNode())
8912 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008913 }
8914
Owen Anderson30c48922010-11-05 19:27:46 +00008915 return SDValue();
8916}
8917
Jim Grosbach11013ed2010-07-16 23:05:05 +00008918/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8919static SDValue PerformORCombine(SDNode *N,
8920 TargetLowering::DAGCombinerInfo &DCI,
8921 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008922 // Attempt to use immediate-form VORR
8923 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008924 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008925 EVT VT = N->getValueType(0);
8926 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008927
Tanya Lattner266792a2011-04-07 15:24:20 +00008928 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8929 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008930
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008931 APInt SplatBits, SplatUndef;
8932 unsigned SplatBitSize;
8933 bool HasAnyUndefs;
8934 if (BVN && Subtarget->hasNEON() &&
8935 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8936 if (SplatBitSize <= 64) {
8937 EVT VorrVT;
8938 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8939 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008940 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008941 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008942 if (Val.getNode()) {
8943 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008944 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008945 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008946 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008947 }
8948 }
8949 }
8950
Evan Chenge87681c2012-02-23 01:19:06 +00008951 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008952 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8953 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8954 if (Result.getNode())
8955 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008956 }
8957
Nadav Rotem3a94c542012-08-13 18:52:44 +00008958 // The code below optimizes (or (and X, Y), Z).
8959 // The AND operand needs to have a single user to make these optimizations
8960 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008961 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008962 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008963 return SDValue();
8964 SDValue N1 = N->getOperand(1);
8965
8966 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8967 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8968 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8969 APInt SplatUndef;
8970 unsigned SplatBitSize;
8971 bool HasAnyUndefs;
8972
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008973 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008974 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008975 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8976 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008977 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008978 HasAnyUndefs) && !HasAnyUndefs) {
8979 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8980 HasAnyUndefs) && !HasAnyUndefs) {
8981 // Ensure that the bit width of the constants are the same and that
8982 // the splat arguments are logical inverses as per the pattern we
8983 // are trying to simplify.
8984 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8985 SplatBits0 == ~SplatBits1) {
8986 // Canonicalize the vector type to make instruction selection
8987 // simpler.
8988 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8989 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8990 N0->getOperand(1),
8991 N0->getOperand(0),
8992 N1->getOperand(0));
8993 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8994 }
8995 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008996 }
8997 }
8998
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008999 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
9000 // reasonable.
9001
Jim Grosbach11013ed2010-07-16 23:05:05 +00009002 // BFI is only available on V6T2+
9003 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
9004 return SDValue();
9005
Andrew Trickef9de2a2013-05-25 02:42:55 +00009006 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009007 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009008 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009009 //
9010 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009011 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009012 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009013 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009014 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009015 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009016
Jim Grosbach11013ed2010-07-16 23:05:05 +00009017 if (VT != MVT::i32)
9018 return SDValue();
9019
Evan Cheng2e51bb42010-12-13 20:32:54 +00009020 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009021
Jim Grosbach11013ed2010-07-16 23:05:05 +00009022 // The value and the mask need to be constants so we can verify this is
9023 // actually a bitfield set. If the mask is 0xffff, we can do better
9024 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00009025 SDValue MaskOp = N0.getOperand(1);
9026 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
9027 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009028 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009029 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009030 if (Mask == 0xffff)
9031 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009032 SDValue Res;
9033 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009034 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9035 if (N1C) {
9036 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00009037 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009038 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009039
Evan Cheng34345752010-12-11 04:11:38 +00009040 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009041 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009042
Evan Cheng2e51bb42010-12-13 20:32:54 +00009043 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009044 DAG.getConstant(Val, DL, MVT::i32),
9045 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00009046
9047 // Do not add new nodes to DAG combiner worklist.
9048 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009049 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00009050 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009051 } else if (N1.getOpcode() == ISD::AND) {
9052 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009053 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9054 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009055 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009056 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009057
Eric Christopherd5530962011-03-26 01:21:03 +00009058 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
9059 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009060 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009061 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009062 // The pack halfword instruction works better for masks that fit it,
9063 // so use that when it's available.
9064 if (Subtarget->hasT2ExtractPack() &&
9065 (Mask == 0xffff || Mask == 0xffff0000))
9066 return SDValue();
9067 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009068 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009069 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009070 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009071 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009072 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009073 // Do not add new nodes to DAG combiner worklist.
9074 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009075 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009076 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009077 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009078 // The pack halfword instruction works better for masks that fit it,
9079 // so use that when it's available.
9080 if (Subtarget->hasT2ExtractPack() &&
9081 (Mask2 == 0xffff || Mask2 == 0xffff0000))
9082 return SDValue();
9083 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009084 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009085 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009086 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009087 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009088 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009089 // Do not add new nodes to DAG combiner worklist.
9090 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009091 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009092 }
9093 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009094
Evan Cheng2e51bb42010-12-13 20:32:54 +00009095 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
9096 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
9097 ARM::isBitFieldInvertedMask(~Mask)) {
9098 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
9099 // where lsb(mask) == #shamt and masked bits of B are known zero.
9100 SDValue ShAmt = N00.getOperand(1);
9101 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009102 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009103 if (ShAmtC != LSB)
9104 return SDValue();
9105
9106 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009107 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009108
9109 // Do not add new nodes to DAG combiner worklist.
9110 DCI.CombineTo(N, Res, false);
9111 }
9112
Jim Grosbach11013ed2010-07-16 23:05:05 +00009113 return SDValue();
9114}
9115
Evan Chenge87681c2012-02-23 01:19:06 +00009116static SDValue PerformXORCombine(SDNode *N,
9117 TargetLowering::DAGCombinerInfo &DCI,
9118 const ARMSubtarget *Subtarget) {
9119 EVT VT = N->getValueType(0);
9120 SelectionDAG &DAG = DCI.DAG;
9121
9122 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9123 return SDValue();
9124
9125 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009126 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
9127 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
9128 if (Result.getNode())
9129 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009130 }
9131
9132 return SDValue();
9133}
9134
James Molloyce12c922015-11-11 15:40:40 +00009135// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9136// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9137// their position in "to" (Rd).
9138static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9139 assert(N->getOpcode() == ARMISD::BFI);
Chad Rosier353d7192015-12-21 18:08:05 +00009140
James Molloyce12c922015-11-11 15:40:40 +00009141 SDValue From = N->getOperand(1);
9142 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9143 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9144
9145 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9146 // #C in the base of the SHR.
9147 if (From->getOpcode() == ISD::SRL &&
9148 isa<ConstantSDNode>(From->getOperand(1))) {
9149 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9150 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9151 FromMask <<= Shift.getLimitedValue(31);
9152 From = From->getOperand(0);
9153 }
9154
9155 return From;
9156}
9157
9158// If A and B contain one contiguous set of bits, does A | B == A . B?
9159//
9160// Neither A nor B must be zero.
9161static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9162 unsigned LastActiveBitInA = A.countTrailingZeros();
9163 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9164 return LastActiveBitInA - 1 == FirstActiveBitInB;
9165}
9166
9167static SDValue FindBFIToCombineWith(SDNode *N) {
9168 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9169 // if one exists.
9170 APInt ToMask, FromMask;
9171 SDValue From = ParseBFI(N, ToMask, FromMask);
9172 SDValue To = N->getOperand(0);
9173
9174 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9175 // aren't compatible, but not if they set the same bit in their destination as
9176 // we do (or that of any BFI we're going to combine with).
9177 SDValue V = To;
9178 APInt CombinedToMask = ToMask;
9179 while (V.getOpcode() == ARMISD::BFI) {
9180 APInt NewToMask, NewFromMask;
9181 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
9182 if (NewFrom != From) {
9183 // This BFI has a different base. Keep going.
9184 CombinedToMask |= NewToMask;
9185 V = V.getOperand(0);
9186 continue;
9187 }
9188
9189 // Do the written bits conflict with any we've seen so far?
9190 if ((NewToMask & CombinedToMask).getBoolValue())
9191 // Conflicting bits - bail out because going further is unsafe.
9192 return SDValue();
9193
9194 // Are the new bits contiguous when combined with the old bits?
9195 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
9196 BitsProperlyConcatenate(FromMask, NewFromMask))
9197 return V;
9198 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
9199 BitsProperlyConcatenate(NewFromMask, FromMask))
9200 return V;
Chad Rosier353d7192015-12-21 18:08:05 +00009201
James Molloyce12c922015-11-11 15:40:40 +00009202 // We've seen a write to some bits, so track it.
9203 CombinedToMask |= NewToMask;
9204 // Keep going...
9205 V = V.getOperand(0);
9206 }
9207
9208 return SDValue();
9209}
9210
Evan Chengc1778132010-12-14 03:22:07 +00009211static SDValue PerformBFICombine(SDNode *N,
9212 TargetLowering::DAGCombinerInfo &DCI) {
9213 SDValue N1 = N->getOperand(1);
9214 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +00009215 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9216 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009217 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9218 if (!N11C)
9219 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009220 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009221 unsigned LSB = countTrailingZeros(~InvMask);
9222 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009223 assert(Width <
9224 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009225 "undefined behavior");
9226 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009227 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009228 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009229 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009230 N->getOperand(0), N1.getOperand(0),
9231 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +00009232 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
9233 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
9234 // Keep track of any consecutive bits set that all come from the same base
9235 // value. We can combine these together into a single BFI.
9236 SDValue CombineBFI = FindBFIToCombineWith(N);
9237 if (CombineBFI == SDValue())
9238 return SDValue();
9239
9240 // We've found a BFI.
9241 APInt ToMask1, FromMask1;
9242 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
9243
9244 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +00009245 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
9246 assert(From1 == From2);
9247 (void)From2;
Chad Rosier353d7192015-12-21 18:08:05 +00009248
James Molloyce12c922015-11-11 15:40:40 +00009249 // First, unlink CombineBFI.
9250 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
9251 // Then create a new BFI, combining the two together.
9252 APInt NewFromMask = FromMask1 | FromMask2;
9253 APInt NewToMask = ToMask1 | ToMask2;
9254
9255 EVT VT = N->getValueType(0);
9256 SDLoc dl(N);
9257
9258 if (NewFromMask[0] == 0)
9259 From1 = DCI.DAG.getNode(
9260 ISD::SRL, dl, VT, From1,
9261 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
9262 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
9263 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +00009264 }
9265 return SDValue();
9266}
9267
Bob Wilson22806742010-09-22 22:09:21 +00009268/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9269/// ARMISD::VMOVRRD.
9270static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009271 TargetLowering::DAGCombinerInfo &DCI,
9272 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009273 // vmovrrd(vmovdrr x, y) -> x,y
9274 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009275 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009276 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009277
9278 // vmovrrd(load f64) -> (load i32), (load i32)
9279 SDNode *InNode = InDouble.getNode();
9280 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9281 InNode->getValueType(0) == MVT::f64 &&
9282 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9283 !cast<LoadSDNode>(InNode)->isVolatile()) {
9284 // TODO: Should this be done for non-FrameIndex operands?
9285 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9286
9287 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009288 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009289 SDValue BasePtr = LD->getBasePtr();
9290 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
9291 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009292 LD->isNonTemporal(), LD->isInvariant(),
9293 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009294
9295 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009296 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009297 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
9298 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009299 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009300 std::min(4U, LD->getAlignment() / 2));
9301
9302 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009303 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009304 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009305 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009306 return Result;
9307 }
9308
Bob Wilson22806742010-09-22 22:09:21 +00009309 return SDValue();
9310}
9311
9312/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9313/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9314static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9315 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9316 SDValue Op0 = N->getOperand(0);
9317 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009318 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009319 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009320 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009321 Op1 = Op1.getOperand(0);
9322 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9323 Op0.getNode() == Op1.getNode() &&
9324 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009325 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009326 N->getValueType(0), Op0.getOperand(0));
9327 return SDValue();
9328}
9329
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009330/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9331/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9332/// i64 vector to have f64 elements, since the value can then be loaded
9333/// directly into a VFP register.
9334static bool hasNormalLoadOperand(SDNode *N) {
9335 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9336 for (unsigned i = 0; i < NumElts; ++i) {
9337 SDNode *Elt = N->getOperand(i).getNode();
9338 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9339 return true;
9340 }
9341 return false;
9342}
9343
Bob Wilsoncb6db982010-09-17 22:59:05 +00009344/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9345/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009346static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009347 TargetLowering::DAGCombinerInfo &DCI,
9348 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009349 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9350 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9351 // into a pair of GPRs, which is fine when the value is used as a scalar,
9352 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009353 SelectionDAG &DAG = DCI.DAG;
9354 if (N->getNumOperands() == 2) {
9355 SDValue RV = PerformVMOVDRRCombine(N, DAG);
9356 if (RV.getNode())
9357 return RV;
9358 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00009359
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009360 // Load i64 elements as f64 values so that type legalization does not split
9361 // them up into i32 values.
9362 EVT VT = N->getValueType(0);
9363 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9364 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009365 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009366 SmallVector<SDValue, 8> Ops;
9367 unsigned NumElts = VT.getVectorNumElements();
9368 for (unsigned i = 0; i < NumElts; ++i) {
9369 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9370 Ops.push_back(V);
9371 // Make the DAGCombiner fold the bitcast.
9372 DCI.AddToWorklist(V.getNode());
9373 }
9374 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00009375 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009376 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9377}
9378
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009379/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9380static SDValue
9381PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9382 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9383 // At that time, we may have inserted bitcasts from integer to float.
9384 // If these bitcasts have survived DAGCombine, change the lowering of this
9385 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9386 // force to use floating point types.
9387
9388 // Make sure we can change the type of the vector.
9389 // This is possible iff:
9390 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9391 // 1.1. Vector is used only once.
9392 // 1.2. Use is a bit convert to an integer type.
9393 // 2. The size of its operands are 32-bits (64-bits are not legal).
9394 EVT VT = N->getValueType(0);
9395 EVT EltVT = VT.getVectorElementType();
9396
9397 // Check 1.1. and 2.
9398 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9399 return SDValue();
9400
9401 // By construction, the input type must be float.
9402 assert(EltVT == MVT::f32 && "Unexpected type!");
9403
9404 // Check 1.2.
9405 SDNode *Use = *N->use_begin();
9406 if (Use->getOpcode() != ISD::BITCAST ||
9407 Use->getValueType(0).isFloatingPoint())
9408 return SDValue();
9409
9410 // Check profitability.
9411 // Model is, if more than half of the relevant operands are bitcast from
9412 // i32, turn the build_vector into a sequence of insert_vector_elt.
9413 // Relevant operands are everything that is not statically
9414 // (i.e., at compile time) bitcasted.
9415 unsigned NumOfBitCastedElts = 0;
9416 unsigned NumElts = VT.getVectorNumElements();
9417 unsigned NumOfRelevantElts = NumElts;
9418 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9419 SDValue Elt = N->getOperand(Idx);
9420 if (Elt->getOpcode() == ISD::BITCAST) {
9421 // Assume only bit cast to i32 will go away.
9422 if (Elt->getOperand(0).getValueType() == MVT::i32)
9423 ++NumOfBitCastedElts;
9424 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
9425 // Constants are statically casted, thus do not count them as
9426 // relevant operands.
9427 --NumOfRelevantElts;
9428 }
9429
9430 // Check if more than half of the elements require a non-free bitcast.
9431 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9432 return SDValue();
9433
9434 SelectionDAG &DAG = DCI.DAG;
9435 // Create the new vector type.
9436 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9437 // Check if the type is legal.
9438 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9439 if (!TLI.isTypeLegal(VecVT))
9440 return SDValue();
9441
9442 // Combine:
9443 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9444 // => BITCAST INSERT_VECTOR_ELT
9445 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9446 // (BITCAST EN), N.
9447 SDValue Vec = DAG.getUNDEF(VecVT);
9448 SDLoc dl(N);
9449 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9450 SDValue V = N->getOperand(Idx);
9451 if (V.getOpcode() == ISD::UNDEF)
9452 continue;
9453 if (V.getOpcode() == ISD::BITCAST &&
9454 V->getOperand(0).getValueType() == MVT::i32)
9455 // Fold obvious case.
9456 V = V.getOperand(0);
9457 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009458 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009459 // Make the DAGCombiner fold the bitcasts.
9460 DCI.AddToWorklist(V.getNode());
9461 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009462 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009463 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9464 }
9465 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9466 // Make the DAGCombiner fold the bitcasts.
9467 DCI.AddToWorklist(Vec.getNode());
9468 return Vec;
9469}
9470
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009471/// PerformInsertEltCombine - Target-specific dag combine xforms for
9472/// ISD::INSERT_VECTOR_ELT.
9473static SDValue PerformInsertEltCombine(SDNode *N,
9474 TargetLowering::DAGCombinerInfo &DCI) {
9475 // Bitcast an i64 load inserted into a vector to f64.
9476 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9477 EVT VT = N->getValueType(0);
9478 SDNode *Elt = N->getOperand(1).getNode();
9479 if (VT.getVectorElementType() != MVT::i64 ||
9480 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9481 return SDValue();
9482
9483 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009484 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009485 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9486 VT.getVectorNumElements());
9487 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9488 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9489 // Make the DAGCombiner fold the bitcasts.
9490 DCI.AddToWorklist(Vec.getNode());
9491 DCI.AddToWorklist(V.getNode());
9492 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9493 Vec, V, N->getOperand(2));
9494 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009495}
9496
Bob Wilsonc7334a12010-10-27 20:38:28 +00009497/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9498/// ISD::VECTOR_SHUFFLE.
9499static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9500 // The LLVM shufflevector instruction does not require the shuffle mask
9501 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9502 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9503 // operands do not match the mask length, they are extended by concatenating
9504 // them with undef vectors. That is probably the right thing for other
9505 // targets, but for NEON it is better to concatenate two double-register
9506 // size vector operands into a single quad-register size vector. Do that
9507 // transformation here:
9508 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9509 // shuffle(concat(v1, v2), undef)
9510 SDValue Op0 = N->getOperand(0);
9511 SDValue Op1 = N->getOperand(1);
9512 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9513 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9514 Op0.getNumOperands() != 2 ||
9515 Op1.getNumOperands() != 2)
9516 return SDValue();
9517 SDValue Concat0Op1 = Op0.getOperand(1);
9518 SDValue Concat1Op1 = Op1.getOperand(1);
9519 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
9520 Concat1Op1.getOpcode() != ISD::UNDEF)
9521 return SDValue();
9522 // Skip the transformation if any of the types are illegal.
9523 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9524 EVT VT = N->getValueType(0);
9525 if (!TLI.isTypeLegal(VT) ||
9526 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9527 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9528 return SDValue();
9529
Andrew Trickef9de2a2013-05-25 02:42:55 +00009530 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009531 Op0.getOperand(0), Op1.getOperand(0));
9532 // Translate the shuffle mask.
9533 SmallVector<int, 16> NewMask;
9534 unsigned NumElts = VT.getVectorNumElements();
9535 unsigned HalfElts = NumElts/2;
9536 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9537 for (unsigned n = 0; n < NumElts; ++n) {
9538 int MaskElt = SVN->getMaskElt(n);
9539 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009540 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009541 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009542 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009543 NewElt = HalfElts + MaskElt - NumElts;
9544 NewMask.push_back(NewElt);
9545 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009546 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009547 DAG.getUNDEF(VT), NewMask.data());
9548}
9549
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009550/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9551/// NEON load/store intrinsics, and generic vector load/stores, to merge
9552/// base address updates.
9553/// For generic load/stores, the memory type is assumed to be a vector.
9554/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009555static SDValue CombineBaseUpdate(SDNode *N,
9556 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009557 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009558 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9559 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009560 const bool isStore = N->getOpcode() == ISD::STORE;
9561 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009562 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009563 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009564 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009565
9566 // Search for a use of the address operand that is an increment.
9567 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9568 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9569 SDNode *User = *UI;
9570 if (User->getOpcode() != ISD::ADD ||
9571 UI.getUse().getResNo() != Addr.getResNo())
9572 continue;
9573
9574 // Check that the add is independent of the load/store. Otherwise, folding
9575 // it would create a cycle.
9576 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9577 continue;
9578
9579 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009580 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009581 bool isLaneOp = false;
9582 unsigned NewOpc = 0;
9583 unsigned NumVecs = 0;
9584 if (isIntrinsic) {
9585 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9586 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009587 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009588 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9589 NumVecs = 1; break;
9590 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9591 NumVecs = 2; break;
9592 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9593 NumVecs = 3; break;
9594 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9595 NumVecs = 4; break;
9596 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9597 NumVecs = 2; isLaneOp = true; break;
9598 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9599 NumVecs = 3; isLaneOp = true; break;
9600 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9601 NumVecs = 4; isLaneOp = true; break;
9602 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009603 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009604 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009605 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009606 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009607 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009608 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009609 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009610 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009611 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009612 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009613 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009614 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009615 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009616 }
9617 } else {
9618 isLaneOp = true;
9619 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009620 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009621 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9622 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9623 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009624 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9625 NumVecs = 1; isLaneOp = false; break;
9626 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9627 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009628 }
9629 }
9630
9631 // Find the size of memory referenced by the load/store.
9632 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009633 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009634 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009635 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009636 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009637 } else {
9638 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9639 VecTy = N->getOperand(1).getValueType();
9640 }
9641
Bob Wilson06fce872011-02-07 17:43:21 +00009642 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9643 if (isLaneOp)
9644 NumBytes /= VecTy.getVectorNumElements();
9645
9646 // If the increment is a constant, it must match the memory ref size.
9647 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9648 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9649 uint64_t IncVal = CInc->getZExtValue();
9650 if (IncVal != NumBytes)
9651 continue;
9652 } else if (NumBytes >= 3 * 16) {
9653 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9654 // separate instructions that make it harder to use a non-constant update.
9655 continue;
9656 }
9657
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009658 // OK, we found an ADD we can fold into the base update.
9659 // Now, create a _UPD node, taking care of not breaking alignment.
9660
9661 EVT AlignedVecTy = VecTy;
9662 unsigned Alignment = MemN->getAlignment();
9663
9664 // If this is a less-than-standard-aligned load/store, change the type to
9665 // match the standard alignment.
9666 // The alignment is overlooked when selecting _UPD variants; and it's
9667 // easier to introduce bitcasts here than fix that.
9668 // There are 3 ways to get to this base-update combine:
9669 // - intrinsics: they are assumed to be properly aligned (to the standard
9670 // alignment of the memory type), so we don't need to do anything.
9671 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9672 // intrinsics, so, likewise, there's nothing to do.
9673 // - generic load/store instructions: the alignment is specified as an
9674 // explicit operand, rather than implicitly as the standard alignment
9675 // of the memory type (like the intrisics). We need to change the
9676 // memory type to match the explicit alignment. That way, we don't
9677 // generate non-standard-aligned ARMISD::VLDx nodes.
9678 if (isa<LSBaseSDNode>(N)) {
9679 if (Alignment == 0)
9680 Alignment = 1;
9681 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9682 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9683 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9684 assert(!isLaneOp && "Unexpected generic load/store lane.");
9685 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9686 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9687 }
9688 // Don't set an explicit alignment on regular load/stores that we want
9689 // to transform to VLD/VST 1_UPD nodes.
9690 // This matches the behavior of regular load/stores, which only get an
9691 // explicit alignment if the MMO alignment is larger than the standard
9692 // alignment of the memory type.
9693 // Intrinsics, however, always get an explicit alignment, set to the
9694 // alignment of the MMO.
9695 Alignment = 1;
9696 }
9697
Bob Wilson06fce872011-02-07 17:43:21 +00009698 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009699 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009700 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009701 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009702 unsigned n;
9703 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009704 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009705 Tys[n++] = MVT::i32;
9706 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009707 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009708
9709 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009710 SmallVector<SDValue, 8> Ops;
9711 Ops.push_back(N->getOperand(0)); // incoming chain
9712 Ops.push_back(N->getOperand(AddrOpIdx));
9713 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009714
9715 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9716 // Try to match the intrinsic's signature
9717 Ops.push_back(StN->getValue());
9718 } else {
9719 // Loads (and of course intrinsics) match the intrinsics' signature,
9720 // so just add all but the alignment operand.
9721 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9722 Ops.push_back(N->getOperand(i));
9723 }
9724
9725 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009726 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009727
9728 // If this is a non-standard-aligned STORE, the penultimate operand is the
9729 // stored value. Bitcast it to the aligned type.
9730 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9731 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009732 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009733 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009734
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009735 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009736 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009737 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009738
9739 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009740 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009741 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009742 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009743
9744 // If this is an non-standard-aligned LOAD, the first result is the loaded
9745 // value. Bitcast it to the expected result type.
9746 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9747 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009748 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009749 }
9750
Bob Wilson06fce872011-02-07 17:43:21 +00009751 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9752 DCI.CombineTo(N, NewResults);
9753 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9754
9755 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009756 }
Bob Wilson06fce872011-02-07 17:43:21 +00009757 return SDValue();
9758}
9759
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009760static SDValue PerformVLDCombine(SDNode *N,
9761 TargetLowering::DAGCombinerInfo &DCI) {
9762 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9763 return SDValue();
9764
9765 return CombineBaseUpdate(N, DCI);
9766}
9767
Bob Wilson2d790df2010-11-28 06:51:26 +00009768/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9769/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9770/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9771/// return true.
9772static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9773 SelectionDAG &DAG = DCI.DAG;
9774 EVT VT = N->getValueType(0);
9775 // vldN-dup instructions only support 64-bit vectors for N > 1.
9776 if (!VT.is64BitVector())
9777 return false;
9778
9779 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9780 SDNode *VLD = N->getOperand(0).getNode();
9781 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9782 return false;
9783 unsigned NumVecs = 0;
9784 unsigned NewOpc = 0;
9785 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9786 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9787 NumVecs = 2;
9788 NewOpc = ARMISD::VLD2DUP;
9789 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9790 NumVecs = 3;
9791 NewOpc = ARMISD::VLD3DUP;
9792 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9793 NumVecs = 4;
9794 NewOpc = ARMISD::VLD4DUP;
9795 } else {
9796 return false;
9797 }
9798
9799 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9800 // numbers match the load.
9801 unsigned VLDLaneNo =
9802 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9803 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9804 UI != UE; ++UI) {
9805 // Ignore uses of the chain result.
9806 if (UI.getUse().getResNo() == NumVecs)
9807 continue;
9808 SDNode *User = *UI;
9809 if (User->getOpcode() != ARMISD::VDUPLANE ||
9810 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9811 return false;
9812 }
9813
9814 // Create the vldN-dup node.
9815 EVT Tys[5];
9816 unsigned n;
9817 for (n = 0; n < NumVecs; ++n)
9818 Tys[n] = VT;
9819 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009820 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009821 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9822 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009823 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009824 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009825 VLDMemInt->getMemOperand());
9826
9827 // Update the uses.
9828 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9829 UI != UE; ++UI) {
9830 unsigned ResNo = UI.getUse().getResNo();
9831 // Ignore uses of the chain result.
9832 if (ResNo == NumVecs)
9833 continue;
9834 SDNode *User = *UI;
9835 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9836 }
9837
9838 // Now the vldN-lane intrinsic is dead except for its chain result.
9839 // Update uses of the chain.
9840 std::vector<SDValue> VLDDupResults;
9841 for (unsigned n = 0; n < NumVecs; ++n)
9842 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9843 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9844 DCI.CombineTo(VLD, VLDDupResults);
9845
9846 return true;
9847}
9848
Bob Wilson103a0dc2010-07-14 01:22:12 +00009849/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9850/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009851static SDValue PerformVDUPLANECombine(SDNode *N,
9852 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009853 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009854
Bob Wilson2d790df2010-11-28 06:51:26 +00009855 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9856 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9857 if (CombineVLDDUP(N, DCI))
9858 return SDValue(N, 0);
9859
9860 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9861 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009862 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009863 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009864 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009865 return SDValue();
9866
9867 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9868 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9869 // The canonical VMOV for a zero vector uses a 32-bit element size.
9870 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9871 unsigned EltBits;
9872 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9873 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009874 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009875 if (EltSize > VT.getVectorElementType().getSizeInBits())
9876 return SDValue();
9877
Andrew Trickef9de2a2013-05-25 02:42:55 +00009878 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009879}
9880
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009881static SDValue PerformLOADCombine(SDNode *N,
9882 TargetLowering::DAGCombinerInfo &DCI) {
9883 EVT VT = N->getValueType(0);
9884
9885 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9886 if (ISD::isNormalLoad(N) && VT.isVector() &&
9887 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9888 return CombineBaseUpdate(N, DCI);
9889
9890 return SDValue();
9891}
9892
Ahmed Bougacha23167462014-12-09 21:26:53 +00009893/// PerformSTORECombine - Target-specific dag combine xforms for
9894/// ISD::STORE.
9895static SDValue PerformSTORECombine(SDNode *N,
9896 TargetLowering::DAGCombinerInfo &DCI) {
9897 StoreSDNode *St = cast<StoreSDNode>(N);
9898 if (St->isVolatile())
9899 return SDValue();
9900
9901 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9902 // pack all of the elements in one place. Next, store to memory in fewer
9903 // chunks.
9904 SDValue StVal = St->getValue();
9905 EVT VT = StVal.getValueType();
9906 if (St->isTruncatingStore() && VT.isVector()) {
9907 SelectionDAG &DAG = DCI.DAG;
9908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9909 EVT StVT = St->getMemoryVT();
9910 unsigned NumElems = VT.getVectorNumElements();
9911 assert(StVT != VT && "Cannot truncate to the same type");
9912 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9913 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9914
9915 // From, To sizes and ElemCount must be pow of two
9916 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9917
9918 // We are going to use the original vector elt for storing.
9919 // Accumulated smaller vector elements must be a multiple of the store size.
9920 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9921
9922 unsigned SizeRatio = FromEltSz / ToEltSz;
9923 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9924
9925 // Create a type on which we perform the shuffle.
9926 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9927 NumElems*SizeRatio);
9928 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9929
9930 SDLoc DL(St);
9931 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9932 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9933 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009934 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9935 ? (i + 1) * SizeRatio - 1
9936 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009937
9938 // Can't shuffle using an illegal type.
9939 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9940
9941 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9942 DAG.getUNDEF(WideVec.getValueType()),
9943 ShuffleVec.data());
9944 // At this point all of the data is stored at the bottom of the
9945 // register. We now need to save it to mem.
9946
9947 // Find the largest store unit
9948 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009949 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009950 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9951 StoreType = Tp;
9952 }
9953 // Didn't find a legal store type.
9954 if (!TLI.isTypeLegal(StoreType))
9955 return SDValue();
9956
9957 // Bitcast the original vector into a vector of store-size units
9958 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9959 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9960 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9961 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9962 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +00009963 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
9964 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009965 SDValue BasePtr = St->getBasePtr();
9966
9967 // Perform one or more big stores into memory.
9968 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9969 for (unsigned I = 0; I < E; I++) {
9970 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9971 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009972 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009973 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9974 St->getPointerInfo(), St->isVolatile(),
9975 St->isNonTemporal(), St->getAlignment());
9976 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9977 Increment);
9978 Chains.push_back(Ch);
9979 }
9980 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9981 }
9982
9983 if (!ISD::isNormalStore(St))
9984 return SDValue();
9985
9986 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9987 // ARM stores of arguments in the same cache line.
9988 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9989 StVal.getNode()->hasOneUse()) {
9990 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +00009991 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +00009992 SDLoc DL(St);
9993 SDValue BasePtr = St->getBasePtr();
9994 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9995 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9996 BasePtr, St->getPointerInfo(), St->isVolatile(),
9997 St->isNonTemporal(), St->getAlignment());
9998
9999 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010000 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010001 return DAG.getStore(NewST1.getValue(0), DL,
10002 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
10003 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
10004 St->isNonTemporal(),
10005 std::min(4U, St->getAlignment() / 2));
10006 }
10007
10008 if (StVal.getValueType() == MVT::i64 &&
10009 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10010
10011 // Bitcast an i64 store extracted from a vector to f64.
10012 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10013 SelectionDAG &DAG = DCI.DAG;
10014 SDLoc dl(StVal);
10015 SDValue IntVec = StVal.getOperand(0);
10016 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10017 IntVec.getValueType().getVectorNumElements());
10018 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
10019 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
10020 Vec, StVal.getOperand(1));
10021 dl = SDLoc(N);
10022 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
10023 // Make the DAGCombiner fold the bitcasts.
10024 DCI.AddToWorklist(Vec.getNode());
10025 DCI.AddToWorklist(ExtElt.getNode());
10026 DCI.AddToWorklist(V.getNode());
10027 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
10028 St->getPointerInfo(), St->isVolatile(),
10029 St->isNonTemporal(), St->getAlignment(),
10030 St->getAAInfo());
10031 }
10032
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010033 // If this is a legal vector store, try to combine it into a VST1_UPD.
10034 if (ISD::isNormalStore(N) && VT.isVector() &&
10035 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10036 return CombineBaseUpdate(N, DCI);
10037
Ahmed Bougacha23167462014-12-09 21:26:53 +000010038 return SDValue();
10039}
10040
Chad Rosierfa8d8932011-06-24 19:23:04 +000010041/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
10042/// can replace combinations of VMUL and VCVT (floating-point to integer)
10043/// when the VMUL has a constant operand that is a power of 2.
10044///
10045/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10046/// vmul.f32 d16, d17, d16
10047/// vcvt.s32.f32 d16, d16
10048/// becomes:
10049/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010050static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010051 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010052 if (!Subtarget->hasNEON())
10053 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010054
Chad Rosiera087fd22015-10-06 20:23:42 +000010055 SDValue Op = N->getOperand(0);
10056 if (!Op.getValueType().isVector() || Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +000010057 return SDValue();
10058
Chad Rosierfa8d8932011-06-24 19:23:04 +000010059 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010060 if (!isa<BuildVectorSDNode>(ConstVec))
10061 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010062
Tim Northover7cbc2152013-06-28 15:29:25 +000010063 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010064 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010065 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010066 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +000010067 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010068 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010069 // These instructions only exist converting from f32 to i32. We can handle
10070 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +000010071 // be lossy. We also can't handle more then 4 lanes, since these intructions
10072 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010073 return SDValue();
10074 }
10075
Chad Rosier169865f2015-10-07 17:28:58 +000010076 BitVector UndefElements;
10077 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10078 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10079 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010080 return SDValue();
10081
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010082 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010083 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010084 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
10085 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +000010086 SDValue FixConv = DAG.getNode(
10087 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
10088 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +000010089 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +000010090
Chad Rosier9df4aff2015-10-06 20:45:45 +000010091 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010092 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +000010093
10094 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010095}
10096
10097/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
10098/// can replace combinations of VCVT (integer to floating-point) and VDIV
10099/// when the VDIV has a constant operand that is a power of 2.
10100///
10101/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10102/// vcvt.f32.s32 d16, d16
10103/// vdiv.f32 d16, d17, d16
10104/// becomes:
10105/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010106static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010107 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010108 if (!Subtarget->hasNEON())
10109 return SDValue();
10110
Chad Rosierfa8d8932011-06-24 19:23:04 +000010111 SDValue Op = N->getOperand(0);
10112 unsigned OpOpcode = Op.getNode()->getOpcode();
Chad Rosiera087fd22015-10-06 20:23:42 +000010113 if (!N->getValueType(0).isVector() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +000010114 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
10115 return SDValue();
10116
Chad Rosierfa8d8932011-06-24 19:23:04 +000010117 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010118 if (!isa<BuildVectorSDNode>(ConstVec))
10119 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010120
Tim Northover7cbc2152013-06-28 15:29:25 +000010121 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010122 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010123 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010124 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010125 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10126 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010127 // These instructions only exist converting from i32 to f32. We can handle
10128 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010129 // be lossy. We also can't handle more then 4 lanes, since these intructions
10130 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010131 return SDValue();
10132 }
10133
Chad Rosier169865f2015-10-07 17:28:58 +000010134 BitVector UndefElements;
10135 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10136 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10137 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010138 return SDValue();
10139
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010140 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010141 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010142 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010143 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010144 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010145 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010146 ConvInput);
10147
Eric Christopher1b8b94192011-06-29 21:10:36 +000010148 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010149 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010150 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010151 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010152 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010153 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010154}
10155
10156/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010157/// operand of a vector shift operation, where all the elements of the
10158/// build_vector must have the same constant integer value.
10159static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10160 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010161 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010162 Op = Op.getOperand(0);
10163 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10164 APInt SplatBits, SplatUndef;
10165 unsigned SplatBitSize;
10166 bool HasAnyUndefs;
10167 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10168 HasAnyUndefs, ElementBits) ||
10169 SplatBitSize > ElementBits)
10170 return false;
10171 Cnt = SplatBits.getSExtValue();
10172 return true;
10173}
10174
10175/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10176/// operand of a vector shift left operation. That value must be in the range:
10177/// 0 <= Value < ElementBits for a left shift; or
10178/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010179static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010180 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010181 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010182 if (! getVShiftImm(Op, ElementBits, Cnt))
10183 return false;
10184 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10185}
10186
10187/// isVShiftRImm - Check if this is a valid build_vector for the immediate
10188/// operand of a vector shift right operation. For a shift opcode, the value
10189/// is positive, but for an intrinsic the value count must be negative. The
10190/// absolute value must be in the range:
10191/// 1 <= |Value| <= ElementBits for a right shift; or
10192/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010193static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000010194 int64_t &Cnt) {
10195 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010196 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010197 if (! getVShiftImm(Op, ElementBits, Cnt))
10198 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010199 if (!isIntrinsic)
10200 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
10201 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010202 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010203 return true;
10204 }
10205 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000010206}
10207
10208/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
10209static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
10210 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
10211 switch (IntNo) {
10212 default:
10213 // Don't do anything for most intrinsics.
10214 break;
10215
10216 // Vector shifts: check for immediate versions and lower them.
10217 // Note: This is done during DAG combining instead of DAG legalizing because
10218 // the build_vectors for 64-bit vector element shift counts are generally
10219 // not legal, and it is hard to see their values after they get legalized to
10220 // loads from a constant pool.
10221 case Intrinsic::arm_neon_vshifts:
10222 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010223 case Intrinsic::arm_neon_vrshifts:
10224 case Intrinsic::arm_neon_vrshiftu:
10225 case Intrinsic::arm_neon_vrshiftn:
10226 case Intrinsic::arm_neon_vqshifts:
10227 case Intrinsic::arm_neon_vqshiftu:
10228 case Intrinsic::arm_neon_vqshiftsu:
10229 case Intrinsic::arm_neon_vqshiftns:
10230 case Intrinsic::arm_neon_vqshiftnu:
10231 case Intrinsic::arm_neon_vqshiftnsu:
10232 case Intrinsic::arm_neon_vqrshiftns:
10233 case Intrinsic::arm_neon_vqrshiftnu:
10234 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010235 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010236 int64_t Cnt;
10237 unsigned VShiftOpc = 0;
10238
10239 switch (IntNo) {
10240 case Intrinsic::arm_neon_vshifts:
10241 case Intrinsic::arm_neon_vshiftu:
10242 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10243 VShiftOpc = ARMISD::VSHL;
10244 break;
10245 }
10246 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10247 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10248 ARMISD::VSHRs : ARMISD::VSHRu);
10249 break;
10250 }
10251 return SDValue();
10252
Bob Wilson2e076c42009-06-22 23:27:02 +000010253 case Intrinsic::arm_neon_vrshifts:
10254 case Intrinsic::arm_neon_vrshiftu:
10255 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10256 break;
10257 return SDValue();
10258
10259 case Intrinsic::arm_neon_vqshifts:
10260 case Intrinsic::arm_neon_vqshiftu:
10261 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10262 break;
10263 return SDValue();
10264
10265 case Intrinsic::arm_neon_vqshiftsu:
10266 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10267 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010268 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010269
Bob Wilson2e076c42009-06-22 23:27:02 +000010270 case Intrinsic::arm_neon_vrshiftn:
10271 case Intrinsic::arm_neon_vqshiftns:
10272 case Intrinsic::arm_neon_vqshiftnu:
10273 case Intrinsic::arm_neon_vqshiftnsu:
10274 case Intrinsic::arm_neon_vqrshiftns:
10275 case Intrinsic::arm_neon_vqrshiftnu:
10276 case Intrinsic::arm_neon_vqrshiftnsu:
10277 // Narrowing shifts require an immediate right shift.
10278 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10279 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010280 llvm_unreachable("invalid shift count for narrowing vector shift "
10281 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010282
10283 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010284 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010285 }
10286
10287 switch (IntNo) {
10288 case Intrinsic::arm_neon_vshifts:
10289 case Intrinsic::arm_neon_vshiftu:
10290 // Opcode already set above.
10291 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010292 case Intrinsic::arm_neon_vrshifts:
10293 VShiftOpc = ARMISD::VRSHRs; break;
10294 case Intrinsic::arm_neon_vrshiftu:
10295 VShiftOpc = ARMISD::VRSHRu; break;
10296 case Intrinsic::arm_neon_vrshiftn:
10297 VShiftOpc = ARMISD::VRSHRN; break;
10298 case Intrinsic::arm_neon_vqshifts:
10299 VShiftOpc = ARMISD::VQSHLs; break;
10300 case Intrinsic::arm_neon_vqshiftu:
10301 VShiftOpc = ARMISD::VQSHLu; break;
10302 case Intrinsic::arm_neon_vqshiftsu:
10303 VShiftOpc = ARMISD::VQSHLsu; break;
10304 case Intrinsic::arm_neon_vqshiftns:
10305 VShiftOpc = ARMISD::VQSHRNs; break;
10306 case Intrinsic::arm_neon_vqshiftnu:
10307 VShiftOpc = ARMISD::VQSHRNu; break;
10308 case Intrinsic::arm_neon_vqshiftnsu:
10309 VShiftOpc = ARMISD::VQSHRNsu; break;
10310 case Intrinsic::arm_neon_vqrshiftns:
10311 VShiftOpc = ARMISD::VQRSHRNs; break;
10312 case Intrinsic::arm_neon_vqrshiftnu:
10313 VShiftOpc = ARMISD::VQRSHRNu; break;
10314 case Intrinsic::arm_neon_vqrshiftnsu:
10315 VShiftOpc = ARMISD::VQRSHRNsu; break;
10316 }
10317
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010318 SDLoc dl(N);
10319 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10320 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010321 }
10322
10323 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010324 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010325 int64_t Cnt;
10326 unsigned VShiftOpc = 0;
10327
10328 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10329 VShiftOpc = ARMISD::VSLI;
10330 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10331 VShiftOpc = ARMISD::VSRI;
10332 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010333 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010334 }
10335
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010336 SDLoc dl(N);
10337 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010338 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010339 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010340 }
10341
10342 case Intrinsic::arm_neon_vqrshifts:
10343 case Intrinsic::arm_neon_vqrshiftu:
10344 // No immediate versions of these to check for.
10345 break;
10346 }
10347
10348 return SDValue();
10349}
10350
10351/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10352/// lowers them. As with the vector shift intrinsics, this is done during DAG
10353/// combining instead of DAG legalizing because the build_vectors for 64-bit
10354/// vector element shift counts are generally not legal, and it is hard to see
10355/// their values after they get legalized to loads from a constant pool.
10356static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10357 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010358 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010359 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10360 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10361 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10362 SDValue N1 = N->getOperand(1);
10363 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10364 SDValue N0 = N->getOperand(0);
10365 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10366 DAG.MaskedValueIsZero(N0.getOperand(0),
10367 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010368 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010369 }
10370 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010371
10372 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010373 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10374 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010375 return SDValue();
10376
10377 assert(ST->hasNEON() && "unexpected vector shift");
10378 int64_t Cnt;
10379
10380 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010381 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010382
10383 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010384 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10385 SDLoc dl(N);
10386 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10387 DAG.getConstant(Cnt, dl, MVT::i32));
10388 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010389 break;
10390
10391 case ISD::SRA:
10392 case ISD::SRL:
10393 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10394 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10395 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010396 SDLoc dl(N);
10397 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10398 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010399 }
10400 }
10401 return SDValue();
10402}
10403
10404/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10405/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10406static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10407 const ARMSubtarget *ST) {
10408 SDValue N0 = N->getOperand(0);
10409
10410 // Check for sign- and zero-extensions of vector extract operations of 8-
10411 // and 16-bit vector elements. NEON supports these directly. They are
10412 // handled during DAG combining because type legalization will promote them
10413 // to 32-bit types and it is messy to recognize the operations after that.
10414 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10415 SDValue Vec = N0.getOperand(0);
10416 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010417 EVT VT = N->getValueType(0);
10418 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010419 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10420
Owen Anderson9f944592009-08-11 20:47:22 +000010421 if (VT == MVT::i32 &&
10422 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010423 TLI.isTypeLegal(Vec.getValueType()) &&
10424 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010425
10426 unsigned Opc = 0;
10427 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010428 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010429 case ISD::SIGN_EXTEND:
10430 Opc = ARMISD::VGETLANEs;
10431 break;
10432 case ISD::ZERO_EXTEND:
10433 case ISD::ANY_EXTEND:
10434 Opc = ARMISD::VGETLANEu;
10435 break;
10436 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010437 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010438 }
10439 }
10440
10441 return SDValue();
10442}
10443
James Molloy9d55f192015-11-10 14:22:05 +000010444static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
10445 APInt &KnownOne) {
10446 if (Op.getOpcode() == ARMISD::BFI) {
10447 // Conservatively, we can recurse down the first operand
10448 // and just mask out all affected bits.
10449 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
10450
10451 // The operand to BFI is already a mask suitable for removing the bits it
10452 // sets.
10453 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
10454 APInt Mask = CI->getAPIntValue();
10455 KnownZero &= Mask;
10456 KnownOne &= Mask;
10457 return;
10458 }
10459 if (Op.getOpcode() == ARMISD::CMOV) {
10460 APInt KZ2(KnownZero.getBitWidth(), 0);
10461 APInt KO2(KnownOne.getBitWidth(), 0);
10462 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
10463 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
10464
10465 KnownZero &= KZ2;
10466 KnownOne &= KO2;
10467 return;
10468 }
10469 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
10470}
10471
10472SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
10473 // If we have a CMOV, OR and AND combination such as:
10474 // if (x & CN)
10475 // y |= CM;
10476 //
10477 // And:
10478 // * CN is a single bit;
10479 // * All bits covered by CM are known zero in y
10480 //
10481 // Then we can convert this into a sequence of BFI instructions. This will
10482 // always be a win if CM is a single bit, will always be no worse than the
10483 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
10484 // three bits (due to the extra IT instruction).
10485
10486 SDValue Op0 = CMOV->getOperand(0);
10487 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000010488 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
10489 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000010490 SDValue CmpZ = CMOV->getOperand(4);
10491
James Molloy20180912015-11-16 10:49:25 +000010492 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000010493 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000010494 return SDValue();
10495
James Molloy9d55f192015-11-10 14:22:05 +000010496 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
10497 SDValue And = CmpZ->getOperand(0);
10498 if (And->getOpcode() != ISD::AND)
10499 return SDValue();
10500 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
10501 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
10502 return SDValue();
10503 SDValue X = And->getOperand(0);
10504
James Molloy8e99e972015-11-12 13:49:17 +000010505 if (CC == ARMCC::EQ) {
10506 // We're performing an "equal to zero" compare. Swap the operands so we
10507 // canonicalize on a "not equal to zero" compare.
10508 std::swap(Op0, Op1);
10509 } else {
10510 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
10511 }
10512
James Molloy9d55f192015-11-10 14:22:05 +000010513 if (Op1->getOpcode() != ISD::OR)
10514 return SDValue();
10515
10516 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
10517 if (!OrC)
10518 return SDValue();
10519 SDValue Y = Op1->getOperand(0);
10520
10521 if (Op0 != Y)
10522 return SDValue();
10523
10524 // Now, is it profitable to continue?
10525 APInt OrCI = OrC->getAPIntValue();
10526 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
10527 if (OrCI.countPopulation() > Heuristic)
10528 return SDValue();
10529
10530 // Lastly, can we determine that the bits defined by OrCI
10531 // are zero in Y?
10532 APInt KnownZero, KnownOne;
10533 computeKnownBits(DAG, Y, KnownZero, KnownOne);
10534 if ((OrCI & KnownZero) != OrCI)
10535 return SDValue();
10536
10537 // OK, we can do the combine.
10538 SDValue V = Y;
10539 SDLoc dl(X);
10540 EVT VT = X.getValueType();
10541 unsigned BitInX = AndC->getAPIntValue().logBase2();
10542
10543 if (BitInX != 0) {
10544 // We must shift X first.
10545 X = DAG.getNode(ISD::SRL, dl, VT, X,
10546 DAG.getConstant(BitInX, dl, VT));
10547 }
10548
10549 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
10550 BitInY < NumActiveBits; ++BitInY) {
10551 if (OrCI[BitInY] == 0)
10552 continue;
10553 APInt Mask(VT.getSizeInBits(), 0);
10554 Mask.setBit(BitInY);
10555 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
10556 // Confusingly, the operand is an *inverted* mask.
10557 DAG.getConstant(~Mask, dl, VT));
10558 }
10559
10560 return V;
10561}
10562
Evan Chengf863e3f2011-07-13 00:42:17 +000010563/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10564SDValue
10565ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10566 SDValue Cmp = N->getOperand(4);
10567 if (Cmp.getOpcode() != ARMISD::CMPZ)
10568 // Only looking at EQ and NE cases.
10569 return SDValue();
10570
10571 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010572 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010573 SDValue LHS = Cmp.getOperand(0);
10574 SDValue RHS = Cmp.getOperand(1);
10575 SDValue FalseVal = N->getOperand(0);
10576 SDValue TrueVal = N->getOperand(1);
10577 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010578 ARMCC::CondCodes CC =
10579 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010580
James Molloy9d55f192015-11-10 14:22:05 +000010581 // BFI is only available on V6T2+.
10582 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
10583 SDValue R = PerformCMOVToBFICombine(N, DAG);
10584 if (R)
10585 return R;
10586 }
10587
Evan Chengf863e3f2011-07-13 00:42:17 +000010588 // Simplify
10589 // mov r1, r0
10590 // cmp r1, x
10591 // mov r0, y
10592 // moveq r0, x
10593 // to
10594 // cmp r0, x
10595 // movne r0, y
10596 //
10597 // mov r1, r0
10598 // cmp r1, x
10599 // mov r0, x
10600 // movne r0, y
10601 // to
10602 // cmp r0, x
10603 // movne r0, y
10604 /// FIXME: Turn this into a target neutral optimization?
10605 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010606 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010607 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10608 N->getOperand(3), Cmp);
10609 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10610 SDValue ARMcc;
10611 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10612 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10613 N->getOperand(3), NewCmp);
10614 }
10615
10616 if (Res.getNode()) {
10617 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010618 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010619 // Capture demanded bits information that would be otherwise lost.
10620 if (KnownZero == 0xfffffffe)
10621 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10622 DAG.getValueType(MVT::i1));
10623 else if (KnownZero == 0xffffff00)
10624 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10625 DAG.getValueType(MVT::i8));
10626 else if (KnownZero == 0xffff0000)
10627 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10628 DAG.getValueType(MVT::i16));
10629 }
10630
10631 return Res;
10632}
10633
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010634SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010635 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010636 switch (N->getOpcode()) {
10637 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010638 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010639 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010640 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010641 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010642 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010643 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10644 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010645 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010646 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010647 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010648 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010649 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010650 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010651 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010652 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010653 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000010654 case ISD::FP_TO_UINT:
10655 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
10656 case ISD::FDIV:
10657 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010658 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010659 case ISD::SHL:
10660 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010661 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010662 case ISD::SIGN_EXTEND:
10663 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010664 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010665 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010666 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010667 case ARMISD::VLD2DUP:
10668 case ARMISD::VLD3DUP:
10669 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010670 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010671 case ARMISD::BUILD_VECTOR:
10672 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010673 case ISD::INTRINSIC_VOID:
10674 case ISD::INTRINSIC_W_CHAIN:
10675 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10676 case Intrinsic::arm_neon_vld1:
10677 case Intrinsic::arm_neon_vld2:
10678 case Intrinsic::arm_neon_vld3:
10679 case Intrinsic::arm_neon_vld4:
10680 case Intrinsic::arm_neon_vld2lane:
10681 case Intrinsic::arm_neon_vld3lane:
10682 case Intrinsic::arm_neon_vld4lane:
10683 case Intrinsic::arm_neon_vst1:
10684 case Intrinsic::arm_neon_vst2:
10685 case Intrinsic::arm_neon_vst3:
10686 case Intrinsic::arm_neon_vst4:
10687 case Intrinsic::arm_neon_vst2lane:
10688 case Intrinsic::arm_neon_vst3lane:
10689 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010690 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010691 default: break;
10692 }
10693 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010694 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010695 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010696}
10697
Evan Chengd42641c2011-02-02 01:06:55 +000010698bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10699 EVT VT) const {
10700 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10701}
10702
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010703bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10704 unsigned,
10705 unsigned,
10706 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010707 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010708 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010709
10710 switch (VT.getSimpleVT().SimpleTy) {
10711 default:
10712 return false;
10713 case MVT::i8:
10714 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010715 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010716 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010717 if (AllowsUnaligned) {
10718 if (Fast)
10719 *Fast = Subtarget->hasV7Ops();
10720 return true;
10721 }
10722 return false;
10723 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010724 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010725 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010726 // For any little-endian targets with neon, we can support unaligned ld/st
10727 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010728 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010729 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010730 if (Fast)
10731 *Fast = true;
10732 return true;
10733 }
10734 return false;
10735 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010736 }
10737}
10738
Lang Hames9929c422011-11-02 22:52:45 +000010739static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10740 unsigned AlignCheck) {
10741 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10742 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10743}
10744
10745EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10746 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010747 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010748 bool MemcpyStrSrc,
10749 MachineFunction &MF) const {
10750 const Function *F = MF.getFunction();
10751
10752 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010753 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10754 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010755 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010756 if (Size >= 16 &&
10757 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010758 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010759 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010760 } else if (Size >= 8 &&
10761 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010762 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10763 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010764 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010765 }
10766 }
10767
Lang Hamesb85fcd02011-11-08 18:56:23 +000010768 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010769 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010770 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010771 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010772 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010773
Lang Hames9929c422011-11-02 22:52:45 +000010774 // Let the target-independent logic figure it out.
10775 return MVT::Other;
10776}
10777
Evan Cheng9ec512d2012-12-06 19:13:27 +000010778bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10779 if (Val.getOpcode() != ISD::LOAD)
10780 return false;
10781
10782 EVT VT1 = Val.getValueType();
10783 if (!VT1.isSimple() || !VT1.isInteger() ||
10784 !VT2.isSimple() || !VT2.isInteger())
10785 return false;
10786
10787 switch (VT1.getSimpleVT().SimpleTy) {
10788 default: break;
10789 case MVT::i1:
10790 case MVT::i8:
10791 case MVT::i16:
10792 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10793 return true;
10794 }
10795
10796 return false;
10797}
10798
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010799bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10800 EVT VT = ExtVal.getValueType();
10801
10802 if (!isTypeLegal(VT))
10803 return false;
10804
10805 // Don't create a loadext if we can fold the extension into a wide/long
10806 // instruction.
10807 // If there's more than one user instruction, the loadext is desirable no
10808 // matter what. There can be two uses by the same instruction.
10809 if (ExtVal->use_empty() ||
10810 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10811 return true;
10812
10813 SDNode *U = *ExtVal->use_begin();
10814 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10815 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10816 return false;
10817
10818 return true;
10819}
10820
Tim Northovercc2e9032013-08-06 13:58:03 +000010821bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10822 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10823 return false;
10824
10825 if (!isTypeLegal(EVT::getEVT(Ty1)))
10826 return false;
10827
10828 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10829
10830 // Assuming the caller doesn't have a zeroext or signext return parameter,
10831 // truncation all the way down to i1 is valid.
10832 return true;
10833}
10834
10835
Evan Chengdc49a8d2009-08-14 20:09:37 +000010836static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10837 if (V < 0)
10838 return false;
10839
10840 unsigned Scale = 1;
10841 switch (VT.getSimpleVT().SimpleTy) {
10842 default: return false;
10843 case MVT::i1:
10844 case MVT::i8:
10845 // Scale == 1;
10846 break;
10847 case MVT::i16:
10848 // Scale == 2;
10849 Scale = 2;
10850 break;
10851 case MVT::i32:
10852 // Scale == 4;
10853 Scale = 4;
10854 break;
10855 }
10856
10857 if ((V & (Scale - 1)) != 0)
10858 return false;
10859 V /= Scale;
10860 return V == (V & ((1LL << 5) - 1));
10861}
10862
10863static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10864 const ARMSubtarget *Subtarget) {
10865 bool isNeg = false;
10866 if (V < 0) {
10867 isNeg = true;
10868 V = - V;
10869 }
10870
10871 switch (VT.getSimpleVT().SimpleTy) {
10872 default: return false;
10873 case MVT::i1:
10874 case MVT::i8:
10875 case MVT::i16:
10876 case MVT::i32:
10877 // + imm12 or - imm8
10878 if (isNeg)
10879 return V == (V & ((1LL << 8) - 1));
10880 return V == (V & ((1LL << 12) - 1));
10881 case MVT::f32:
10882 case MVT::f64:
10883 // Same as ARM mode. FIXME: NEON?
10884 if (!Subtarget->hasVFP2())
10885 return false;
10886 if ((V & 3) != 0)
10887 return false;
10888 V >>= 2;
10889 return V == (V & ((1LL << 8) - 1));
10890 }
10891}
10892
Evan Cheng2150b922007-03-12 23:30:29 +000010893/// isLegalAddressImmediate - Return true if the integer value can be used
10894/// as the offset of the target addressing mode for load / store of the
10895/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010896static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010897 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010898 if (V == 0)
10899 return true;
10900
Evan Chengce5dfb62009-03-09 19:15:00 +000010901 if (!VT.isSimple())
10902 return false;
10903
Evan Chengdc49a8d2009-08-14 20:09:37 +000010904 if (Subtarget->isThumb1Only())
10905 return isLegalT1AddressImmediate(V, VT);
10906 else if (Subtarget->isThumb2())
10907 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010908
Evan Chengdc49a8d2009-08-14 20:09:37 +000010909 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010910 if (V < 0)
10911 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010912 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010913 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010914 case MVT::i1:
10915 case MVT::i8:
10916 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010917 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010918 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010919 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010920 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010921 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010922 case MVT::f32:
10923 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010924 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010925 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010926 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010927 return false;
10928 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010929 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010930 }
Evan Cheng10043e22007-01-19 07:51:42 +000010931}
10932
Evan Chengdc49a8d2009-08-14 20:09:37 +000010933bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10934 EVT VT) const {
10935 int Scale = AM.Scale;
10936 if (Scale < 0)
10937 return false;
10938
10939 switch (VT.getSimpleVT().SimpleTy) {
10940 default: return false;
10941 case MVT::i1:
10942 case MVT::i8:
10943 case MVT::i16:
10944 case MVT::i32:
10945 if (Scale == 1)
10946 return true;
10947 // r + r << imm
10948 Scale = Scale & ~1;
10949 return Scale == 2 || Scale == 4 || Scale == 8;
10950 case MVT::i64:
10951 // r + r
10952 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10953 return true;
10954 return false;
10955 case MVT::isVoid:
10956 // Note, we allow "void" uses (basically, uses that aren't loads or
10957 // stores), because arm allows folding a scale into many arithmetic
10958 // operations. This should be made more precise and revisited later.
10959
10960 // Allow r << imm, but the imm has to be a multiple of two.
10961 if (Scale & 1) return false;
10962 return isPowerOf2_32(Scale);
10963 }
10964}
10965
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010966/// isLegalAddressingMode - Return true if the addressing mode represented
10967/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010968bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10969 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010970 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010971 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010972 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010973 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010974
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010975 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010976 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010977 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010978
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010979 switch (AM.Scale) {
10980 case 0: // no scale reg, must be "r+i" or "r", or "i".
10981 break;
10982 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010983 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010984 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010985 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010986 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010987 // ARM doesn't support any R+R*scale+imm addr modes.
10988 if (AM.BaseOffs)
10989 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010990
Bob Wilson866c1742009-04-08 17:55:28 +000010991 if (!VT.isSimple())
10992 return false;
10993
Evan Chengdc49a8d2009-08-14 20:09:37 +000010994 if (Subtarget->isThumb2())
10995 return isLegalT2ScaledAddressingMode(AM, VT);
10996
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010997 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010998 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010999 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011000 case MVT::i1:
11001 case MVT::i8:
11002 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011003 if (Scale < 0) Scale = -Scale;
11004 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011005 return true;
11006 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000011007 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000011008 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011009 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011010 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011011 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011012 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000011013 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011014
Owen Anderson9f944592009-08-11 20:47:22 +000011015 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011016 // Note, we allow "void" uses (basically, uses that aren't loads or
11017 // stores), because arm allows folding a scale into many arithmetic
11018 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000011019
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011020 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000011021 if (Scale & 1) return false;
11022 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011023 }
Evan Cheng2150b922007-03-12 23:30:29 +000011024 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011025 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000011026}
11027
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011028/// isLegalICmpImmediate - Return true if the specified immediate is legal
11029/// icmp immediate, that is the target has icmp instructions which can compare
11030/// a register against the immediate without having to materialize the
11031/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000011032bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011033 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011034 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011035 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011036 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011037 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011038 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000011039 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011040}
11041
Andrew Tricka22cdb72012-07-18 18:34:27 +000011042/// isLegalAddImmediate - Return true if the specified immediate is a legal add
11043/// *or sub* immediate, that is the target has add or sub instructions which can
11044/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000011045/// immediate into a register.
11046bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000011047 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011048 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000011049 if (!Subtarget->isThumb())
11050 return ARM_AM::getSOImmVal(AbsImm) != -1;
11051 if (Subtarget->isThumb2())
11052 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
11053 // Thumb1 only has 8-bit unsigned immediate.
11054 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000011055}
11056
Owen Anderson53aa7a92009-08-10 22:56:29 +000011057static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011058 bool isSEXTLoad, SDValue &Base,
11059 SDValue &Offset, bool &isInc,
11060 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000011061 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11062 return false;
11063
Owen Anderson9f944592009-08-11 20:47:22 +000011064 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000011065 // AddressingMode 3
11066 Base = Ptr->getOperand(0);
11067 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011068 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011069 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011070 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011071 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011072 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011073 return true;
11074 }
11075 }
11076 isInc = (Ptr->getOpcode() == ISD::ADD);
11077 Offset = Ptr->getOperand(1);
11078 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000011079 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000011080 // AddressingMode 2
11081 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011082 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011083 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011084 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011085 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011086 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011087 Base = Ptr->getOperand(0);
11088 return true;
11089 }
11090 }
11091
11092 if (Ptr->getOpcode() == ISD::ADD) {
11093 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000011094 ARM_AM::ShiftOpc ShOpcVal=
11095 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000011096 if (ShOpcVal != ARM_AM::no_shift) {
11097 Base = Ptr->getOperand(1);
11098 Offset = Ptr->getOperand(0);
11099 } else {
11100 Base = Ptr->getOperand(0);
11101 Offset = Ptr->getOperand(1);
11102 }
11103 return true;
11104 }
11105
11106 isInc = (Ptr->getOpcode() == ISD::ADD);
11107 Base = Ptr->getOperand(0);
11108 Offset = Ptr->getOperand(1);
11109 return true;
11110 }
11111
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011112 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011113 return false;
11114}
11115
Owen Anderson53aa7a92009-08-10 22:56:29 +000011116static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011117 bool isSEXTLoad, SDValue &Base,
11118 SDValue &Offset, bool &isInc,
11119 SelectionDAG &DAG) {
11120 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11121 return false;
11122
11123 Base = Ptr->getOperand(0);
11124 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
11125 int RHSC = (int)RHS->getZExtValue();
11126 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
11127 assert(Ptr->getOpcode() == ISD::ADD);
11128 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011129 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011130 return true;
11131 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
11132 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011133 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011134 return true;
11135 }
11136 }
11137
11138 return false;
11139}
11140
Evan Cheng10043e22007-01-19 07:51:42 +000011141/// getPreIndexedAddressParts - returns true by value, base pointer and
11142/// offset pointer and addressing mode by reference if the node's address
11143/// can be legally represented as pre-indexed load / store address.
11144bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011145ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11146 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011147 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011148 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011149 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011150 return false;
11151
Owen Anderson53aa7a92009-08-10 22:56:29 +000011152 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011153 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011154 bool isSEXTLoad = false;
11155 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11156 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011157 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011158 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11159 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11160 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011161 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011162 } else
11163 return false;
11164
11165 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011166 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011167 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011168 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
11169 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011170 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011171 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000011172 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000011173 if (!isLegal)
11174 return false;
11175
11176 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
11177 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011178}
11179
11180/// getPostIndexedAddressParts - returns true by value, base pointer and
11181/// offset pointer and addressing mode by reference if this node can be
11182/// combined with a load / store to form a post-indexed load / store.
11183bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011184 SDValue &Base,
11185 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011186 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011187 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011188 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011189 return false;
11190
Owen Anderson53aa7a92009-08-10 22:56:29 +000011191 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011192 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011193 bool isSEXTLoad = false;
11194 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011195 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011196 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011197 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11198 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011199 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011200 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011201 } else
11202 return false;
11203
11204 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011205 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011206 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011207 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000011208 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011209 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011210 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
11211 isInc, DAG);
11212 if (!isLegal)
11213 return false;
11214
Evan Chengf19384d2010-05-18 21:31:17 +000011215 if (Ptr != Base) {
11216 // Swap base ptr and offset to catch more post-index load / store when
11217 // it's legal. In Thumb2 mode, offset must be an immediate.
11218 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
11219 !Subtarget->isThumb2())
11220 std::swap(Base, Offset);
11221
11222 // Post-indexed load / store update the base pointer.
11223 if (Ptr != Base)
11224 return false;
11225 }
11226
Evan Cheng84c6cda2009-07-02 07:28:31 +000011227 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
11228 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011229}
11230
Jay Foada0653a32014-05-14 21:14:37 +000011231void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11232 APInt &KnownZero,
11233 APInt &KnownOne,
11234 const SelectionDAG &DAG,
11235 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000011236 unsigned BitWidth = KnownOne.getBitWidth();
11237 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000011238 switch (Op.getOpcode()) {
11239 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000011240 case ARMISD::ADDC:
11241 case ARMISD::ADDE:
11242 case ARMISD::SUBC:
11243 case ARMISD::SUBE:
11244 // These nodes' second result is a boolean
11245 if (Op.getResNo() == 0)
11246 break;
11247 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
11248 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011249 case ARMISD::CMOV: {
11250 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000011251 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011252 if (KnownZero == 0 && KnownOne == 0) return;
11253
Dan Gohmanf990faf2008-02-13 00:35:47 +000011254 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000011255 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011256 KnownZero &= KnownZeroRHS;
11257 KnownOne &= KnownOneRHS;
11258 return;
11259 }
Tim Northover01b4aa92014-04-03 15:10:35 +000011260 case ISD::INTRINSIC_W_CHAIN: {
11261 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
11262 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
11263 switch (IntID) {
11264 default: return;
11265 case Intrinsic::arm_ldaex:
11266 case Intrinsic::arm_ldrex: {
11267 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
11268 unsigned MemBits = VT.getScalarType().getSizeInBits();
11269 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
11270 return;
11271 }
11272 }
11273 }
Evan Cheng10043e22007-01-19 07:51:42 +000011274 }
11275}
11276
11277//===----------------------------------------------------------------------===//
11278// ARM Inline Assembly Support
11279//===----------------------------------------------------------------------===//
11280
Evan Cheng078b0b02011-01-08 01:24:27 +000011281bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
11282 // Looking for "rev" which is V6+.
11283 if (!Subtarget->hasV6Ops())
11284 return false;
11285
11286 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11287 std::string AsmStr = IA->getAsmString();
11288 SmallVector<StringRef, 4> AsmPieces;
11289 SplitString(AsmStr, AsmPieces, ";\n");
11290
11291 switch (AsmPieces.size()) {
11292 default: return false;
11293 case 1:
11294 AsmStr = AsmPieces[0];
11295 AsmPieces.clear();
11296 SplitString(AsmStr, AsmPieces, " \t,");
11297
11298 // rev $0, $1
11299 if (AsmPieces.size() == 3 &&
11300 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
11301 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000011302 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000011303 if (Ty && Ty->getBitWidth() == 32)
11304 return IntrinsicLowering::LowerToByteSwap(CI);
11305 }
11306 break;
11307 }
11308
11309 return false;
11310}
11311
Evan Cheng10043e22007-01-19 07:51:42 +000011312/// getConstraintType - Given a constraint letter, return the type of
11313/// constraint it is for this target.
11314ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011315ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011316 if (Constraint.size() == 1) {
11317 switch (Constraint[0]) {
11318 default: break;
11319 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000011320 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000011321 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000011322 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000011323 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000011324 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000011325 // An address with a single base register. Due to the way we
11326 // currently handle addresses it is the same as an 'r' memory constraint.
11327 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011328 }
Eric Christophere256cd02011-06-21 22:10:57 +000011329 } else if (Constraint.size() == 2) {
11330 switch (Constraint[0]) {
11331 default: break;
11332 // All 'U+' constraints are addresses.
11333 case 'U': return C_Memory;
11334 }
Evan Cheng10043e22007-01-19 07:51:42 +000011335 }
Chris Lattnerd6855142007-03-25 02:14:49 +000011336 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000011337}
11338
John Thompsone8360b72010-10-29 17:29:13 +000011339/// Examine constraint type and operand type and determine a weight value.
11340/// This object must already have been set up with the operand type
11341/// and the current alternative constraint selected.
11342TargetLowering::ConstraintWeight
11343ARMTargetLowering::getSingleConstraintMatchWeight(
11344 AsmOperandInfo &info, const char *constraint) const {
11345 ConstraintWeight weight = CW_Invalid;
11346 Value *CallOperandVal = info.CallOperandVal;
11347 // If we don't have a value, we can't do a match,
11348 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011349 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011350 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011351 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000011352 // Look at the constraint type.
11353 switch (*constraint) {
11354 default:
11355 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11356 break;
11357 case 'l':
11358 if (type->isIntegerTy()) {
11359 if (Subtarget->isThumb())
11360 weight = CW_SpecificReg;
11361 else
11362 weight = CW_Register;
11363 }
11364 break;
11365 case 'w':
11366 if (type->isFloatingPointTy())
11367 weight = CW_Register;
11368 break;
11369 }
11370 return weight;
11371}
11372
Eric Christophercf2007c2011-06-30 23:50:52 +000011373typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011374RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11375 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011376 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011377 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011378 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011379 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011380 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011381 return RCPair(0U, &ARM::tGPRRegClass);
11382 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011383 case 'h': // High regs or no regs.
11384 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011385 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011386 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011387 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011388 if (Subtarget->isThumb1Only())
11389 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011390 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011391 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011392 if (VT == MVT::Other)
11393 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011394 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011395 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011396 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011397 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011398 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011399 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011400 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011401 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011402 if (VT == MVT::Other)
11403 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011404 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011405 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011406 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011407 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011408 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011409 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011410 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011411 case 't':
11412 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011413 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011414 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011415 }
11416 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011417 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011418 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011419
Eric Christopher11e4df72015-02-26 22:38:43 +000011420 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011421}
11422
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011423/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11424/// vector. If it is invalid, don't add anything to Ops.
11425void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011426 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011427 std::vector<SDValue>&Ops,
11428 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011429 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011430
Eric Christopherde9399b2011-06-02 23:16:42 +000011431 // Currently only support length 1 constraints.
11432 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011433
Eric Christopherde9399b2011-06-02 23:16:42 +000011434 char ConstraintLetter = Constraint[0];
11435 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011436 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011437 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011438 case 'I': case 'J': case 'K': case 'L':
11439 case 'M': case 'N': case 'O':
11440 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11441 if (!C)
11442 return;
11443
11444 int64_t CVal64 = C->getSExtValue();
11445 int CVal = (int) CVal64;
11446 // None of these constraints allow values larger than 32 bits. Check
11447 // that the value fits in an int.
11448 if (CVal != CVal64)
11449 return;
11450
Eric Christopherde9399b2011-06-02 23:16:42 +000011451 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011452 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011453 // Constant suitable for movw, must be between 0 and
11454 // 65535.
11455 if (Subtarget->hasV6T2Ops())
11456 if (CVal >= 0 && CVal <= 65535)
11457 break;
11458 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011459 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011460 if (Subtarget->isThumb1Only()) {
11461 // This must be a constant between 0 and 255, for ADD
11462 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011463 if (CVal >= 0 && CVal <= 255)
11464 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011465 } else if (Subtarget->isThumb2()) {
11466 // A constant that can be used as an immediate value in a
11467 // data-processing instruction.
11468 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11469 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011470 } else {
11471 // A constant that can be used as an immediate value in a
11472 // data-processing instruction.
11473 if (ARM_AM::getSOImmVal(CVal) != -1)
11474 break;
11475 }
11476 return;
11477
11478 case 'J':
Eric Christopherb7932302016-01-08 00:34:44 +000011479 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011480 // This must be a constant between -255 and -1, for negated ADD
11481 // immediates. This can be used in GCC with an "n" modifier that
11482 // prints the negated value, for use with SUB instructions. It is
11483 // not useful otherwise but is implemented for compatibility.
11484 if (CVal >= -255 && CVal <= -1)
11485 break;
11486 } else {
11487 // This must be a constant between -4095 and 4095. It is not clear
11488 // what this constraint is intended for. Implemented for
11489 // compatibility with GCC.
11490 if (CVal >= -4095 && CVal <= 4095)
11491 break;
11492 }
11493 return;
11494
11495 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011496 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011497 // A 32-bit value where only one byte has a nonzero value. Exclude
11498 // zero to match GCC. This constraint is used by GCC internally for
11499 // constants that can be loaded with a move/shift combination.
11500 // It is not useful otherwise but is implemented for compatibility.
11501 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11502 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011503 } else if (Subtarget->isThumb2()) {
11504 // A constant whose bitwise inverse can be used as an immediate
11505 // value in a data-processing instruction. This can be used in GCC
11506 // with a "B" modifier that prints the inverted value, for use with
11507 // BIC and MVN instructions. It is not useful otherwise but is
11508 // implemented for compatibility.
11509 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11510 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011511 } else {
11512 // A constant whose bitwise inverse can be used as an immediate
11513 // value in a data-processing instruction. This can be used in GCC
11514 // with a "B" modifier that prints the inverted value, for use with
11515 // BIC and MVN instructions. It is not useful otherwise but is
11516 // implemented for compatibility.
11517 if (ARM_AM::getSOImmVal(~CVal) != -1)
11518 break;
11519 }
11520 return;
11521
11522 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011523 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011524 // This must be a constant between -7 and 7,
11525 // for 3-operand ADD/SUB immediate instructions.
11526 if (CVal >= -7 && CVal < 7)
11527 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011528 } else if (Subtarget->isThumb2()) {
11529 // A constant whose negation can be used as an immediate value in a
11530 // data-processing instruction. This can be used in GCC with an "n"
11531 // modifier that prints the negated value, for use with SUB
11532 // instructions. It is not useful otherwise but is implemented for
11533 // compatibility.
11534 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11535 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011536 } else {
11537 // A constant whose negation can be used as an immediate value in a
11538 // data-processing instruction. This can be used in GCC with an "n"
11539 // modifier that prints the negated value, for use with SUB
11540 // instructions. It is not useful otherwise but is implemented for
11541 // compatibility.
11542 if (ARM_AM::getSOImmVal(-CVal) != -1)
11543 break;
11544 }
11545 return;
11546
11547 case 'M':
Eric Christopherb7932302016-01-08 00:34:44 +000011548 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011549 // This must be a multiple of 4 between 0 and 1020, for
11550 // ADD sp + immediate.
11551 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11552 break;
11553 } else {
11554 // A power of two or a constant between 0 and 32. This is used in
11555 // GCC for the shift amount on shifted register operands, but it is
11556 // useful in general for any shift amounts.
11557 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11558 break;
11559 }
11560 return;
11561
11562 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011563 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011564 // This must be a constant between 0 and 31, for shift amounts.
11565 if (CVal >= 0 && CVal <= 31)
11566 break;
11567 }
11568 return;
11569
11570 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011571 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011572 // This must be a multiple of 4 between -508 and 508, for
11573 // ADD/SUB sp = sp + immediate.
11574 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11575 break;
11576 }
11577 return;
11578 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011579 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011580 break;
11581 }
11582
11583 if (Result.getNode()) {
11584 Ops.push_back(Result);
11585 return;
11586 }
Dale Johannesence97d552010-06-25 21:55:36 +000011587 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011588}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011589
Scott Douglassd2974a62015-08-24 09:17:11 +000011590static RTLIB::Libcall getDivRemLibcall(
11591 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011592 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11593 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011594 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011595 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11596 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011597 RTLIB::Libcall LC;
11598 switch (SVT) {
11599 default: llvm_unreachable("Unexpected request for libcall!");
11600 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11601 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11602 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11603 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11604 }
11605 return LC;
11606}
11607
11608static TargetLowering::ArgListTy getDivRemArgList(
11609 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011610 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11611 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011612 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011613 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11614 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011615 TargetLowering::ArgListTy Args;
11616 TargetLowering::ArgListEntry Entry;
11617 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11618 EVT ArgVT = N->getOperand(i).getValueType();
11619 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11620 Entry.Node = N->getOperand(i);
11621 Entry.Ty = ArgTy;
11622 Entry.isSExt = isSigned;
11623 Entry.isZExt = !isSigned;
11624 Args.push_back(Entry);
11625 }
11626 return Args;
11627}
11628
Renato Golin87610692013-07-16 09:32:17 +000011629SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011630 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) &&
11631 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011632 unsigned Opcode = Op->getOpcode();
11633 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011634 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011635 bool isSigned = (Opcode == ISD::SDIVREM);
11636 EVT VT = Op->getValueType(0);
11637 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11638
Scott Douglassd2974a62015-08-24 09:17:11 +000011639 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11640 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011641 SDValue InChain = DAG.getEntryNode();
11642
Scott Douglassd2974a62015-08-24 09:17:11 +000011643 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11644 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011645
11646 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011647 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011648
Reid Kleckner343c3952014-11-20 23:51:47 +000011649 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011650
11651 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011652 TargetLowering::CallLoweringInfo CLI(DAG);
11653 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011654 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011655 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011656
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011657 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011658 return CallInfo.first;
11659}
11660
Scott Douglassbdef6042015-08-24 09:17:18 +000011661// Lowers REM using divmod helpers
11662// see RTABI section 4.2/4.3
11663SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11664 // Build return types (div and rem)
11665 std::vector<Type*> RetTyParams;
11666 Type *RetTyElement;
11667
11668 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11669 default: llvm_unreachable("Unexpected request for libcall!");
11670 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11671 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11672 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11673 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11674 }
11675
11676 RetTyParams.push_back(RetTyElement);
11677 RetTyParams.push_back(RetTyElement);
11678 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11679 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11680
11681 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11682 SimpleTy);
11683 SDValue InChain = DAG.getEntryNode();
11684 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11685 bool isSigned = N->getOpcode() == ISD::SREM;
11686 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11687 getPointerTy(DAG.getDataLayout()));
11688
11689 // Lower call
11690 CallLoweringInfo CLI(DAG);
11691 CLI.setChain(InChain)
11692 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11693 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11694 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11695
11696 // Return second (rem) result operand (first contains div)
11697 SDNode *ResNode = CallResult.first.getNode();
11698 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11699 return ResNode->getOperand(1);
11700}
11701
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011702SDValue
11703ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11704 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11705 SDLoc DL(Op);
11706
11707 // Get the inputs.
11708 SDValue Chain = Op.getOperand(0);
11709 SDValue Size = Op.getOperand(1);
11710
11711 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011712 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011713
11714 SDValue Flag;
11715 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11716 Flag = Chain.getValue(1);
11717
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011718 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011719 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11720
11721 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11722 Chain = NewSP.getValue(1);
11723
11724 SDValue Ops[2] = { NewSP, Chain };
11725 return DAG.getMergeValues(Ops, DL);
11726}
11727
Oliver Stannard51b1d462014-08-21 12:50:31 +000011728SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11729 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11730 "Unexpected type for custom-lowering FP_EXTEND");
11731
11732 RTLIB::Libcall LC;
11733 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11734
11735 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011736 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11737 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011738}
11739
11740SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11741 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11742 Subtarget->isFPOnlySP() &&
11743 "Unexpected type for custom-lowering FP_ROUND");
11744
11745 RTLIB::Libcall LC;
11746 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11747
11748 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011749 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11750 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011751}
11752
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011753bool
11754ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11755 // The ARM target isn't yet aware of offsets.
11756 return false;
11757}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011758
Jim Grosbach11013ed2010-07-16 23:05:05 +000011759bool ARM::isBitFieldInvertedMask(unsigned v) {
11760 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011761 return false;
11762
Jim Grosbach11013ed2010-07-16 23:05:05 +000011763 // there can be 1's on either or both "outsides", all the "inside"
11764 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011765 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011766}
11767
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011768/// isFPImmLegal - Returns true if the target can instruction select the
11769/// specified FP immediate natively. If false, the legalizer will
11770/// materialize the FP immediate as a load from a constant pool.
11771bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11772 if (!Subtarget->hasVFP3())
11773 return false;
11774 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011775 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011776 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011777 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011778 return false;
11779}
Bob Wilson5549d492010-09-21 17:56:22 +000011780
Wesley Peck527da1b2010-11-23 03:31:01 +000011781/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011782/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11783/// specified in the intrinsic calls.
11784bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11785 const CallInst &I,
11786 unsigned Intrinsic) const {
11787 switch (Intrinsic) {
11788 case Intrinsic::arm_neon_vld1:
11789 case Intrinsic::arm_neon_vld2:
11790 case Intrinsic::arm_neon_vld3:
11791 case Intrinsic::arm_neon_vld4:
11792 case Intrinsic::arm_neon_vld2lane:
11793 case Intrinsic::arm_neon_vld3lane:
11794 case Intrinsic::arm_neon_vld4lane: {
11795 Info.opc = ISD::INTRINSIC_W_CHAIN;
11796 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011797 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000011798 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000011799 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11800 Info.ptrVal = I.getArgOperand(0);
11801 Info.offset = 0;
11802 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11803 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11804 Info.vol = false; // volatile loads with NEON intrinsics not supported
11805 Info.readMem = true;
11806 Info.writeMem = false;
11807 return true;
11808 }
11809 case Intrinsic::arm_neon_vst1:
11810 case Intrinsic::arm_neon_vst2:
11811 case Intrinsic::arm_neon_vst3:
11812 case Intrinsic::arm_neon_vst4:
11813 case Intrinsic::arm_neon_vst2lane:
11814 case Intrinsic::arm_neon_vst3lane:
11815 case Intrinsic::arm_neon_vst4lane: {
11816 Info.opc = ISD::INTRINSIC_VOID;
11817 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011818 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011819 unsigned NumElts = 0;
11820 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011821 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011822 if (!ArgTy->isVectorTy())
11823 break;
Ahmed Bougacha97564c32015-12-09 01:19:50 +000011824 NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000011825 }
11826 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11827 Info.ptrVal = I.getArgOperand(0);
11828 Info.offset = 0;
11829 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11830 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11831 Info.vol = false; // volatile stores with NEON intrinsics not supported
11832 Info.readMem = false;
11833 Info.writeMem = true;
11834 return true;
11835 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011836 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011837 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011838 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011839 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11840 Info.opc = ISD::INTRINSIC_W_CHAIN;
11841 Info.memVT = MVT::getVT(PtrTy->getElementType());
11842 Info.ptrVal = I.getArgOperand(0);
11843 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011844 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011845 Info.vol = true;
11846 Info.readMem = true;
11847 Info.writeMem = false;
11848 return true;
11849 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011850 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011851 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011852 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011853 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11854 Info.opc = ISD::INTRINSIC_W_CHAIN;
11855 Info.memVT = MVT::getVT(PtrTy->getElementType());
11856 Info.ptrVal = I.getArgOperand(1);
11857 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011858 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011859 Info.vol = true;
11860 Info.readMem = false;
11861 Info.writeMem = true;
11862 return true;
11863 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011864 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011865 case Intrinsic::arm_strexd: {
11866 Info.opc = ISD::INTRINSIC_W_CHAIN;
11867 Info.memVT = MVT::i64;
11868 Info.ptrVal = I.getArgOperand(2);
11869 Info.offset = 0;
11870 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011871 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011872 Info.readMem = false;
11873 Info.writeMem = true;
11874 return true;
11875 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011876 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011877 case Intrinsic::arm_ldrexd: {
11878 Info.opc = ISD::INTRINSIC_W_CHAIN;
11879 Info.memVT = MVT::i64;
11880 Info.ptrVal = I.getArgOperand(0);
11881 Info.offset = 0;
11882 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011883 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011884 Info.readMem = true;
11885 Info.writeMem = false;
11886 return true;
11887 }
Bob Wilson5549d492010-09-21 17:56:22 +000011888 default:
11889 break;
11890 }
11891
11892 return false;
11893}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011894
11895/// \brief Returns true if it is beneficial to convert a load of a constant
11896/// to just the constant itself.
11897bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11898 Type *Ty) const {
11899 assert(Ty->isIntegerTy());
11900
11901 unsigned Bits = Ty->getPrimitiveSizeInBits();
11902 if (Bits == 0 || Bits > 32)
11903 return false;
11904 return true;
11905}
Tim Northover037f26f22014-04-17 18:22:47 +000011906
Robin Morisset5349e8e2014-09-18 18:56:04 +000011907Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11908 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011909 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011910
11911 // First, if the target has no DMB, see what fallback we can use.
11912 if (!Subtarget->hasDataBarrier()) {
11913 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11914 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11915 // here.
11916 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11917 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11918 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11919 Builder.getInt32(0), Builder.getInt32(7),
11920 Builder.getInt32(10), Builder.getInt32(5)};
11921 return Builder.CreateCall(MCR, args);
11922 } else {
11923 // Instead of using barriers, atomic accesses on these subtargets use
11924 // libcalls.
11925 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11926 }
11927 } else {
11928 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11929 // Only a full system barrier exists in the M-class architectures.
11930 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11931 Constant *CDomain = Builder.getInt32(Domain);
11932 return Builder.CreateCall(DMB, CDomain);
11933 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011934}
11935
11936// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011937Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011938 AtomicOrdering Ord, bool IsStore,
11939 bool IsLoad) const {
11940 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011941 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011942
11943 switch (Ord) {
11944 case NotAtomic:
11945 case Unordered:
11946 llvm_unreachable("Invalid fence: unordered/non-atomic");
11947 case Monotonic:
11948 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011949 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011950 case SequentiallyConsistent:
11951 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011952 return nullptr; // Nothing to do
11953 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011954 case Release:
11955 case AcquireRelease:
11956 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011957 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011958 // FIXME: add a comment with a link to documentation justifying this.
11959 else
Robin Morissetdedef332014-09-23 20:31:14 +000011960 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011961 }
Robin Morissetdedef332014-09-23 20:31:14 +000011962 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011963}
11964
Robin Morissetdedef332014-09-23 20:31:14 +000011965Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011966 AtomicOrdering Ord, bool IsStore,
11967 bool IsLoad) const {
11968 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011969 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011970
11971 switch (Ord) {
11972 case NotAtomic:
11973 case Unordered:
11974 llvm_unreachable("Invalid fence: unordered/not-atomic");
11975 case Monotonic:
11976 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011977 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011978 case Acquire:
11979 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011980 case SequentiallyConsistent:
11981 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011982 }
Robin Morissetdedef332014-09-23 20:31:14 +000011983 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011984}
11985
Robin Morisseted3d48f2014-09-03 21:29:59 +000011986// Loads and stores less than 64-bits are already atomic; ones above that
11987// are doomed anyway, so defer to the default libcall and blame the OS when
11988// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11989// anything for those.
11990bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11991 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11992 return (Size == 64) && !Subtarget->isMClass();
11993}
Tim Northover037f26f22014-04-17 18:22:47 +000011994
Robin Morisseted3d48f2014-09-03 21:29:59 +000011995// Loads and stores less than 64-bits are already atomic; ones above that
11996// are doomed anyway, so defer to the default libcall and blame the OS when
11997// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11998// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011999// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
12000// guarantee, see DDI0406C ARM architecture reference manual,
12001// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000012002TargetLowering::AtomicExpansionKind
12003ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012004 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Tim Northoverf520eff2015-12-02 18:12:57 +000012005 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +000012006 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000012007}
12008
12009// For the real atomic operations, we have ldrex/strex up to 32 bits,
12010// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000012011TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000012012ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012013 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000012014 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000012015 ? AtomicExpansionKind::LLSC
12016 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000012017}
12018
Ahmed Bougacha52468672015-09-11 17:08:28 +000012019bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
12020 AtomicCmpXchgInst *AI) const {
12021 return true;
12022}
12023
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012024// This has so far only been implemented for MachO.
12025bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000012026 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012027}
12028
Quentin Colombetc32615d2014-10-31 17:52:53 +000012029bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
12030 unsigned &Cost) const {
12031 // If we do not have NEON, vector types are not natively supported.
12032 if (!Subtarget->hasNEON())
12033 return false;
12034
12035 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000012036 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000012037 // better to leave at float as we have more freedom in the addressing mode for
12038 // those.
12039 if (VectorTy->isFPOrFPVectorTy())
12040 return false;
12041
12042 // If the index is unknown at compile time, this is very expensive to lower
12043 // and it is not possible to combine the store with the extract.
12044 if (!isa<ConstantInt>(Idx))
12045 return false;
12046
12047 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
12048 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
12049 // We can do a store + vector extract on any vector that fits perfectly in a D
12050 // or Q register.
12051 if (BitWidth == 64 || BitWidth == 128) {
12052 Cost = 0;
12053 return true;
12054 }
12055 return false;
12056}
12057
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000012058bool ARMTargetLowering::isCheapToSpeculateCttz() const {
12059 return Subtarget->hasV6T2Ops();
12060}
12061
12062bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
12063 return Subtarget->hasV6T2Ops();
12064}
12065
Tim Northover037f26f22014-04-17 18:22:47 +000012066Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
12067 AtomicOrdering Ord) const {
12068 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12069 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000012070 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012071
12072 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
12073 // intrinsic must return {i32, i32} and we have to recombine them into a
12074 // single i64 here.
12075 if (ValTy->getPrimitiveSizeInBits() == 64) {
12076 Intrinsic::ID Int =
12077 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
12078 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
12079
12080 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
12081 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
12082
12083 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
12084 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012085 if (!Subtarget->isLittle())
12086 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012087 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
12088 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
12089 return Builder.CreateOr(
12090 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
12091 }
12092
12093 Type *Tys[] = { Addr->getType() };
12094 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
12095 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
12096
12097 return Builder.CreateTruncOrBitCast(
12098 Builder.CreateCall(Ldrex, Addr),
12099 cast<PointerType>(Addr->getType())->getElementType());
12100}
12101
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012102void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
12103 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000012104 if (!Subtarget->hasV7Ops())
12105 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012106 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12107 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
12108}
12109
Tim Northover037f26f22014-04-17 18:22:47 +000012110Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
12111 Value *Addr,
12112 AtomicOrdering Ord) const {
12113 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000012114 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012115
12116 // Since the intrinsics must have legal type, the i64 intrinsics take two
12117 // parameters: "i32, i32". We must marshal Val into the appropriate form
12118 // before the call.
12119 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
12120 Intrinsic::ID Int =
12121 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
12122 Function *Strex = Intrinsic::getDeclaration(M, Int);
12123 Type *Int32Ty = Type::getInt32Ty(M->getContext());
12124
12125 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
12126 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012127 if (!Subtarget->isLittle())
12128 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012129 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000012130 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012131 }
12132
12133 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
12134 Type *Tys[] = { Addr->getType() };
12135 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
12136
David Blaikieff6409d2015-05-18 22:13:54 +000012137 return Builder.CreateCall(
12138 Strex, {Builder.CreateZExtOrBitCast(
12139 Val, Strex->getFunctionType()->getParamType(0)),
12140 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012141}
Oliver Stannardc24f2172014-05-09 14:01:47 +000012142
Hao Liu2cd34bb2015-06-26 02:45:36 +000012143/// \brief Lower an interleaved load into a vldN intrinsic.
12144///
12145/// E.g. Lower an interleaved load (Factor = 2):
12146/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
12147/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
12148/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
12149///
12150/// Into:
12151/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
12152/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
12153/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
12154bool ARMTargetLowering::lowerInterleavedLoad(
12155 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
12156 ArrayRef<unsigned> Indices, unsigned Factor) const {
12157 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12158 "Invalid interleave factor");
12159 assert(!Shuffles.empty() && "Empty shufflevector input");
12160 assert(Shuffles.size() == Indices.size() &&
12161 "Unmatched number of shufflevectors and indices");
12162
12163 VectorType *VecTy = Shuffles[0]->getType();
12164 Type *EltTy = VecTy->getVectorElementType();
12165
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012166 const DataLayout &DL = LI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012167 unsigned VecSize = DL.getTypeSizeInBits(VecTy);
12168 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012169
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012170 // Skip if we do not have NEON and skip illegal vector types and vector types
12171 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
12172 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012173 return false;
12174
12175 // A pointer vector can not be the return type of the ldN intrinsics. Need to
12176 // load integer vectors first and then convert to pointer vectors.
12177 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012178 VecTy =
12179 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000012180
12181 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
12182 Intrinsic::arm_neon_vld3,
12183 Intrinsic::arm_neon_vld4};
12184
Hao Liu2cd34bb2015-06-26 02:45:36 +000012185 IRBuilder<> Builder(LI);
12186 SmallVector<Value *, 2> Ops;
12187
12188 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
12189 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
12190 Ops.push_back(Builder.getInt32(LI->getAlignment()));
12191
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012192 Type *Tys[] = { VecTy, Int8Ptr };
12193 Function *VldnFunc =
12194 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012195 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
12196
12197 // Replace uses of each shufflevector with the corresponding vector loaded
12198 // by ldN.
12199 for (unsigned i = 0; i < Shuffles.size(); i++) {
12200 ShuffleVectorInst *SV = Shuffles[i];
12201 unsigned Index = Indices[i];
12202
12203 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
12204
12205 // Convert the integer vector to pointer vector if the element is pointer.
12206 if (EltTy->isPointerTy())
12207 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
12208
12209 SV->replaceAllUsesWith(SubVec);
12210 }
12211
12212 return true;
12213}
12214
12215/// \brief Get a mask consisting of sequential integers starting from \p Start.
12216///
12217/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
12218static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
12219 unsigned NumElts) {
12220 SmallVector<Constant *, 16> Mask;
12221 for (unsigned i = 0; i < NumElts; i++)
12222 Mask.push_back(Builder.getInt32(Start + i));
12223
12224 return ConstantVector::get(Mask);
12225}
12226
12227/// \brief Lower an interleaved store into a vstN intrinsic.
12228///
12229/// E.g. Lower an interleaved store (Factor = 3):
12230/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
12231/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
12232/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
12233///
12234/// Into:
12235/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
12236/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
12237/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
12238/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
12239///
12240/// Note that the new shufflevectors will be removed and we'll only generate one
12241/// vst3 instruction in CodeGen.
12242bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
12243 ShuffleVectorInst *SVI,
12244 unsigned Factor) const {
12245 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12246 "Invalid interleave factor");
12247
12248 VectorType *VecTy = SVI->getType();
12249 assert(VecTy->getVectorNumElements() % Factor == 0 &&
12250 "Invalid interleaved store");
12251
12252 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
12253 Type *EltTy = VecTy->getVectorElementType();
12254 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
12255
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012256 const DataLayout &DL = SI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012257 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
12258 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012259
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012260 // Skip if we do not have NEON and skip illegal vector types and vector types
12261 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
12262 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
12263 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012264 return false;
12265
12266 Value *Op0 = SVI->getOperand(0);
12267 Value *Op1 = SVI->getOperand(1);
12268 IRBuilder<> Builder(SI);
12269
12270 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
12271 // vectors to integer vectors.
12272 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012273 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012274
12275 // Convert to the corresponding integer vector.
12276 Type *IntVecTy =
12277 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
12278 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
12279 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
12280
12281 SubVecTy = VectorType::get(IntTy, NumSubElts);
12282 }
12283
Craig Topper26260942015-10-18 05:15:34 +000012284 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
12285 Intrinsic::arm_neon_vst3,
12286 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000012287 SmallVector<Value *, 6> Ops;
12288
12289 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
12290 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
12291
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012292 Type *Tys[] = { Int8Ptr, SubVecTy };
12293 Function *VstNFunc = Intrinsic::getDeclaration(
12294 SI->getModule(), StoreInts[Factor - 2], Tys);
12295
Hao Liu2cd34bb2015-06-26 02:45:36 +000012296 // Split the shufflevector operands into sub vectors for the new vstN call.
12297 for (unsigned i = 0; i < Factor; i++)
12298 Ops.push_back(Builder.CreateShuffleVector(
12299 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
12300
12301 Ops.push_back(Builder.getInt32(SI->getAlignment()));
12302 Builder.CreateCall(VstNFunc, Ops);
12303 return true;
12304}
12305
Oliver Stannardc24f2172014-05-09 14:01:47 +000012306enum HABaseType {
12307 HA_UNKNOWN = 0,
12308 HA_FLOAT,
12309 HA_DOUBLE,
12310 HA_VECT64,
12311 HA_VECT128
12312};
12313
12314static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
12315 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000012316 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012317 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
12318 uint64_t SubMembers = 0;
12319 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
12320 return false;
12321 Members += SubMembers;
12322 }
Craig Toppere3dcce92015-08-01 22:20:21 +000012323 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012324 uint64_t SubMembers = 0;
12325 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
12326 return false;
12327 Members += SubMembers * AT->getNumElements();
12328 } else if (Ty->isFloatTy()) {
12329 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
12330 return false;
12331 Members = 1;
12332 Base = HA_FLOAT;
12333 } else if (Ty->isDoubleTy()) {
12334 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
12335 return false;
12336 Members = 1;
12337 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000012338 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012339 Members = 1;
12340 switch (Base) {
12341 case HA_FLOAT:
12342 case HA_DOUBLE:
12343 return false;
12344 case HA_VECT64:
12345 return VT->getBitWidth() == 64;
12346 case HA_VECT128:
12347 return VT->getBitWidth() == 128;
12348 case HA_UNKNOWN:
12349 switch (VT->getBitWidth()) {
12350 case 64:
12351 Base = HA_VECT64;
12352 return true;
12353 case 128:
12354 Base = HA_VECT128;
12355 return true;
12356 default:
12357 return false;
12358 }
12359 }
12360 }
12361
12362 return (Members > 0 && Members <= 4);
12363}
12364
Tim Northovere95c5b32015-02-24 17:22:34 +000012365/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
12366/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
12367/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000012368bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
12369 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012370 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12371 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012372 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012373
12374 HABaseType Base = HA_UNKNOWN;
12375 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012376 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12377 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12378
12379 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12380 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012381}
Joseph Tremouletf748c892015-11-07 01:11:31 +000012382
12383unsigned ARMTargetLowering::getExceptionPointerRegister(
12384 const Constant *PersonalityFn) const {
12385 // Platforms which do not use SjLj EH may return values in these registers
12386 // via the personality function.
12387 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
12388}
12389
12390unsigned ARMTargetLowering::getExceptionSelectorRegister(
12391 const Constant *PersonalityFn) const {
12392 // Platforms which do not use SjLj EH may return values in these registers
12393 // via the personality function.
12394 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
12395}